add role search by name

This commit is contained in:
mfnefd 2024-06-20 04:17:46 +04:00
parent b1d13a591d
commit 106ad56325
2 changed files with 72 additions and 69 deletions

View File

@ -6,8 +6,9 @@ using System.Threading.Tasks;
namespace Contracts.SearchModels namespace Contracts.SearchModels
{ {
public class RoleSearchModel public class RoleSearchModel
{ {
public Guid? Id { get; set; } public Guid? Id { get; set; }
} public string? Name { get; set; }
}
} }

View File

@ -9,80 +9,82 @@ using System.Threading.Tasks;
namespace DatabaseImplement.Implements namespace DatabaseImplement.Implements
{ {
public class RoleStorage : IRoleStorage public class RoleStorage : IRoleStorage
{ {
public RoleBindingModel? Delete(RoleSearchModel model) public RoleBindingModel? Delete(RoleSearchModel model)
{ {
if (model.Id is null) if (model.Id is null)
{ {
return null; return null;
} }
var context = new Database(); var context = new Database();
var role = context.Roles.FirstOrDefault(r => r.Id == model.Id); var role = context.Roles.FirstOrDefault(r => r.Id == model.Id);
if (role is null) if (role is null)
{ {
return null; return null;
} }
context.Remove(role); context.Remove(role);
context.SaveChanges(); context.SaveChanges();
return role.GetBindingModel(); return role.GetBindingModel();
} }
public RoleBindingModel? GetElement(RoleSearchModel model) public RoleBindingModel? GetElement(RoleSearchModel model)
{ {
if (model.Id is null) if (model.Id is null && string.IsNullOrWhiteSpace(model.Name))
{ {
return null; return null;
} }
var context = new Database(); var context = new Database();
return context.Roles return context.Roles
.FirstOrDefault(r => r.Id == model.Id) .FirstOrDefault(r => (model.Id.HasValue && r.Id == model.Id)
?.GetBindingModel(); || (!string.IsNullOrWhiteSpace(model.Name) && r.Name.Contains(model.Name)))
} ?.GetBindingModel();
}
public IEnumerable<RoleBindingModel> GetList(RoleSearchModel? model) public IEnumerable<RoleBindingModel> GetList(RoleSearchModel? model)
{ {
var context = new Database(); var context = new Database();
if (model is null) if (model is null && string.IsNullOrWhiteSpace(model.Name))
{ {
return context.Roles.Select(r => r.GetBindingModel()); return context.Roles.Select(r => r.GetBindingModel());
} }
if (model.Id is null) if (model.Id is null)
{ {
return []; return [];
} }
return context.Roles return context.Roles
.Where(r => r.Id == model.Id) .Where(r => (model.Id.HasValue && r.Id == model.Id)
.Select(r => r.GetBindingModel()); || (!string.IsNullOrWhiteSpace(model.Name) && r.Name.Contains(model.Name)))
} .Select(r => r.GetBindingModel());
}
public RoleBindingModel? Insert(RoleBindingModel model) public RoleBindingModel? Insert(RoleBindingModel model)
{ {
var context = new Database(); var context = new Database();
var newRole = Models.Role.ToRoleFromBinding(model); var newRole = Models.Role.ToRoleFromBinding(model);
context.Roles.Add(newRole); context.Roles.Add(newRole);
context.SaveChanges(); context.SaveChanges();
return newRole.GetBindingModel(); return newRole.GetBindingModel();
} }
public RoleBindingModel? Update(RoleBindingModel model) public RoleBindingModel? Update(RoleBindingModel model)
{ {
var context = new Database(); var context = new Database();
var role = context.Roles.FirstOrDefault(r => r.Id == model.Id); var role = context.Roles.FirstOrDefault(r => r.Id == model.Id);
if (role is null) if (role is null)
{ {
return null; return null;
} }
role.Update(model); role.Update(model);
context.SaveChanges(); context.SaveChanges();
return role.GetBindingModel(); return role.GetBindingModel();
} }
} }
} }