From 14f80b4425ab8476d04d8d9cf7ca980123a51938 Mon Sep 17 00:00:00 2001 From: Vadim Date: Sat, 8 Apr 2023 20:09:07 +0400 Subject: [PATCH] BusinessLogic --- .../BusinessLogics/CarLogic.cs | 112 +++++++++++++++++ .../BusinessLogics/EmployerLogic.cs | 111 +++++++++++++++++ .../BusinessLogics/MaintenanceLogic.cs | 99 +++++++++++++++ .../BusinessLogics/ReportLogic.cs | 44 +++++++ .../BusinessLogics/ServiceLogic.cs | 91 ++++++++++++++ .../BusinessLogics/SpareLogic.cs | 109 +++++++++++++++++ .../BusinessLogics/StorekeeperLogic.cs | 113 ++++++++++++++++++ .../BusinessLogics/WorkDurationLogic.cs | 96 +++++++++++++++ .../BusinessLogics/WorkLogic.cs | 108 +++++++++++++++++ Sto/STOBusinessLogic/STOBusinessLogic.csproj | 26 ++++ 10 files changed, 909 insertions(+) create mode 100644 Sto/STOBusinessLogic/BusinessLogics/CarLogic.cs create mode 100644 Sto/STOBusinessLogic/BusinessLogics/EmployerLogic.cs create mode 100644 Sto/STOBusinessLogic/BusinessLogics/MaintenanceLogic.cs create mode 100644 Sto/STOBusinessLogic/BusinessLogics/ReportLogic.cs create mode 100644 Sto/STOBusinessLogic/BusinessLogics/ServiceLogic.cs create mode 100644 Sto/STOBusinessLogic/BusinessLogics/SpareLogic.cs create mode 100644 Sto/STOBusinessLogic/BusinessLogics/StorekeeperLogic.cs create mode 100644 Sto/STOBusinessLogic/BusinessLogics/WorkDurationLogic.cs create mode 100644 Sto/STOBusinessLogic/BusinessLogics/WorkLogic.cs create mode 100644 Sto/STOBusinessLogic/STOBusinessLogic.csproj diff --git a/Sto/STOBusinessLogic/BusinessLogics/CarLogic.cs b/Sto/STOBusinessLogic/BusinessLogics/CarLogic.cs new file mode 100644 index 0000000..ac050d4 --- /dev/null +++ b/Sto/STOBusinessLogic/BusinessLogics/CarLogic.cs @@ -0,0 +1,112 @@ +using Microsoft.Extensions.Logging; +using STOContracts.BindingModels; +using STOContracts.BusinessLogicsContracts; +using STOContracts.SearchModels; +using STOContracts.StoragesContracts; +using STOContracts.ViewModels; + +namespace STOBusinessLogic.BusinessLogics +{ + public class CarLogic : ICarLogic + { + private readonly ILogger _logger; + private readonly ICarStorage _carStorage; + public CarLogic(ILogger logger, ICarStorage carStorage) + { + _logger = logger; + _carStorage = carStorage; + } + public List? ReadList(CarSearchModel? model) + { + _logger.LogInformation("ReadList.CarId:{ CarId}", model?.Id); + var list = model == null ? _carStorage.GetFullList() : _carStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public CarViewModel? ReadElement(CarSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. CarId:{ CarId}", model?.Id); + var element = _carStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + + public bool Create(CarBindingModel model) + { + CheckModel(model); + if (_carStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(CarBindingModel model) + { + CheckModel(model); + if (_carStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(CarBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_carStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(CarBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Brand)) + { + throw new ArgumentNullException("Нет бренда автомобиля", nameof(model.Brand)); + } + if (string.IsNullOrEmpty(model.Model)) + { + throw new ArgumentNullException("Нет модели автомобиля", nameof(model.Model)); + } + if (string.IsNullOrEmpty(model.VIN)) + { + throw new ArgumentNullException("Нет VIN автомобиля", nameof(model.Model)); + } + var element = _carStorage.GetElement(new CarSearchModel + { + VIN = model.VIN + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Автомобиль с таким VIN уже есть"); + } + _logger.LogInformation("Car. Id:{ Id}.CarBrand:{CarBrand}.CarModel:{CarModel}.CarVIN:{CarVIN}", model?.Id, model?.Brand, model?.Brand, model?.VIN); + } + } +} diff --git a/Sto/STOBusinessLogic/BusinessLogics/EmployerLogic.cs b/Sto/STOBusinessLogic/BusinessLogics/EmployerLogic.cs new file mode 100644 index 0000000..fc158a2 --- /dev/null +++ b/Sto/STOBusinessLogic/BusinessLogics/EmployerLogic.cs @@ -0,0 +1,111 @@ +using Microsoft.Extensions.Logging; +using STOContracts.BindingModels; +using STOContracts.BusinessLogicsContracts; +using STOContracts.SearchModels; +using STOContracts.StoragesContracts; +using STOContracts.ViewModels; + +namespace STOBusinessLogic.BusinessLogics +{ + public class EmployerLogic : IEmployerLogic + { + private readonly ILogger _logger; + private readonly IEmployerStorage _employerStorage; + public EmployerLogic(ILogger logger, IEmployerStorage employerStorage) + { + _logger = logger; + _employerStorage = employerStorage; + } + public List? ReadList(EmployerSearchModel? model) + { + _logger.LogInformation("ReadList. EmployerId:{ EmployerId}", model?.Id); + var list = model == null ? _employerStorage.GetFullList() : _employerStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public EmployerViewModel? ReadElement(EmployerSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. EmployerId:{ EmployerId}", model?.Id); + var element = _employerStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + public bool Create(EmployerBindingModel model) + { + CheckModel(model); + if (_employerStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(EmployerBindingModel model) + { + CheckModel(model); + if (_employerStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(EmployerBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_employerStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(EmployerBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Login)) + { + throw new ArgumentNullException("Нет логина работника", nameof(model.Login)); + } + if (string.IsNullOrEmpty(model.Email)) + { + throw new ArgumentNullException("Нет почты работника", nameof(model.Email)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Нет пароля работника", nameof(model.Password)); + } + var element = _employerStorage.GetElement(new EmployerSearchModel + { + Login = model.Login + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("работник с таким логином уже есть"); + } + _logger.LogInformation("Employer. EmployerId:{ Id}.EmployerLogin:{EmployerLogin}.EmployerEmail:{EmployerEmail}", model?.Id, model?.Login, model?.Email); + } + } +} diff --git a/Sto/STOBusinessLogic/BusinessLogics/MaintenanceLogic.cs b/Sto/STOBusinessLogic/BusinessLogics/MaintenanceLogic.cs new file mode 100644 index 0000000..622d24d --- /dev/null +++ b/Sto/STOBusinessLogic/BusinessLogics/MaintenanceLogic.cs @@ -0,0 +1,99 @@ +using Microsoft.Extensions.Logging; +using STOContracts.BindingModels; +using STOContracts.BusinessLogicsContracts; +using STOContracts.SearchModels; +using STOContracts.StoragesContracts; +using STOContracts.ViewModels; + +namespace STOBusinessLogic.BusinessLogics +{ + public class MaintenanceLogic : IMaintenanceLogic + { + private readonly ILogger _logger; + private readonly IMaintenanceStorage _maintenanceStorage; + public MaintenanceLogic(ILogger logger, IMaintenanceStorage maintenanceStorage) + { + _logger = logger; + _maintenanceStorage = maintenanceStorage; + } + public List? ReadList(MaintenanceSearchModel? model) + { + _logger.LogInformation("ReadList. MaintenanceId:{ MaintenanceId}", model?.Id); + var list = model == null ? _maintenanceStorage.GetFullList() : _maintenanceStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public MaintenanceViewModel? ReadElement(MaintenanceSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. MaintenanceId:{ MaintenanceId}", model?.Id); + var element = _maintenanceStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + public bool Create(MaintenanceBindingModel model) + { + CheckModel(model); + if (_maintenanceStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(MaintenanceBindingModel model) + { + CheckModel(model); + if (_maintenanceStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(MaintenanceBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_maintenanceStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(MaintenanceBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.EmployerId<0) + { + throw new ArgumentNullException("Некорректный идентификатор работника", nameof(model.EmployerId)); + } + if (model.Cost<=0) + { + throw new ArgumentNullException("Стоимость должна быть больше нуля", nameof(model.Cost)); + } + _logger.LogInformation("Maintenance. MaintenanceId:{MaintenanceId}.EmployerId: { EmployerId}.Cost:{Cost}", model.Id, model.EmployerId, model.Cost); + } + } +} diff --git a/Sto/STOBusinessLogic/BusinessLogics/ReportLogic.cs b/Sto/STOBusinessLogic/BusinessLogics/ReportLogic.cs new file mode 100644 index 0000000..4e19c71 --- /dev/null +++ b/Sto/STOBusinessLogic/BusinessLogics/ReportLogic.cs @@ -0,0 +1,44 @@ +using STOContracts.BindingModels; +using STOContracts.BusinessLogicsContracts; +using STOContracts.SearchModels; +using STOContracts.StoragesContracts; +using STOContracts.ViewModels; +using System.Security.Cryptography.X509Certificates; + +namespace STOBusinessLogic.BusinessLogics +{ + public class ReportLogic : IReportLogic + { + private readonly IWorkStorage _workStorage; + private readonly ISpareStorage _spareStorage; + public ReportLogic(IWorkStorage work, ISpareStorage spareStorage) + { + _workStorage = work; + _spareStorage = spareStorage; + } + public List GetSpares(ReportBindingModel model) + { + return _workStorage.GetFilteredList(new WorkSearchModel { DateTo = model.DateTo, DateFrom = model.DateFrom }) + .Select(x => new ReportViewModel + { + Name = x.Title, + Spares = x.WorkSpares.Select(x => x.Value.Item1.Name).ToList(), + }).ToList(); + } + + public void SaveToPdfFile(ReportBindingModel model) + { + throw new NotImplementedException(); + } + + public void SaveToExcelFile(ReportBindingModel model) + { + throw new NotImplementedException(); + } + + public void SaveToWordFile(ReportBindingModel model) + { + throw new NotImplementedException(); + } + } +} diff --git a/Sto/STOBusinessLogic/BusinessLogics/ServiceLogic.cs b/Sto/STOBusinessLogic/BusinessLogics/ServiceLogic.cs new file mode 100644 index 0000000..099466e --- /dev/null +++ b/Sto/STOBusinessLogic/BusinessLogics/ServiceLogic.cs @@ -0,0 +1,91 @@ +using Microsoft.Extensions.Logging; +using STOContracts.BindingModels; +using STOContracts.BusinessLogicsContracts; +using STOContracts.SearchModels; +using STOContracts.StoragesContracts; +using STOContracts.ViewModels; + +namespace STOBusinessLogic.BusinessLogics +{ + public class ServiceLogic : IServiceLogic + { + private readonly ILogger _logger; + private readonly IServiceStorage _serviceStorage; + public ServiceLogic(ILogger logger, IServiceStorage serviceStorage) + { + _logger = logger; + _serviceStorage = serviceStorage; + } + public List? ReadList(ServiceSearchModel? model) + { + _logger.LogInformation("ReadList. ServiceId:{ ServiceId}", model?.Id); + var list = model == null ? _serviceStorage.GetFullList() : _serviceStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public ServiceViewModel? ReadElement(ServiceSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ServiceId:{ ServiceId}", model.Id); + var element = _serviceStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + public bool Create(ServiceBindingModel model) + { + CheckModel(model); + if (_serviceStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(ServiceBindingModel model) + { + CheckModel(model); + if (_serviceStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(ServiceBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_serviceStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(ServiceBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + _logger.LogInformation("Service. ServiceId: { ServiceId}.", model.Id); + } + } +} diff --git a/Sto/STOBusinessLogic/BusinessLogics/SpareLogic.cs b/Sto/STOBusinessLogic/BusinessLogics/SpareLogic.cs new file mode 100644 index 0000000..9f18aaf --- /dev/null +++ b/Sto/STOBusinessLogic/BusinessLogics/SpareLogic.cs @@ -0,0 +1,109 @@ +using Microsoft.Extensions.Logging; +using STOContracts.BindingModels; +using STOContracts.BusinessLogicsContracts; +using STOContracts.SearchModels; +using STOContracts.StoragesContracts; +using STOContracts.ViewModels; + +namespace STOBusinessLogic.BusinessLogics +{ + public class SpareLogic : ISpareLogic + { + private readonly ILogger _logger; + private readonly ISpareStorage _spareStorage; + public SpareLogic(ILogger logger, ISpareStorage spareStorage) + { + _logger = logger; + _spareStorage = spareStorage; + } + public List? ReadList(SpareSearchModel? model) + { + _logger.LogInformation("ReadList.SpareId:{Id}", model?.Id); + var list = model == null ? _spareStorage.GetFullList() : _spareStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public SpareViewModel? ReadElement(SpareSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. SpareId:{Id}", model?.Id); + var element = _spareStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + + public bool Create(SpareBindingModel model) + { + CheckModel(model); + if (_spareStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(SpareBindingModel model) + { + CheckModel(model); + if (_spareStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(SpareBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_spareStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(SpareBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Name)) + { + throw new ArgumentNullException("Нет название детали", nameof(model.Name)); + } + if (model.Price <= 0) + { + throw new ArgumentNullException("Неправильная цена детали", nameof(model.Price)); + } + + var element = _spareStorage.GetElement(new SpareSearchModel + { + Name = model.Name + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Деталь с таким названием уже есть"); + } + _logger.LogInformation("Spare. Id:{ Id}.Name:{Name}.Price:{Price}", model?.Id, model?.Name, model?.Price); + } + } +} diff --git a/Sto/STOBusinessLogic/BusinessLogics/StorekeeperLogic.cs b/Sto/STOBusinessLogic/BusinessLogics/StorekeeperLogic.cs new file mode 100644 index 0000000..2034b63 --- /dev/null +++ b/Sto/STOBusinessLogic/BusinessLogics/StorekeeperLogic.cs @@ -0,0 +1,113 @@ +using Microsoft.Extensions.Logging; +using STOContracts.BindingModels; +using STOContracts.BusinessLogicsContracts; +using STOContracts.SearchModels; +using STOContracts.StoragesContracts; +using STOContracts.ViewModels; + +namespace STOBusinessLogic.BusinessLogics +{ + public class StorekeeperLogic : IStorekeeperLogic + { + private readonly ILogger _logger; + private readonly IStorekeeperStorage _storekeeperStorage; + public StorekeeperLogic(ILogger logger, IStorekeeperStorage storekeeperStorage) + { + _logger = logger; + _storekeeperStorage = storekeeperStorage; + } + public List? ReadList(StorekeeperSearchModel? model) + { + _logger.LogInformation("ReadList.StorekeeperId:{Id}", model?.Id); + var list = model == null ? _storekeeperStorage.GetFullList() : _storekeeperStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public StorekeeperViewModel? ReadElement(StorekeeperSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. StorekeeperId:{Id}", model?.Id); + var element = _storekeeperStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + + public bool Create(StorekeeperBindingModel model) + { + CheckModel(model); + if (_storekeeperStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(StorekeeperBindingModel model) + { + CheckModel(model); + if (_storekeeperStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(StorekeeperBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_storekeeperStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(StorekeeperBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Login)) + { + throw new ArgumentNullException("Нет логина кладовщика", nameof(model.Login)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Нет пароля кладовщика", nameof(model.Password)); + } + if (string.IsNullOrEmpty(model.Email)) + { + throw new ArgumentNullException("Нет почты кладовщика", nameof(model.Email)); + } + var element = _storekeeperStorage.GetElement(new StorekeeperSearchModel + { + Login = model.Login, + Email = model.Email + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Кладовщик с такими параметрами уже есть"); + } + _logger.LogInformation("Storekeeper. Id:{ Id}.Login:{Login}.Email:{Email}.Password:{Password}", model?.Id, model?.Login, model?.Email, model?.Password); + } + } +} diff --git a/Sto/STOBusinessLogic/BusinessLogics/WorkDurationLogic.cs b/Sto/STOBusinessLogic/BusinessLogics/WorkDurationLogic.cs new file mode 100644 index 0000000..cea3bdf --- /dev/null +++ b/Sto/STOBusinessLogic/BusinessLogics/WorkDurationLogic.cs @@ -0,0 +1,96 @@ +using Microsoft.Extensions.Logging; +using STOContracts.BindingModels; +using STOContracts.BusinessLogicsContracts; +using STOContracts.SearchModels; +using STOContracts.StoragesContracts; +using STOContracts.ViewModels; + +namespace STOBusinessLogic.BusinessLogics +{ + public class WorkDurationLogic : IWorkDurationLogic + { + private readonly ILogger _logger; + private readonly IWorkDurationStorage _workDurationStorage; + public WorkDurationLogic(ILogger logger, IWorkDurationStorage workDurationStorage) + { + _logger = logger; + _workDurationStorage = workDurationStorage; + } + public List? ReadList(WorkDurationSearchModel? model) + { + _logger.LogInformation("ReadList.WorkDurationId:{Id}", model?.Id); + var list = model == null ? _workDurationStorage.GetFullList() : _workDurationStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public WorkDurationViewModel? ReadElement(WorkDurationSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. WorkDurationId:{Id}", model?.Id); + var element = _workDurationStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + + public bool Create(WorkDurationBindingModel model) + { + CheckModel(model); + if (_workDurationStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(WorkDurationBindingModel model) + { + CheckModel(model); + if (_workDurationStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(WorkDurationBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_workDurationStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(WorkDurationBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.Duration < 0) + { + throw new ArgumentNullException("Нет длительности", nameof(model.Duration)); + } + _logger.LogInformation("WorkDuration. Id:{ Id}.Duration:{Duration}", model?.Id, model?.Duration); + } + } +} diff --git a/Sto/STOBusinessLogic/BusinessLogics/WorkLogic.cs b/Sto/STOBusinessLogic/BusinessLogics/WorkLogic.cs new file mode 100644 index 0000000..a4a5418 --- /dev/null +++ b/Sto/STOBusinessLogic/BusinessLogics/WorkLogic.cs @@ -0,0 +1,108 @@ +using Microsoft.Extensions.Logging; +using STOContracts.BindingModels; +using STOContracts.BusinessLogicsContracts; +using STOContracts.SearchModels; +using STOContracts.StoragesContracts; +using STOContracts.ViewModels; + +namespace STOBusinessLogic.BusinessLogics +{ + public class WorkLogic : IWorkLogic + { + private readonly ILogger _logger; + private readonly IWorkStorage _workStorage; + public WorkLogic(ILogger logger, IWorkStorage workStorage) + { + _logger = logger; + _workStorage = workStorage; + } + public List? ReadList(WorkSearchModel? model) + { + _logger.LogInformation("ReadList.WorkId:{Id}", model?.Id); + var list = model == null ? _workStorage.GetFullList() : _workStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public WorkViewModel? ReadElement(WorkSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. WorkId:{ Id}", model?.Id); + var element = _workStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + + public bool Create(WorkBindingModel model) + { + CheckModel(model); + if (_workStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(WorkBindingModel model) + { + CheckModel(model); + if (_workStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(WorkBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_workStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(WorkBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Title)) + { + throw new ArgumentNullException("Нет названия работы", nameof(model.Title)); + } + if (model.Price < 0) + { + throw new ArgumentNullException("Нет цены работы", nameof(model.Price)); + } + var element = _workStorage.GetElement(new WorkSearchModel + { + Title = model.Title, + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Работа с таким именем уже есть"); + } + _logger.LogInformation("Work. Id:{ Id}.Title:{Title}.Price:{Price}", model?.Id, model?.Title, model?.Price); + } + } +} diff --git a/Sto/STOBusinessLogic/STOBusinessLogic.csproj b/Sto/STOBusinessLogic/STOBusinessLogic.csproj new file mode 100644 index 0000000..7129a87 --- /dev/null +++ b/Sto/STOBusinessLogic/STOBusinessLogic.csproj @@ -0,0 +1,26 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + + + + + + + + +