using System;
using System.Data.Entity;
using iiie.Authentication.Business.JWT;
using iiie.Authentication.DBO;
using iiie.Logs.DBO;
using iiie.WebApiUtils.BusinessManager;
using iiie.WebApiUtils.DBO;
namespace iiie.Authentication.DataAccess
{
///
/// Manage basic user informations
///
public class UserBasicManager : SqlServerManager
{
public override DbSet GetTable(UserEntities db)
{
return db.T_Users;
}
public override UserGet DbToDboGet(T_Users obj)
{
var user = new UserGet
{
Firstname = obj.firstname,
Id = obj.id,
Lastname = obj.lastname,
ProfileId = obj.profile_id,
Role = obj.role,
Username = obj.username
};
user.SetHash(obj.password);
user.SetSalt(obj.salt);
return user;
}
public override OpResult DboAddToDb(UserAdd obj, T_Users add)
{
add.username = obj.Username;
add.password = PasswordHash.CreateHash(obj.GetPlainTextPrivate());
add.salt = PasswordHash.GetSalt();
add.role = obj.Role;
add.firstname = obj.Firstname;
add.lastname = obj.Lastname;
add.profile_id = obj.ProfileId;
return OpResult.Ok(true);
}
public override OpResult DboEditToDb(UserEdit obj, T_Users edit)
{
if (obj.Private != null)
{
edit.salt = PasswordHash.GetSalt();
edit.password = PasswordHash.CreateHash(obj.GetPlainTextPrivate());
}
edit.firstname = obj.Firstname;
edit.lastname = obj.Lastname;
edit.role = obj.Role;
return OpResult.Ok(true);
}
public static OpResult GetSingleByUsername(string username)
{
return GetSingle(x => x.username.ToLower() == username.ToLower());
}
public static OpResult> GetMultipleSearch(string query, int page = 0, int perPage = Int32.MaxValue)
{
return GetMultiple(x => x.firstname.ToLower().Contains(query) || x.lastname.ToLower().Contains(query)
|| x.username.ToLower().Contains(query), x => x.username, page, perPage);
}
}
}