diff --git a/Contracts/SearchModels/RoleSearchModel.cs b/Contracts/SearchModels/RoleSearchModel.cs index 058eda2..f0df1b8 100644 --- a/Contracts/SearchModels/RoleSearchModel.cs +++ b/Contracts/SearchModels/RoleSearchModel.cs @@ -6,8 +6,9 @@ using System.Threading.Tasks; namespace Contracts.SearchModels { - public class RoleSearchModel - { - public Guid? Id { get; set; } - } + public class RoleSearchModel + { + public Guid? Id { get; set; } + public string? Name { get; set; } + } } \ No newline at end of file diff --git a/DatabaseImplement/Implements/RoleStorage.cs b/DatabaseImplement/Implements/RoleStorage.cs index 4394a59..a1d8e63 100644 --- a/DatabaseImplement/Implements/RoleStorage.cs +++ b/DatabaseImplement/Implements/RoleStorage.cs @@ -9,80 +9,82 @@ using System.Threading.Tasks; namespace DatabaseImplement.Implements { - public class RoleStorage : IRoleStorage - { - public RoleBindingModel? Delete(RoleSearchModel model) - { - if (model.Id is null) - { - return null; - } + public class RoleStorage : IRoleStorage + { + public RoleBindingModel? Delete(RoleSearchModel model) + { + if (model.Id is null) + { + return null; + } - var context = new Database(); - var role = context.Roles.FirstOrDefault(r => r.Id == model.Id); - if (role is null) - { - return null; - } + var context = new Database(); + var role = context.Roles.FirstOrDefault(r => r.Id == model.Id); + if (role is null) + { + return null; + } - context.Remove(role); - context.SaveChanges(); - return role.GetBindingModel(); - } + context.Remove(role); + context.SaveChanges(); + return role.GetBindingModel(); + } - public RoleBindingModel? GetElement(RoleSearchModel model) - { - if (model.Id is null) - { - return null; - } - var context = new Database(); - return context.Roles - .FirstOrDefault(r => r.Id == model.Id) - ?.GetBindingModel(); - } + public RoleBindingModel? GetElement(RoleSearchModel model) + { + if (model.Id is null && string.IsNullOrWhiteSpace(model.Name)) + { + return null; + } + var context = new Database(); + return context.Roles + .FirstOrDefault(r => (model.Id.HasValue && r.Id == model.Id) + || (!string.IsNullOrWhiteSpace(model.Name) && r.Name.Contains(model.Name))) + ?.GetBindingModel(); + } - public IEnumerable GetList(RoleSearchModel? model) - { - var context = new Database(); - if (model is null) - { - return context.Roles.Select(r => r.GetBindingModel()); - } - if (model.Id is null) - { - return []; - } - return context.Roles - .Where(r => r.Id == model.Id) - .Select(r => r.GetBindingModel()); - } + public IEnumerable GetList(RoleSearchModel? model) + { + var context = new Database(); + if (model is null && string.IsNullOrWhiteSpace(model.Name)) + { + return context.Roles.Select(r => r.GetBindingModel()); + } + if (model.Id is null) + { + return []; + } + return context.Roles + .Where(r => (model.Id.HasValue && r.Id == model.Id) + || (!string.IsNullOrWhiteSpace(model.Name) && r.Name.Contains(model.Name))) + .Select(r => r.GetBindingModel()); + } - public RoleBindingModel? Insert(RoleBindingModel model) - { - var context = new Database(); - var newRole = Models.Role.ToRoleFromBinding(model); + public RoleBindingModel? Insert(RoleBindingModel model) + { + var context = new Database(); + var newRole = Models.Role.ToRoleFromBinding(model); - context.Roles.Add(newRole); - context.SaveChanges(); + context.Roles.Add(newRole); + context.SaveChanges(); - return newRole.GetBindingModel(); - } + return newRole.GetBindingModel(); + } - public RoleBindingModel? Update(RoleBindingModel model) - { - var context = new Database(); - var role = context.Roles.FirstOrDefault(r => r.Id == model.Id); + public RoleBindingModel? Update(RoleBindingModel model) + { + var context = new Database(); + var role = context.Roles.FirstOrDefault(r => r.Id == model.Id); - if (role is null) - { - return null; - } + if (role is null) + { + return null; + } - role.Update(model); + role.Update(model); - context.SaveChanges(); - return role.GetBindingModel(); - } - } + context.SaveChanges(); + return role.GetBindingModel(); + } + } } \ No newline at end of file