From 1abd4a86a5b3e3daddf3dc66704f7e5ef4d538ac Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Sun, 7 Apr 2024 16:58:41 +0400 Subject: [PATCH] lab-5 adding password to ClientSearchModel --- .../Implements/ClientStorage.cs | 48 +++++++++++-------- .../SearchModels/ClientSearchModel.cs | 1 + .../Implements/ClientStorage.cs | 19 +++++--- .../Implements/ClientStorage.cs | 38 +++++++-------- 4 files changed, 59 insertions(+), 47 deletions(-) diff --git a/BlacksmithWorkshop/BlackcmithWorkshopFileImplement/Implements/ClientStorage.cs b/BlacksmithWorkshop/BlackcmithWorkshopFileImplement/Implements/ClientStorage.cs index 48fc156..1d700c2 100644 --- a/BlacksmithWorkshop/BlackcmithWorkshopFileImplement/Implements/ClientStorage.cs +++ b/BlacksmithWorkshop/BlackcmithWorkshopFileImplement/Implements/ClientStorage.cs @@ -14,67 +14,73 @@ namespace BlacksmithWorkshopFileImplement.Implements { public class ClientStorage : IClientStorage { - private readonly DataFileSingleton source; + private readonly DataFileSingleton _source; public ClientStorage() { - source = DataFileSingleton.GetInstance(); + _source = DataFileSingleton.GetInstance(); } public List GetFullList() { - return source.Clients + return _source.Clients .Select(x => x.GetViewModel) .ToList(); } - public List GetFilteredList(ClientSearchModel - model) + public List GetFilteredList(ClientSearchModel model) { - if (string.IsNullOrEmpty(model.ClientFIO) && string.IsNullOrEmpty(model.Email)) + if (string.IsNullOrEmpty(model.ClientFIO) && + string.IsNullOrEmpty(model.Email) && + string.IsNullOrEmpty(model.Password)) { return new(); } - return source.Clients - .Where(x => string.IsNullOrEmpty(model.ClientFIO) || x.ClientFIO.Contains(model.ClientFIO) && - (string.IsNullOrEmpty(model.Email) || x.ClientFIO.Contains(model.Email))) + return _source.Clients + .Where(x => + (string.IsNullOrEmpty(model.ClientFIO) || x.ClientFIO.Contains(model.ClientFIO)) && + (string.IsNullOrEmpty(model.Email) || x.Email.Contains(model.Email)) && + (string.IsNullOrEmpty(model.Password) || x.Password.Contains(model.Password))) .Select(x => x.GetViewModel) .ToList(); } public ClientViewModel? GetElement(ClientSearchModel model) { - return source.Clients - .FirstOrDefault(x => (string.IsNullOrEmpty(model.ClientFIO) || x.ClientFIO == model.ClientFIO) && - (!model.Id.HasValue || x.Id == model.Id) && (string.IsNullOrEmpty(model.Email) || x.Email == model.Email)) - ?.GetViewModel; + return _source.Clients + .FirstOrDefault(x => + (string.IsNullOrEmpty(model.ClientFIO) || x.ClientFIO == model.ClientFIO) && + (string.IsNullOrEmpty(model.Email) || x.Email == model.Email) && + (string.IsNullOrEmpty(model.Password) || x.Password == model.Password) && + (!model.Id.HasValue || x.Id == model.Id)) + ?.GetViewModel; } public ClientViewModel? Insert(ClientBindingModel model) { - model.Id = source.Clients.Count > 0 ? source.Clients.Max(x => x.Id) + 1 : 1; + model.Id = _source.Clients.Count > 0 ? _source.Clients.Max(x => x.Id) + 1 : 1; var newClient = Client.Create(model); if (newClient == null) { return null; } - source.Clients.Add(newClient); - source.SaveClients(); + _source.Clients.Add(newClient); + _source.SaveClients(); return newClient.GetViewModel; } public ClientViewModel? Update(ClientBindingModel model) { - var client = source.Clients.FirstOrDefault(x => x.Id == model.Id); + var client = _source.Clients.FirstOrDefault(x => x.Id == model.Id); if (client == null) { return null; } client.Update(model); - source.SaveClients(); + _source.SaveClients(); return client.GetViewModel; } public ClientViewModel? Delete(ClientBindingModel model) { - var element = source.Clients.FirstOrDefault(rec => rec.Id == model.Id); + var element = _source.Clients.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { - source.Clients.Remove(element); - source.SaveClients(); + _source.Clients.Remove(element); + _source.SaveClients(); return element.GetViewModel; } return null; diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/ClientSearchModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/ClientSearchModel.cs index fa3a984..3513eb6 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/ClientSearchModel.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/ClientSearchModel.cs @@ -11,5 +11,6 @@ namespace BlacksmithWorkshopContracts.SearchModels public int? Id { get; set; } public string? ClientFIO { get; set; } public string? Email { get; set; } + public string? Password { get; set; } } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ClientStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ClientStorage.cs index 13f9894..c3b595b 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ClientStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ClientStorage.cs @@ -23,28 +23,35 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements public List GetFilteredList(ClientSearchModel model) { if (string.IsNullOrEmpty(model.ClientFIO) && - string.IsNullOrEmpty(model.Email)) + string.IsNullOrEmpty(model.Email) && + string.IsNullOrEmpty(model.Password)) { return new(); } using var context = new BlacksmithWorkshopDataBase(); return context.Clients .Where(x => - (string.IsNullOrEmpty(model.ClientFIO) || x.ClientFIO.Contains(model.ClientFIO)) && - (string.IsNullOrEmpty(model.Email) || x.ClientFIO.Contains(model.Email))) + (string.IsNullOrEmpty(model.ClientFIO) || x.ClientFIO.Contains(model.ClientFIO)) && + (string.IsNullOrEmpty(model.Email) || x.Email.Contains(model.Email)) && + (string.IsNullOrEmpty(model.Password) || x.Password.Contains(model.Password))) .Select(x => x.GetViewModel) .ToList(); } public ClientViewModel? GetElement(ClientSearchModel model) { - if (string.IsNullOrEmpty(model.ClientFIO) && string.IsNullOrEmpty(model.Email) && !model.Id.HasValue) + if (string.IsNullOrEmpty(model.ClientFIO) && + string.IsNullOrEmpty(model.Email) && + string.IsNullOrEmpty(model.Password) && + !model.Id.HasValue) { return null; } using var context = new BlacksmithWorkshopDataBase(); return context.Clients - .FirstOrDefault(x => (string.IsNullOrEmpty(model.ClientFIO) || x.ClientFIO == model.ClientFIO) && - (!model.Id.HasValue || x.Id == model.Id) && (string.IsNullOrEmpty(model.Email) || x.Email == model.Email)) + .FirstOrDefault(x => (string.IsNullOrEmpty(model.ClientFIO) || x.ClientFIO == model.ClientFIO) && + (string.IsNullOrEmpty(model.Email) || x.Email == model.Email) && + (string.IsNullOrEmpty(model.Password) || x.Password == model.Password) && + (!model.Id.HasValue || x.Id == model.Id)) ?.GetViewModel; } public ClientViewModel? Insert(ClientBindingModel model) diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ClientStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ClientStorage.cs index 01f0da7..028e78a 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ClientStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ClientStorage.cs @@ -29,31 +29,29 @@ namespace BlacksmithWorkshopListImplement.Implements } public List GetFilteredList(ClientSearchModel model) { - var result = new List(); - if (string.IsNullOrEmpty(model.ClientFIO) && string.IsNullOrEmpty(model.Email)) + if (string.IsNullOrEmpty(model.ClientFIO) && + string.IsNullOrEmpty(model.Email) && + string.IsNullOrEmpty(model.Password)) { - return result; + return new(); } - foreach (var client in _source.Clients) - { - if (model.ClientFIO != null && client.ClientFIO.Contains(model.ClientFIO)) - { - result.Add(client.GetViewModel); - } - } - return result; + return _source.Clients + .Where(x => + (string.IsNullOrEmpty(model.ClientFIO) || x.ClientFIO.Contains(model.ClientFIO)) && + (string.IsNullOrEmpty(model.Email) || x.Email.Contains(model.Email)) && + (string.IsNullOrEmpty(model.Password) || x.Password.Contains(model.Password))) + .Select(x => x.GetViewModel) + .ToList(); } public ClientViewModel? GetElement(ClientSearchModel model) { - foreach (var client in _source.Clients) - { - if ((string.IsNullOrEmpty(model.ClientFIO) || client.ClientFIO == model.ClientFIO) && - (!model.Id.HasValue || client.Id == model.Id) && (string.IsNullOrEmpty(model.Email) || client.Email == model.Email)) - { - return client.GetViewModel; - } - } - return null; + return _source.Clients + .FirstOrDefault(x => + (string.IsNullOrEmpty(model.ClientFIO) || x.ClientFIO == model.ClientFIO) && + (string.IsNullOrEmpty(model.Email) || x.Email == model.Email) && + (string.IsNullOrEmpty(model.Password) || x.Password == model.Password) && + (!model.Id.HasValue || x.Id == model.Id)) + ?.GetViewModel; } public ClientViewModel? Insert(ClientBindingModel model) {