From 50bc49151d9d1f187ebcc70d142a51a3d50489d6 Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Sun, 5 May 2024 23:59:47 +0400 Subject: [PATCH] add BusinessLogic --- CarShowroom/CarShowroom.sln | 10 +- .../BusinessLogic/CarLogic.cs | 89 +++++++++++++++++ .../BusinessLogic/ClientLogic.cs | 95 ++++++++++++++++++ .../BusinessLogic/EmployeeLogic.cs | 97 +++++++++++++++++++ .../BusinessLogic/MakeLogic.cs | 93 ++++++++++++++++++ .../BusinessLogic/ModelStorage.cs | 97 +++++++++++++++++++ .../BusinessLogic/SaleLogic.cs | 91 +++++++++++++++++ .../BusinessLogic/ServiceLogic.cs | 96 ++++++++++++++++++ .../CarShowroomBusinessLogic.csproj | 14 +++ .../BusinessLogic/IMakeLogic.cs | 2 +- .../SearchModel/ClientSearch.cs | 1 + .../SearchModel/MakeSearch.cs | 1 + .../SearchModel/ModelSearch.cs | 1 + .../SearchModel/ServiceSearch.cs | 1 + .../Storages/CarStorage.cs | 4 +- .../Storages/ClientStorage.cs | 16 +-- .../Storages/EmployeeStorage.cs | 18 ++-- .../Storages/MakeStorage.cs | 16 +-- .../Storages/ModelStorage.cs | 24 +++-- .../Storages/SaleStorage.cs | 4 +- .../{IServiceStorage.cs => ServiceStorage.cs} | 16 +-- 21 files changed, 741 insertions(+), 45 deletions(-) create mode 100644 CarShowroom/CarShowroomBusinessLogic/BusinessLogic/CarLogic.cs create mode 100644 CarShowroom/CarShowroomBusinessLogic/BusinessLogic/ClientLogic.cs create mode 100644 CarShowroom/CarShowroomBusinessLogic/BusinessLogic/EmployeeLogic.cs create mode 100644 CarShowroom/CarShowroomBusinessLogic/BusinessLogic/MakeLogic.cs create mode 100644 CarShowroom/CarShowroomBusinessLogic/BusinessLogic/ModelStorage.cs create mode 100644 CarShowroom/CarShowroomBusinessLogic/BusinessLogic/SaleLogic.cs create mode 100644 CarShowroom/CarShowroomBusinessLogic/BusinessLogic/ServiceLogic.cs create mode 100644 CarShowroom/CarShowroomBusinessLogic/CarShowroomBusinessLogic.csproj rename CarShowroom/CarShowroomDatabaseStorage/Storages/{IServiceStorage.cs => ServiceStorage.cs} (85%) diff --git a/CarShowroom/CarShowroom.sln b/CarShowroom/CarShowroom.sln index c8e5b51..ecb200a 100644 --- a/CarShowroom/CarShowroom.sln +++ b/CarShowroom/CarShowroom.sln @@ -5,9 +5,11 @@ VisualStudioVersion = 17.9.34723.18 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarShowroomDataModels", "CarShowroomDataModels\CarShowroomDataModels.csproj", "{7EECD77A-5D26-4093-85B0-F2D33C6F0C2C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarShowroomContracts", "CarShowroomContracts\CarShowroomContracts.csproj", "{6274B5CD-C339-4FD3-AC45-DE8384AD4CB7}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarShowroomContracts", "CarShowroomContracts\CarShowroomContracts.csproj", "{6274B5CD-C339-4FD3-AC45-DE8384AD4CB7}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarShowroomDatabaseStorage", "CarShowroomDatabaseStorage\CarShowroomDatabaseStorage.csproj", "{7F0A0E4D-6EFD-459D-AFFF-99F3185243F8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarShowroomDatabaseStorage", "CarShowroomDatabaseStorage\CarShowroomDatabaseStorage.csproj", "{7F0A0E4D-6EFD-459D-AFFF-99F3185243F8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarShowroomBusinessLogic", "CarShowroomBusinessLogic\CarShowroomBusinessLogic.csproj", "{B0DA0319-2DBC-4785-BA5F-338AD5E3E437}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -27,6 +29,10 @@ Global {7F0A0E4D-6EFD-459D-AFFF-99F3185243F8}.Debug|Any CPU.Build.0 = Debug|Any CPU {7F0A0E4D-6EFD-459D-AFFF-99F3185243F8}.Release|Any CPU.ActiveCfg = Release|Any CPU {7F0A0E4D-6EFD-459D-AFFF-99F3185243F8}.Release|Any CPU.Build.0 = Release|Any CPU + {B0DA0319-2DBC-4785-BA5F-338AD5E3E437}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B0DA0319-2DBC-4785-BA5F-338AD5E3E437}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B0DA0319-2DBC-4785-BA5F-338AD5E3E437}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B0DA0319-2DBC-4785-BA5F-338AD5E3E437}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/CarLogic.cs b/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/CarLogic.cs new file mode 100644 index 0000000..df08933 --- /dev/null +++ b/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/CarLogic.cs @@ -0,0 +1,89 @@ +using CarShowroomContracts.BusinessLogic; +using CarShowroomContracts.StorageContracts; +using CarShowroomDataModels.Dtos; +using CarShowroomDataModels.SearchModel; +using CarShowroomDataModels.Views; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarShowroomBusinessLogic.BusinessLogic +{ + public class CarLogic : ICarLogic + { + private readonly ICarStorage _carStorage; + + public CarLogic(ICarStorage storage) + { + _carStorage = storage; + } + + public List? ReadList(CarSearch? model) + { + var list = model == null ? _carStorage.GetFullList() : _carStorage.GetFilteredList(model); + if (list == null) + { + return null; + } + return list; + } + + public CarView? ReadElement(CarSearch model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + var element = _carStorage.GetElement(model); + if (element == null) + { + return null; + } + return element; + } + + public bool Create(CarDto model) + { + CheckModel(model); + if (_carStorage.Insert(model) == null) + { + return false; + } + return true; + } + + public bool Update(CarDto model) + { + CheckModel(model); + if (_carStorage.Update(model) == null) + { + return false; + } + return true; + } + + public bool Delete(CarDto model) + { + CheckModel(model, false); + if (_carStorage.Delete(model) == null) + { + return false; + } + return true; + } + + private void CheckModel(CarDto model, bool withParams = true) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + if (!withParams) + return; + if (model.ModelId < 0) + throw new InvalidOperationException(); + if (string.IsNullOrEmpty(model.Color)) + throw new InvalidOperationException(); + } + } +} diff --git a/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/ClientLogic.cs b/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/ClientLogic.cs new file mode 100644 index 0000000..75cf837 --- /dev/null +++ b/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/ClientLogic.cs @@ -0,0 +1,95 @@ +using CarShowroomContracts.BusinessLogic; +using CarShowroomContracts.StorageContracts; +using CarShowroomDataModels.Dtos; +using CarShowroomDataModels.SearchModel; +using CarShowroomDataModels.Views; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarShowroomBusinessLogic.BusinessLogic +{ + public class ClientLogic : IClientLogic + { + private readonly IClientStorage _clientStorage; + + public ClientLogic(IClientStorage storage) + { + _clientStorage = storage; + } + + public List? ReadList(ClientSearch? model) + { + var list = model == null ? _clientStorage.GetFullList() : _clientStorage.GetFilteredList(model); + if (list == null) + { + return null; + } + return list; + } + + public ClientView? ReadElement(ClientSearch model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + var element = _clientStorage.GetElement(model); + if (element == null) + { + return null; + } + return element; + } + + public bool Create(ClientDto model) + { + CheckModel(model); + if (_clientStorage.Insert(model) == null) + { + return false; + } + return true; + } + + public bool Update(ClientDto model) + { + CheckModel(model); + if (_clientStorage.Update(model) == null) + { + return false; + } + return true; + } + + public bool Delete(ClientDto model) + { + CheckModel(model, false); + if (_clientStorage.Delete(model) == null) + { + return false; + } + return true; + } + + private void CheckModel(ClientDto model, bool withParams = true) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + if (!withParams) + return; + if (string.IsNullOrEmpty(model.Name)) + throw new InvalidOperationException(); + if (string.IsNullOrEmpty(model.PhoneNumber)) + throw new InvalidOperationException(); + var element = _clientStorage.GetElement(new ClientSearch + { + PhoneNumber = model.PhoneNumber, + }); + if (element != null && element.Id != model.Id) + throw new InvalidOperationException(); + } + } +} diff --git a/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/EmployeeLogic.cs b/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/EmployeeLogic.cs new file mode 100644 index 0000000..d37739d --- /dev/null +++ b/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/EmployeeLogic.cs @@ -0,0 +1,97 @@ +using CarShowroomContracts.BusinessLogic; +using CarShowroomContracts.StorageContracts; +using CarShowroomDataModels.Dtos; +using CarShowroomDataModels.SearchModel; +using CarShowroomDataModels.Views; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarShowroomBusinessLogic.BusinessLogic +{ + public class EmployeeLogic : IEmployeeLogic + { + private readonly IEmployeeStorage _employeeStorage; + + public EmployeeLogic(IEmployeeStorage storage) + { + _employeeStorage = storage; + } + + public List? ReadList(EmployeeSearch? model) + { + var list = model == null ? _employeeStorage.GetFullList() : _employeeStorage.GetFilteredList(model); + if (list == null) + { + return null; + } + return list; + } + + public EmployeeView? ReadElement(EmployeeSearch model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + var element = _employeeStorage.GetElement(model); + if (element == null) + { + return null; + } + return element; + } + + public bool Create(EmployeeDto model) + { + CheckModel(model); + if (_employeeStorage.Insert(model) == null) + { + return false; + } + return true; + } + + public bool Update(EmployeeDto model) + { + CheckModel(model); + if (_employeeStorage.Update(model) == null) + { + return false; + } + return true; + } + + public bool Delete(EmployeeDto model) + { + CheckModel(model, false); + if (_employeeStorage.Delete(model) == null) + { + return false; + } + return true; + } + + private void CheckModel(EmployeeDto model, bool withParams = true) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + if (!withParams) + return; + if (string.IsNullOrEmpty(model.Name)) + throw new InvalidOperationException(); + if (string.IsNullOrEmpty(model.Email)) + throw new InvalidOperationException(); + if (string.IsNullOrEmpty(model.Password)) + throw new InvalidOperationException(); + var element = _employeeStorage.GetElement(new EmployeeSearch + { + Email = model.Email, + }); + if (element != null && element.Id != model.Id) + throw new InvalidOperationException(); + } + } +} diff --git a/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/MakeLogic.cs b/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/MakeLogic.cs new file mode 100644 index 0000000..3d65350 --- /dev/null +++ b/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/MakeLogic.cs @@ -0,0 +1,93 @@ +using CarShowroomContracts.BusinessLogic; +using CarShowroomContracts.StorageContracts; +using CarShowroomDataModels.Dtos; +using CarShowroomDataModels.SearchModel; +using CarShowroomDataModels.Views; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarShowroomBusinessLogic.BusinessLogic +{ + public class MakeLogic : IMakeLogic + { + private readonly IMakeStorage _makeStorage; + + public MakeLogic(IMakeStorage storage) + { + _makeStorage = storage; + } + + public List? ReadList(MakeSearch? model) + { + var list = model == null ? _makeStorage.GetFullList() : _makeStorage.GetFilteredList(model); + if (list == null) + { + return null; + } + return list; + } + + public MakeView? ReadElement(MakeSearch model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + var element = _makeStorage.GetElement(model); + if (element == null) + { + return null; + } + return element; + } + + public bool Create(MakeDto model) + { + CheckModel(model); + if (_makeStorage.Insert(model) == null) + { + return false; + } + return true; + } + + public bool Update(MakeDto model) + { + CheckModel(model); + if (_makeStorage.Update(model) == null) + { + return false; + } + return true; + } + + public bool Delete(MakeDto model) + { + CheckModel(model, false); + if (_makeStorage.Delete(model) == null) + { + return false; + } + return true; + } + + private void CheckModel(MakeDto model, bool withParams = true) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + if (!withParams) + return; + if (string.IsNullOrEmpty(model.Name)) + throw new InvalidOperationException(); + var element = _makeStorage.GetElement(new MakeSearch + { + Name = model.Name, + }); + if (element != null && element.Id != model.Id) + throw new InvalidOperationException(); + } + } +} diff --git a/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/ModelStorage.cs b/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/ModelStorage.cs new file mode 100644 index 0000000..fa74589 --- /dev/null +++ b/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/ModelStorage.cs @@ -0,0 +1,97 @@ +using CarShowroomContracts.BusinessLogic; +using CarShowroomContracts.StorageContracts; +using CarShowroomDataModels.Dtos; +using CarShowroomDataModels.SearchModel; +using CarShowroomDataModels.Views; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarShowroomBusinessLogic.BusinessLogic +{ + public class ModelLogic : IModelLogic + { + private readonly IModelStorage _modelStorage; + + public ModelLogic(IModelStorage storage) + { + _modelStorage = storage; + } + + public List? ReadList(ModelSearch? model) + { + var list = model == null ? _modelStorage.GetFullList() : _modelStorage.GetFilteredList(model); + if (list == null) + { + return null; + } + return list; + } + + public ModelView? ReadElement(ModelSearch model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + var element = _modelStorage.GetElement(model); + if (element == null) + { + return null; + } + return element; + } + + public bool Create(ModelDto model) + { + CheckModel(model); + if (_modelStorage.Insert(model) == null) + { + return false; + } + return true; + } + + public bool Update(ModelDto model) + { + CheckModel(model); + if (_modelStorage.Update(model) == null) + { + return false; + } + return true; + } + + public bool Delete(ModelDto model) + { + CheckModel(model, false); + if (_modelStorage.Delete(model) == null) + { + return false; + } + return true; + } + + private void CheckModel(ModelDto model, bool withParams = true) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + if (!withParams) + return; + if (string.IsNullOrEmpty(model.Name)) + throw new InvalidOperationException(); + if (model.MakeId < 0) + throw new InvalidOperationException(); + if (model.Price < 0) + throw new InvalidOperationException(); + var element = _modelStorage.GetElement(new ModelSearch + { + Name = model.Name, + }); + if (element != null && element.Id != model.Id) + throw new InvalidOperationException(); + } + } +} diff --git a/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/SaleLogic.cs b/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/SaleLogic.cs new file mode 100644 index 0000000..6f9dc24 --- /dev/null +++ b/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/SaleLogic.cs @@ -0,0 +1,91 @@ +using CarShowroomContracts.BusinessLogic; +using CarShowroomContracts.StorageContracts; +using CarShowroomDataModels.Dtos; +using CarShowroomDataModels.SearchModel; +using CarShowroomDataModels.Views; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarShowroomBusinessLogic.BusinessLogic +{ + public class SaleLogic : ISaleLogic + { + private readonly ISaleStorage _saleStorage; + + public SaleLogic(ISaleStorage storage) + { + _saleStorage = storage; + } + + public List? ReadList(SaleSearch? model) + { + var list = model == null ? _saleStorage.GetFullList() : _saleStorage.GetFilteredList(model); + if (list == null) + { + return null; + } + return list; + } + + public SaleView? ReadElement(SaleSearch model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + var element = _saleStorage.GetElement(model); + if (element == null) + { + return null; + } + return element; + } + + public bool Create(SaleDto model) + { + CheckModel(model); + if (_saleStorage.Insert(model) == null) + { + return false; + } + return true; + } + + public bool Update(SaleDto model) + { + CheckModel(model); + if (_saleStorage.Update(model) == null) + { + return false; + } + return true; + } + + public bool Delete(SaleDto model) + { + CheckModel(model, false); + if (_saleStorage.Delete(model) == null) + { + return false; + } + return true; + } + + private void CheckModel(SaleDto model, bool withParams = true) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + if (!withParams) + return; + if (model.Cost < 0) + throw new InvalidOperationException(); + if (model.EmployeeId < 0) + throw new InvalidOperationException(); + if (model.ClientId < 0) + throw new InvalidOperationException(); + } + } +} diff --git a/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/ServiceLogic.cs b/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/ServiceLogic.cs new file mode 100644 index 0000000..aa1594b --- /dev/null +++ b/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/ServiceLogic.cs @@ -0,0 +1,96 @@ +using CarShowroomContracts.BusinessLogic; +using CarShowroomContracts.StorageContracts; +using CarShowroomDataModels.Dtos; +using CarShowroomDataModels.SearchModel; +using CarShowroomDataModels.Views; +using ServiceShowroomContracts.BusinessLogic; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarShowroomBusinessLogic.BusinessLogic +{ + public class ServiceLogic : IServiceLogic + { + private readonly IServiceStorage _serviceStorage; + + public ServiceLogic(IServiceStorage storage) + { + _serviceStorage = storage; + } + + public List? ReadList(ServiceSearch? model) + { + var list = model == null ? _serviceStorage.GetFullList() : _serviceStorage.GetFilteredList(model); + if (list == null) + { + return null; + } + return list; + } + + public ServiceView? ReadElement(ServiceSearch model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + var element = _serviceStorage.GetElement(model); + if (element == null) + { + return null; + } + return element; + } + + public bool Create(ServiceDto model) + { + CheckModel(model); + if (_serviceStorage.Insert(model) == null) + { + return false; + } + return true; + } + + public bool Update(ServiceDto model) + { + CheckModel(model); + if (_serviceStorage.Update(model) == null) + { + return false; + } + return true; + } + + public bool Delete(ServiceDto model) + { + CheckModel(model, false); + if (_serviceStorage.Delete(model) == null) + { + return false; + } + return true; + } + + private void CheckModel(ServiceDto model, bool withParams = true) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + if (!withParams) + return; + if (string.IsNullOrEmpty(model.Name)) + throw new InvalidOperationException(); + if (model.Cost < 0) + throw new InvalidOperationException(); + var element = _serviceStorage.GetElement(new ServiceSearch + { + Name = model.Name, + }); + if (element != null && element.Id != model.Id) + throw new InvalidOperationException(); + } + } +} diff --git a/CarShowroom/CarShowroomBusinessLogic/CarShowroomBusinessLogic.csproj b/CarShowroom/CarShowroomBusinessLogic/CarShowroomBusinessLogic.csproj new file mode 100644 index 0000000..c00b1c4 --- /dev/null +++ b/CarShowroom/CarShowroomBusinessLogic/CarShowroomBusinessLogic.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/CarShowroom/CarShowroomContracts/BusinessLogic/IMakeLogic.cs b/CarShowroom/CarShowroomContracts/BusinessLogic/IMakeLogic.cs index db001b1..05a2abf 100644 --- a/CarShowroom/CarShowroomContracts/BusinessLogic/IMakeLogic.cs +++ b/CarShowroom/CarShowroomContracts/BusinessLogic/IMakeLogic.cs @@ -1,4 +1,4 @@ -using CarShowroomContracts.Dtos; +using CarShowroomDataModels.Dtos; using CarShowroomDataModels.Views; using CarShowroomDataModels.SearchModel; using System; diff --git a/CarShowroom/CarShowroomDataModels/SearchModel/ClientSearch.cs b/CarShowroom/CarShowroomDataModels/SearchModel/ClientSearch.cs index 1f7627f..ac09b8f 100644 --- a/CarShowroom/CarShowroomDataModels/SearchModel/ClientSearch.cs +++ b/CarShowroom/CarShowroomDataModels/SearchModel/ClientSearch.cs @@ -9,5 +9,6 @@ namespace CarShowroomDataModels.SearchModel public class ClientSearch { public int? Id; + public string? PhoneNumber; } } diff --git a/CarShowroom/CarShowroomDataModels/SearchModel/MakeSearch.cs b/CarShowroom/CarShowroomDataModels/SearchModel/MakeSearch.cs index ef33e75..8e023fd 100644 --- a/CarShowroom/CarShowroomDataModels/SearchModel/MakeSearch.cs +++ b/CarShowroom/CarShowroomDataModels/SearchModel/MakeSearch.cs @@ -9,5 +9,6 @@ namespace CarShowroomDataModels.SearchModel public class MakeSearch { public int? Id; + public string? Name; } } diff --git a/CarShowroom/CarShowroomDataModels/SearchModel/ModelSearch.cs b/CarShowroom/CarShowroomDataModels/SearchModel/ModelSearch.cs index 775d5e4..6e8723d 100644 --- a/CarShowroom/CarShowroomDataModels/SearchModel/ModelSearch.cs +++ b/CarShowroom/CarShowroomDataModels/SearchModel/ModelSearch.cs @@ -9,5 +9,6 @@ namespace CarShowroomDataModels.SearchModel public class ModelSearch { public int? Id; + public string? Name; } } diff --git a/CarShowroom/CarShowroomDataModels/SearchModel/ServiceSearch.cs b/CarShowroom/CarShowroomDataModels/SearchModel/ServiceSearch.cs index f6a6879..de607ee 100644 --- a/CarShowroom/CarShowroomDataModels/SearchModel/ServiceSearch.cs +++ b/CarShowroom/CarShowroomDataModels/SearchModel/ServiceSearch.cs @@ -9,5 +9,6 @@ namespace CarShowroomDataModels.SearchModel public class ServiceSearch { public int? Id; + public string? Name; } } diff --git a/CarShowroom/CarShowroomDatabaseStorage/Storages/CarStorage.cs b/CarShowroom/CarShowroomDatabaseStorage/Storages/CarStorage.cs index 77a256e..c32d65c 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/Storages/CarStorage.cs +++ b/CarShowroom/CarShowroomDatabaseStorage/Storages/CarStorage.cs @@ -17,8 +17,8 @@ namespace CarShowroomDatabaseStorage.Storages { using var context = new CarShowroomDatabase(); return context.Cars - .Select(x => x.GetView()) - .ToList(); + .Select(x => x.GetView()) + .ToList(); } public List GetFilteredList(CarSearch model) diff --git a/CarShowroom/CarShowroomDatabaseStorage/Storages/ClientStorage.cs b/CarShowroom/CarShowroomDatabaseStorage/Storages/ClientStorage.cs index 8550da9..806993d 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/Storages/ClientStorage.cs +++ b/CarShowroom/CarShowroomDatabaseStorage/Storages/ClientStorage.cs @@ -17,32 +17,36 @@ namespace CarShowroomDatabaseStorage.Storages { using var context = new CarShowroomDatabase(); return context.Clients - .Select(x => x.GetView()) - .ToList(); + .Select(x => x.GetView()) + .ToList(); } public List GetFilteredList(ClientSearch model) { - if (!model.Id.HasValue) + if (!model.Id.HasValue && + string.IsNullOrEmpty(model.PhoneNumber)) { return new(); } using var context = new CarShowroomDatabase(); return context.Clients - .Where(x => !model.Id.HasValue || x.Id == model.Id) + .Where(x => !model.Id.HasValue || x.Id == model.Id && + (string.IsNullOrEmpty(model.PhoneNumber) || x.PhoneNumber.Equals(model.PhoneNumber))) .Select(x => x.GetView()) .ToList(); } public ClientView? GetElement(ClientSearch model) { - if (!model.Id.HasValue) + if (!model.Id.HasValue && + string.IsNullOrEmpty(model.PhoneNumber)) { return null; } using var context = new CarShowroomDatabase(); return context.Clients - .FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id) + .FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id && + (string.IsNullOrEmpty(model.PhoneNumber) || x.PhoneNumber.Equals(model.PhoneNumber))) ?.GetView(); } diff --git a/CarShowroom/CarShowroomDatabaseStorage/Storages/EmployeeStorage.cs b/CarShowroom/CarShowroomDatabaseStorage/Storages/EmployeeStorage.cs index ed2b16e..887e5b9 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/Storages/EmployeeStorage.cs +++ b/CarShowroom/CarShowroomDatabaseStorage/Storages/EmployeeStorage.cs @@ -17,8 +17,8 @@ namespace CarShowroomDatabaseStorage.Storages { using var context = new CarShowroomDatabase(); return context.Employees - .Select(x => x.GetView()) - .ToList(); + .Select(x => x.GetView()) + .ToList(); } public List GetFilteredList(EmployeeSearch model) @@ -31,10 +31,9 @@ namespace CarShowroomDatabaseStorage.Storages } using var context = new CarShowroomDatabase(); return context.Employees - .Where(x => - !model.Id.HasValue || x.Id == model.Id && - (string.IsNullOrEmpty(model.Password) || x.Password == model.Password) && - (string.IsNullOrEmpty(model.Email) || x.Password == model.Email)) + .Where(x => !model.Id.HasValue || x.Id == model.Id && + (string.IsNullOrEmpty(model.Password) || x.Password.Equals(model.Password)) && + (string.IsNullOrEmpty(model.Email) || x.Password.Equals(model.Email))) .Select(x => x.GetView()) .ToList(); } @@ -49,10 +48,9 @@ namespace CarShowroomDatabaseStorage.Storages } using var context = new CarShowroomDatabase(); return context.Employees - .FirstOrDefault(x => - !model.Id.HasValue || x.Id == model.Id && - (string.IsNullOrEmpty(model.Password) || x.Password == model.Password) && - (string.IsNullOrEmpty(model.Email) || x.Password == model.Email)) + .FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id && + (string.IsNullOrEmpty(model.Password) || x.Password.Equals(model.Password)) && + (string.IsNullOrEmpty(model.Email) || x.Password.Equals(model.Email))) ?.GetView(); } diff --git a/CarShowroom/CarShowroomDatabaseStorage/Storages/MakeStorage.cs b/CarShowroom/CarShowroomDatabaseStorage/Storages/MakeStorage.cs index e0450f0..9f1d89d 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/Storages/MakeStorage.cs +++ b/CarShowroom/CarShowroomDatabaseStorage/Storages/MakeStorage.cs @@ -17,32 +17,36 @@ namespace CarShowroomDatabaseStorage.Storages { using var context = new CarShowroomDatabase(); return context.Makes - .Select(x => x.GetView()) - .ToList(); + .Select(x => x.GetView()) + .ToList(); } public List GetFilteredList(MakeSearch model) { - if (!model.Id.HasValue) + if (!model.Id.HasValue && + string.IsNullOrEmpty(model.Name)) { return new(); } using var context = new CarShowroomDatabase(); return context.Makes - .Where(x => !model.Id.HasValue || x.Id == model.Id) + .Where(x => !model.Id.HasValue || x.Id == model.Id && + (string.IsNullOrEmpty(model.Name) || x.Name.Equals(model.Name))) .Select(x => x.GetView()) .ToList(); } public MakeView? GetElement(MakeSearch model) { - if (!model.Id.HasValue) + if (!model.Id.HasValue && + string.IsNullOrEmpty(model.Name)) { return null; } using var context = new CarShowroomDatabase(); return context.Makes - .FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id) + .FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id && + (string.IsNullOrEmpty(model.Name) || x.Name.Equals(model.Name))) ?.GetView(); } diff --git a/CarShowroom/CarShowroomDatabaseStorage/Storages/ModelStorage.cs b/CarShowroom/CarShowroomDatabaseStorage/Storages/ModelStorage.cs index bbccf4c..0116a8d 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/Storages/ModelStorage.cs +++ b/CarShowroom/CarShowroomDatabaseStorage/Storages/ModelStorage.cs @@ -17,32 +17,36 @@ namespace CarShowroomDatabaseStorage.Storages { using var context = new CarShowroomDatabase(); return context.Models - .Select(x => x.GetView()) - .ToList(); + .Select(x => x.GetView()) + .ToList(); } public List GetFilteredList(ModelSearch model) { - if (!model.Id.HasValue) + if (!model.Id.HasValue && + string.IsNullOrEmpty(model.Name)) { return new(); } using var context = new CarShowroomDatabase(); return context.Models - .Where(x => !model.Id.HasValue || x.Id == model.Id) + .Where(x => !model.Id.HasValue || x.Id == model.Id && + (string.IsNullOrEmpty(model.Name) || x.Name.Equals(model.Name))) .Select(x => x.GetView()) .ToList(); } public ModelView? GetElement(ModelSearch model) { - if (!model.Id.HasValue) + if (!model.Id.HasValue && + string.IsNullOrEmpty(model.Name)) { return null; } using var context = new CarShowroomDatabase(); return context.Models - .FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id) + .FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id && + (string.IsNullOrEmpty(model.Name) || x.Name.Equals(model.Name))) ?.GetView(); } @@ -62,14 +66,14 @@ namespace CarShowroomDatabaseStorage.Storages public ModelView? Update(ModelDto model) { using var context = new CarShowroomDatabase(); - var model = context.Models.FirstOrDefault(x => x.Id == model.Id); - if (model == null) + var entity = context.Models.FirstOrDefault(x => x.Id == model.Id); + if (entity == null) { return null; } - model.Update(model); + entity.Update(entity); context.SaveChanges(); - return model.GetView(); + return entity.GetView(); } public ModelView? Delete(ModelDto model) diff --git a/CarShowroom/CarShowroomDatabaseStorage/Storages/SaleStorage.cs b/CarShowroom/CarShowroomDatabaseStorage/Storages/SaleStorage.cs index 0dc31d6..70d2e69 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/Storages/SaleStorage.cs +++ b/CarShowroom/CarShowroomDatabaseStorage/Storages/SaleStorage.cs @@ -17,8 +17,8 @@ namespace CarShowroomDatabaseStorage.Storages { using var context = new CarShowroomDatabase(); return context.Sales - .Select(x => x.GetView()) - .ToList(); + .Select(x => x.GetView()) + .ToList(); } public List GetFilteredList(SaleSearch model) diff --git a/CarShowroom/CarShowroomDatabaseStorage/Storages/IServiceStorage.cs b/CarShowroom/CarShowroomDatabaseStorage/Storages/ServiceStorage.cs similarity index 85% rename from CarShowroom/CarShowroomDatabaseStorage/Storages/IServiceStorage.cs rename to CarShowroom/CarShowroomDatabaseStorage/Storages/ServiceStorage.cs index 4dbd1fb..26c0754 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/Storages/IServiceStorage.cs +++ b/CarShowroom/CarShowroomDatabaseStorage/Storages/ServiceStorage.cs @@ -17,32 +17,36 @@ namespace CarShowroomDatabaseStorage.Storages { using var context = new CarShowroomDatabase(); return context.Services - .Select(x => x.GetView()) - .ToList(); + .Select(x => x.GetView()) + .ToList(); } public List GetFilteredList(ServiceSearch model) { - if (!model.Id.HasValue) + if (!model.Id.HasValue && + string.IsNullOrEmpty(model.Name)) { return new(); } using var context = new CarShowroomDatabase(); return context.Services - .Where(x => !model.Id.HasValue || x.Id == model.Id) + .Where(x => !model.Id.HasValue || x.Id == model.Id && + (string.IsNullOrEmpty(model.Name) || x.Name.Equals(model.Name))) .Select(x => x.GetView()) .ToList(); } public ServiceView? GetElement(ServiceSearch model) { - if (!model.Id.HasValue) + if (!model.Id.HasValue && + string.IsNullOrEmpty(model.Name)) { return null; } using var context = new CarShowroomDatabase(); return context.Services - .FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id) + .FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id && + (string.IsNullOrEmpty(model.Name) || x.Name.Equals(model.Name))) ?.GetView(); }