From a864d1108d9f73a7ea8e3e27fe4a9d57e55999fc Mon Sep 17 00:00:00 2001 From: russell Date: Mon, 29 Apr 2024 19:16:39 +0400 Subject: [PATCH] BusinessLogics --- TravelAgency/TravelAgency.sln | 8 +- .../BusinessLogics/ExcursionGroupLogic.cs | 119 +++++++++++++++++ .../BusinessLogics/ExcursionLogic.cs | 119 +++++++++++++++++ .../BusinessLogics/GuideLogic.cs | 120 +++++++++++++++++ .../BusinessLogics/PlaceLogic.cs | 119 +++++++++++++++++ .../BusinessLogics/TourLogic.cs | 123 +++++++++++++++++ .../BusinessLogics/TripLogic.cs | 119 +++++++++++++++++ .../BusinessLogics/UserLogic.cs | 125 ++++++++++++++++++ .../TravelAgencyBusinessLogic.csproj | 17 +++ .../Implements/ExcursionGroupStorage.cs | 34 ++++- .../Implements/ExcursionStorage.cs | 21 ++- .../Implements/GuideStorage.cs | 30 +++-- .../Implements/PlaceStorage.cs | 43 ++++-- .../Implements/TourStorage.cs | 18 ++- .../Implements/TripStorage.cs | 21 ++- .../Implements/UserStorage.cs | 36 +++-- 16 files changed, 1021 insertions(+), 51 deletions(-) create mode 100644 TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/ExcursionGroupLogic.cs create mode 100644 TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/ExcursionLogic.cs create mode 100644 TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/GuideLogic.cs create mode 100644 TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/PlaceLogic.cs create mode 100644 TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/TourLogic.cs create mode 100644 TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/TripLogic.cs create mode 100644 TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/UserLogic.cs create mode 100644 TravelAgency/TravelAgencyBusinessLogic/TravelAgencyBusinessLogic.csproj diff --git a/TravelAgency/TravelAgency.sln b/TravelAgency/TravelAgency.sln index e0b7e33..f46f2dd 100644 --- a/TravelAgency/TravelAgency.sln +++ b/TravelAgency/TravelAgency.sln @@ -7,7 +7,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TravelAgencyDataModels", "T EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TravelAgencyContracts", "TravelAgencyContracts\TravelAgencyContracts.csproj", "{5A5C7696-4047-4BB8-BEC3-9DDB4000394E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TravelAgencyDatabaseImplement", "TravelAgencyDatabaseImplement\TravelAgencyDatabaseImplement.csproj", "{7F470B39-8E7F-4F32-A79F-CB77FDAEE513}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TravelAgencyDatabaseImplement", "TravelAgencyDatabaseImplement\TravelAgencyDatabaseImplement.csproj", "{7F470B39-8E7F-4F32-A79F-CB77FDAEE513}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TravelAgencyBusinessLogic", "TravelAgencyBusinessLogic\TravelAgencyBusinessLogic.csproj", "{AA81731F-6DB3-4BA0-98AF-E319788D7388}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -27,6 +29,10 @@ Global {7F470B39-8E7F-4F32-A79F-CB77FDAEE513}.Debug|Any CPU.Build.0 = Debug|Any CPU {7F470B39-8E7F-4F32-A79F-CB77FDAEE513}.Release|Any CPU.ActiveCfg = Release|Any CPU {7F470B39-8E7F-4F32-A79F-CB77FDAEE513}.Release|Any CPU.Build.0 = Release|Any CPU + {AA81731F-6DB3-4BA0-98AF-E319788D7388}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AA81731F-6DB3-4BA0-98AF-E319788D7388}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AA81731F-6DB3-4BA0-98AF-E319788D7388}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AA81731F-6DB3-4BA0-98AF-E319788D7388}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/ExcursionGroupLogic.cs b/TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/ExcursionGroupLogic.cs new file mode 100644 index 0000000..5761f1f --- /dev/null +++ b/TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/ExcursionGroupLogic.cs @@ -0,0 +1,119 @@ +using Microsoft.Extensions.Logging; +using TravelAgencyContracts.BindingModels; +using TravelAgencyContracts.BusinessLogicsContracts; +using TravelAgencyContracts.SearchModels; +using TravelAgencyContracts.StoragesContracts; +using TravelAgencyContracts.ViewModels; + +namespace TravelAgencyBusinessLogic.BusinessLogics +{ + public class ExcursionGroupLogic : IExcursionGroupLogic + { + private readonly ILogger _logger; + + private readonly IExcursionGroupStorage _excursionGroupStorage; + + public ExcursionGroupLogic(ILogger logger, IExcursionGroupStorage excursionGroupStorage) + { + _logger = logger; + _excursionGroupStorage = excursionGroupStorage; + } + + public List? ReadList(ExcursionGroupSearchModel? model) + { + _logger.LogInformation("ReadList. Id: {Id}, ExcursionGroupName: {ExcursionGroupName}, UserId: {UserId}, GuideId: {GuideId}.", model?.Id, model?.ExcursionGroupName, model?.UserId, model?.GuideId); + var list = (model == null) ? _excursionGroupStorage.GetFullList() : _excursionGroupStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count: {Count}", list.Count); + return list; + } + + public ExcursionGroupViewModel? ReadElement(ExcursionGroupSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ExcursionGroup id: {Id}, ExcursionGroup name: {ExcursionGroupName}", model?.Id, model?.ExcursionGroupName); + var element = _excursionGroupStorage.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(ExcursionGroupBindingModel model) + { + CheckModel(model); + if (_excursionGroupStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(ExcursionGroupBindingModel model) + { + CheckModel(model); + if (_excursionGroupStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(ExcursionGroupBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id: {Id}", model.Id); + if (_excursionGroupStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(ExcursionGroupBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ExcursionGroupName)) + { + throw new ArgumentNullException("Нет названия группы", nameof(model.ExcursionGroupName)); + } + if (model.ParticipantsAmount <= 0) + { + throw new ArgumentNullException("Количество участников быть больше 0", nameof(model.ParticipantsAmount)); + } + if (model.UserId <= 0) + { + throw new ArgumentNullException("Некорректный идентификатор пользователя", nameof(model.UserId)); + } + _logger.LogInformation("ExcursionGroup. Id: {id}, ExcursionGroupName: {ExcursionGroupName}, ParticipantsAmount: {ParticipantsAmount}, UserId: {UserId}", model.Id, model.ExcursionGroupName, model.ParticipantsAmount, model.UserId); + var element = _excursionGroupStorage.GetElement(new ExcursionGroupSearchModel + { + ExcursionGroupName = model.ExcursionGroupName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Группа с таким названием уже есть"); + } + } + } +} diff --git a/TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/ExcursionLogic.cs b/TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/ExcursionLogic.cs new file mode 100644 index 0000000..5d94297 --- /dev/null +++ b/TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/ExcursionLogic.cs @@ -0,0 +1,119 @@ +using Microsoft.Extensions.Logging; +using TravelAgencyContracts.BindingModels; +using TravelAgencyContracts.BusinessLogicsContracts; +using TravelAgencyContracts.SearchModels; +using TravelAgencyContracts.StoragesContracts; +using TravelAgencyContracts.ViewModels; + +namespace TravelAgencyBusinessLogic.BusinessLogics +{ + public class ExcursionLogic : IExcursionLogic + { + private readonly ILogger _logger; + + private readonly IExcursionStorage _excursionStorage; + + public ExcursionLogic(ILogger logger, IExcursionStorage excursionStorage) + { + _logger = logger; + _excursionStorage = excursionStorage; + } + + public List? ReadList(ExcursionSearchModel? model) + { + _logger.LogInformation("ReadList. Id: {Id}, ExcursionName: {ExcursionName}, UserId: {UserId}.", model?.Id, model?.ExcursionName, model?.UserId); + var list = (model == null) ? _excursionStorage.GetFullList() : _excursionStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count: {Count}", list.Count); + return list; + } + + public ExcursionViewModel? ReadElement(ExcursionSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Excursion id: {Id}, Excursion name: {ExcursionName}", model?.Id, model?.ExcursionName); + var element = _excursionStorage.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(ExcursionBindingModel model) + { + CheckModel(model); + if (_excursionStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(ExcursionBindingModel model) + { + CheckModel(model); + if (_excursionStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(ExcursionBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id: {Id}", model.Id); + if (_excursionStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(ExcursionBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ExcursionName)) + { + throw new ArgumentNullException("Нет названия экскурсии", nameof(model.ExcursionName)); + } + if (model.Price <= 0) + { + throw new ArgumentNullException("Цена экскурсии должна быть больше 0", nameof(model.Price)); + } + if (model.UserId <= 0) + { + throw new ArgumentNullException("Некорректный идентификатор пользователя", nameof(model.UserId)); + } + _logger.LogInformation("Excursion. Id: {id}, ExcursionName: {ExcursionName}, Price: {Price}, UserId: {UserId}", model.Id, model.ExcursionName, model.Price, model.UserId); + var element = _excursionStorage.GetElement(new ExcursionSearchModel + { + ExcursionName = model.ExcursionName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Экскурсия с таким названием уже есть"); + } + } + } +} diff --git a/TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/GuideLogic.cs b/TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/GuideLogic.cs new file mode 100644 index 0000000..b84c5b4 --- /dev/null +++ b/TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/GuideLogic.cs @@ -0,0 +1,120 @@ +using Microsoft.Extensions.Logging; +using TravelAgencyContracts.BindingModels; +using TravelAgencyContracts.BusinessLogicsContracts; +using TravelAgencyContracts.SearchModels; +using TravelAgencyContracts.StoragesContracts; +using TravelAgencyContracts.ViewModels; + +namespace TravelAgencyBusinessLogic.BusinessLogics +{ + public class GuideLogic : IGuideLogic + { + private readonly ILogger _logger; + + private readonly IGuideStorage _guideStorage; + + public GuideLogic(ILogger logger, IGuideStorage guideStorage) + { + _logger = logger; + _guideStorage = guideStorage; + } + + public List? ReadList(GuideSearchModel? model) + { + _logger.LogInformation("ReadList. GuideFIO: {GuideFIO}. Id: {Id} ", model?.GuideFIO, model?.Id); + var list = (model == null) ? _guideStorage.GetFullList() : _guideStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count: {Count}", list.Count); + return list; + } + + public GuideViewModel? ReadElement(GuideSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Guide id: {Id}, Guide email: {Email}, Guide phone number: {PhoneNumber}", model?.Id, model?.Email, model?.PhoneNumber); + var element = _guideStorage.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(GuideBindingModel model) + { + CheckModel(model); + if (_guideStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(GuideBindingModel model) + { + CheckModel(model); + if (_guideStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(GuideBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id: {Id}", model.Id); + if (_guideStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(GuideBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.GuideFIO)) + { + throw new ArgumentNullException("Нет ФИО гида", nameof(model.GuideFIO)); + } + if (string.IsNullOrEmpty(model.Email)) + { + throw new ArgumentNullException("Нет почты гида", nameof(model.Email)); + } + if (string.IsNullOrEmpty(model.PhoneNumber)) + { + throw new ArgumentNullException("Нет номера телефона гида", nameof(model.Email)); + } + _logger.LogInformation("Guide. Id: {id}, FIO: {fio}, email: {email}", model.Id, model.GuideFIO, model.Email); + var element = _guideStorage.GetElement(new GuideSearchModel + { + Email = model.Email, + PhoneNumber = model.PhoneNumber, + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Гид с такой почтой или номером телефона уже есть"); + } + } + } +} diff --git a/TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/PlaceLogic.cs b/TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/PlaceLogic.cs new file mode 100644 index 0000000..c1d42d7 --- /dev/null +++ b/TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/PlaceLogic.cs @@ -0,0 +1,119 @@ +using Microsoft.Extensions.Logging; +using TravelAgencyContracts.BindingModels; +using TravelAgencyContracts.BusinessLogicsContracts; +using TravelAgencyContracts.SearchModels; +using TravelAgencyContracts.StoragesContracts; +using TravelAgencyContracts.ViewModels; + +namespace TravelAgencyBusinessLogic.BusinessLogics +{ + public class PlaceLogic : IPlaceLogic + { + private readonly ILogger _logger; + + private readonly IPlaceStorage _placeStorage; + + public PlaceLogic(ILogger logger, IPlaceStorage placeStorage) + { + _logger = logger; + _placeStorage = placeStorage; + } + + public List? ReadList(PlaceSearchModel? model) + { + _logger.LogInformation("ReadList. Id: {Id}, PlaceName: {PlaceName}, ExcursionId: {ExcursionId}.", model?.Id, model?.PlaceName, model?.ExcursionId); + var list = (model == null) ? _placeStorage.GetFullList() : _placeStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count: {Count}", list.Count); + return list; + } + + public PlaceViewModel? ReadElement(PlaceSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Place id: {Id}, Place name: {PlaceName}, Place address: {PlaceAddress}", model?.Id, model?.PlaceName, model?.PlaceAddress); + var element = _placeStorage.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(PlaceBindingModel model) + { + CheckModel(model); + if (_placeStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(PlaceBindingModel model) + { + CheckModel(model); + if (_placeStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(PlaceBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id: {Id}", model.Id); + if (_placeStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(PlaceBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.PlaceName)) + { + throw new ArgumentNullException("Нет названия места", nameof(model.PlaceName)); + } + if (string.IsNullOrEmpty(model.PlaceAddress)) + { + throw new ArgumentNullException("Нет адреса места", nameof(model.PlaceAddress)); + } + if (model.ExcursionId <= 0) + { + throw new ArgumentNullException("Некорректный идентификатор экскурсии", nameof(model.ExcursionId)); + } + _logger.LogInformation("Place. Id: {id}, PlaceName: {PlaceName}, PlaceAddress: {PlaceAddress}, ExcursionId: {ExcursionId}", model.Id, model.PlaceName, model.PlaceAddress, model.ExcursionId); + var element = _placeStorage.GetElement(new PlaceSearchModel + { + PlaceName = model.PlaceName, + PlaceAddress = model.PlaceAddress + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Место с таким названием или адресом уже есть"); + } + } +} diff --git a/TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/TourLogic.cs b/TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/TourLogic.cs new file mode 100644 index 0000000..2567fed --- /dev/null +++ b/TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/TourLogic.cs @@ -0,0 +1,123 @@ +using Microsoft.Extensions.Logging; +using TravelAgencyContracts.BindingModels; +using TravelAgencyContracts.BusinessLogicsContracts; +using TravelAgencyContracts.SearchModels; +using TravelAgencyContracts.StoragesContracts; +using TravelAgencyContracts.ViewModels; + +namespace TravelAgencyBusinessLogic.BusinessLogics +{ + public class TourLogic : ITourLogic + { + private readonly ILogger _logger; + + private readonly ITourStorage _tourStorage; + + public TourLogic(ILogger logger, ITourStorage tourStorage) + { + _logger = logger; + _tourStorage = tourStorage; + } + + public List? ReadList(TourSearchModel? model) + { + _logger.LogInformation("ReadList. Id: {Id}, TourName: {TourName}, UserId: {UserId}.", model?.Id, model?.TourName, model?.UserId); + var list = (model == null) ? _tourStorage.GetFullList() : _tourStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count: {Count}", list.Count); + return list; + } + + public TourViewModel? ReadElement(TourSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Tour id: {Id}, Tour name: {TourName}", model?.Id, model?.TourName); + var element = _tourStorage.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(TourBindingModel model) + { + CheckModel(model); + if (_tourStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(TourBindingModel model) + { + CheckModel(model); + if (_tourStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(TourBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id: {Id}", model.Id); + if (_tourStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(TourBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.TourName)) + { + throw new ArgumentNullException("Нет названия тура", nameof(model.TourName)); + } + if (model.TourDate == DateTime.MinValue) + { + throw new ArgumentNullException("Нет даты тура", nameof(model.TourDate)); + } + if (model.Price <= 0) + { + throw new ArgumentNullException("Цена тура должна быть больше 0", nameof(model.Price)); + } + if (model.UserId <= 0) + { + throw new ArgumentNullException("Некорректный идентификатор пользователя", nameof(model.UserId)); + } + _logger.LogInformation("Tour. Id: {id}, TourName: {TourName}, Price: {Price}, UserId: {UserId}", model.Id, model.TourName, model.Price, model.UserId); + var element = _tourStorage.GetElement(new TourSearchModel + { + TourName = model.TourName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Тур с таким названием уже есть"); + } + } + } +} diff --git a/TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/TripLogic.cs b/TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/TripLogic.cs new file mode 100644 index 0000000..9d6860e --- /dev/null +++ b/TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/TripLogic.cs @@ -0,0 +1,119 @@ +using Microsoft.Extensions.Logging; +using TravelAgencyContracts.BindingModels; +using TravelAgencyContracts.BusinessLogicsContracts; +using TravelAgencyContracts.SearchModels; +using TravelAgencyContracts.StoragesContracts; +using TravelAgencyContracts.ViewModels; + +namespace TravelAgencyBusinessLogic.BusinessLogics +{ + public class TripLogic : ITripLogic + { + private readonly ILogger _logger; + + private readonly ITripStorage _tripStorage; + + public TripLogic(ILogger logger, ITripStorage tripStorage) + { + _logger = logger; + _tripStorage = tripStorage; + } + + public List? ReadList(TripSearchModel? model) + { + _logger.LogInformation("ReadList. Id: {Id}, TripName: {TripName}, GuideId: {GuideId}.", model?.Id, model?.TripName, model?.GuideId); + var list = (model == null) ? _tripStorage.GetFullList() : _tripStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count: {Count}", list.Count); + return list; + } + + public TripViewModel? ReadElement(TripSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Trip id: {Id}, Trip name: {TripName}", model?.Id, model?.TripName); + var element = _tripStorage.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(TripBindingModel model) + { + CheckModel(model); + if (_tripStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(TripBindingModel model) + { + CheckModel(model); + if (_tripStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(TripBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id: {Id}", model.Id); + if (_tripStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(TripBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.TripName)) + { + throw new ArgumentNullException("Нет названия поездки", nameof(model.TripName)); + } + if (model.TripDate == DateTime.MinValue) + { + throw new ArgumentNullException("Нет даты поездки", nameof(model.TripDate)); + } + if (model.GuideId <= 0) + { + throw new ArgumentNullException("Некорректный идентификатор гида", nameof(model.GuideId)); + } + _logger.LogInformation("Trip. Id: {id}, TripName: {TripName}, GuideId: {GuideId}", model.Id, model.TripName, model.GuideId); + var element = _tripStorage.GetElement(new TripSearchModel + { + TripName = model.TripName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Поездка с таким названием уже есть"); + } + } + } +} diff --git a/TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/UserLogic.cs b/TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/UserLogic.cs new file mode 100644 index 0000000..cfdf395 --- /dev/null +++ b/TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/UserLogic.cs @@ -0,0 +1,125 @@ +using Microsoft.Extensions.Logging; +using TravelAgencyContracts.BindingModels; +using TravelAgencyContracts.BusinessLogicsContracts; +using TravelAgencyContracts.SearchModels; +using TravelAgencyContracts.StoragesContracts; +using TravelAgencyContracts.ViewModels; + +namespace TravelAgencyBusinessLogic.BusinessLogics +{ + public class UserLogic : IUserLogic + { + private readonly ILogger _logger; + + private readonly IUserStorage _userStorage; + + public UserLogic(ILogger logger, IUserStorage userStorage) + { + _logger = logger; + _userStorage = userStorage; + } + + public List? ReadList(UserSearchModel? model) + { + _logger.LogInformation("ReadList. UserFIO: {UserFIO}. Id: {Id} ", model?.UserFIO, model?.Id); + var list = (model == null) ? _userStorage.GetFullList() : _userStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count: {Count}", list.Count); + return list; + } + + public UserViewModel? ReadElement(UserSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. User id: {Id}, User email: {Email}, User phone number: {PhoneNumber}, User password: {Password}", model?.Id, model?.Email, model?.PhoneNumber, model?.Password); + var element = _userStorage.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(UserBindingModel model) + { + CheckModel(model); + if (_userStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(UserBindingModel model) + { + CheckModel(model); + if (_userStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(UserBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id: {Id}", model.Id); + if (_userStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(UserBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.UserFIO)) + { + throw new ArgumentNullException("Нет ФИО пользователя", nameof(model.UserFIO)); + } + if (string.IsNullOrEmpty(model.Email)) + { + throw new ArgumentNullException("Нет логина(почты) пользователя", nameof(model.Email)); + } + if (string.IsNullOrEmpty(model.PhoneNumber)) + { + throw new ArgumentNullException("Нет номера телефона пользователя", nameof(model.Email)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Нет пароля пользователя", nameof(model.Password)); + } + _logger.LogInformation("User. Id: {id}, FIO: {fio}, email: {email}, phone number: {PhoneNumber}, password: {password}", model.Id, model.UserFIO, model.Email, model.PhoneNumber, + model.Password); + var element = _userStorage.GetElement(new UserSearchModel + { + Email = model.Email, + PhoneNumber = model.PhoneNumber, + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Пользователь с таким логином(почтой) или номером телефона уже есть"); + } + } + } +} diff --git a/TravelAgency/TravelAgencyBusinessLogic/TravelAgencyBusinessLogic.csproj b/TravelAgency/TravelAgencyBusinessLogic/TravelAgencyBusinessLogic.csproj new file mode 100644 index 0000000..bc89be4 --- /dev/null +++ b/TravelAgency/TravelAgencyBusinessLogic/TravelAgencyBusinessLogic.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Implements/ExcursionGroupStorage.cs b/TravelAgency/TravelAgencyDatabaseImplement/Implements/ExcursionGroupStorage.cs index 62f63b3..1eccb28 100644 --- a/TravelAgency/TravelAgencyDatabaseImplement/Implements/ExcursionGroupStorage.cs +++ b/TravelAgency/TravelAgencyDatabaseImplement/Implements/ExcursionGroupStorage.cs @@ -24,12 +24,10 @@ namespace TravelAgencyDatabaseImplement.Implements public List GetFilteredList(ExcursionGroupSearchModel model) { - if (string.IsNullOrEmpty(model.ExcursionGroupName)) - { - return new(); - } using var context = new TravelAgencyDatabase(); - return context.ExcursionGroups + if (!string.IsNullOrEmpty(model.ExcursionGroupName)) + { + return context.ExcursionGroups .Include(x => x.User) .Include(x => x.Guide) .Include(x => x.Tours) @@ -38,6 +36,32 @@ namespace TravelAgencyDatabaseImplement.Implements .ToList() .Select(x => x.GetViewModel) .ToList(); + } + if (model.UserId.HasValue) + { + return context.ExcursionGroups + .Include(x => x.User) + .Include(x => x.Guide) + .Include(x => x.Tours) + .ThenInclude(x => x.Tour) + .Where(x => x.UserId.Equals(model.UserId)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + if (model.GuideId.HasValue) + { + return context.ExcursionGroups + .Include(x => x.User) + .Include(x => x.Guide) + .Include(x => x.Tours) + .ThenInclude(x => x.Tour) + .Where(x => x.GuideId.Equals(model.GuideId)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + return new(); } public ExcursionGroupViewModel? GetElement(ExcursionGroupSearchModel model) diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Implements/ExcursionStorage.cs b/TravelAgency/TravelAgencyDatabaseImplement/Implements/ExcursionStorage.cs index d219637..f8a8f0b 100644 --- a/TravelAgency/TravelAgencyDatabaseImplement/Implements/ExcursionStorage.cs +++ b/TravelAgency/TravelAgencyDatabaseImplement/Implements/ExcursionStorage.cs @@ -23,12 +23,10 @@ namespace TravelAgencyDatabaseImplement.Implements public List GetFilteredList(ExcursionSearchModel model) { - if (string.IsNullOrEmpty(model.ExcursionName)) - { - return new(); - } using var context = new TravelAgencyDatabase(); - return context.Excursions + if (!string.IsNullOrEmpty(model.ExcursionName)) + { + return context.Excursions .Include(x => x.User) .Include(x => x.Tours) .ThenInclude(x => x.Tour) @@ -36,6 +34,19 @@ namespace TravelAgencyDatabaseImplement.Implements .ToList() .Select(x => x.GetViewModel) .ToList(); + } + if (model.UserId.HasValue) + { + return context.Excursions + .Include(x => x.User) + .Include(x => x.Tours) + .ThenInclude(x => x.Tour) + .Where(x => x.UserId.Equals(model.UserId)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + return new(); } public ExcursionViewModel? GetElement(ExcursionSearchModel model) diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Implements/GuideStorage.cs b/TravelAgency/TravelAgencyDatabaseImplement/Implements/GuideStorage.cs index 4d37a00..1ab2bc4 100644 --- a/TravelAgency/TravelAgencyDatabaseImplement/Implements/GuideStorage.cs +++ b/TravelAgency/TravelAgencyDatabaseImplement/Implements/GuideStorage.cs @@ -18,27 +18,39 @@ namespace TravelAgencyDatabaseImplement.Implements public List GetFilteredList(GuideSearchModel model) { - if (string.IsNullOrEmpty(model.Email)) + if (string.IsNullOrEmpty(model.GuideFIO)) { return new(); } using var context = new TravelAgencyDatabase(); return context.Guides - .Where(x => x.Email.Contains(model.Email)) + .Where(x => x.GuideFIO.Contains(model.GuideFIO)) .Select(x => x.GetViewModel) .ToList(); } public GuideViewModel? GetElement(GuideSearchModel model) { - if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue) - { - return null; - } using var context = new TravelAgencyDatabase(); - return context.Guides.FirstOrDefault(x => - (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email) - || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + if (model.Id.HasValue) + { + return context.Guides + .FirstOrDefault(x => x.Id.Equals(model.Id)) + ?.GetViewModel; + } + if (!string.IsNullOrEmpty(model.Email)) + { + return context.Guides + .FirstOrDefault(x => x.Email.Equals(model.Email)) + ?.GetViewModel; + } + if (!string.IsNullOrEmpty(model.PhoneNumber)) + { + return context.Guides + .FirstOrDefault(x => x.PhoneNumber.Equals(model.PhoneNumber)) + ?.GetViewModel; + } + return null; } public GuideViewModel? Insert(GuideBindingModel model) diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Implements/PlaceStorage.cs b/TravelAgency/TravelAgencyDatabaseImplement/Implements/PlaceStorage.cs index 62d43e3..b5d3d34 100644 --- a/TravelAgency/TravelAgencyDatabaseImplement/Implements/PlaceStorage.cs +++ b/TravelAgency/TravelAgencyDatabaseImplement/Implements/PlaceStorage.cs @@ -20,29 +20,48 @@ namespace TravelAgencyDatabaseImplement.Implements public List GetFilteredList(PlaceSearchModel model) { - if (string.IsNullOrEmpty(model.PlaceName)) - { - return new(); - } using var context = new TravelAgencyDatabase(); - return context.Places + if (!string.IsNullOrEmpty(model.PlaceName)) + { + return context.Places .Include(x => x.Excursion) .Where(x => x.PlaceName.Contains(model.PlaceName)) .Select(x => x.GetViewModel) .ToList(); + } + if (model.ExcursionId.HasValue) + { + return context.Places + .Include(x => x.Excursion) + .Where(x => x.ExcursionId.Equals(model.ExcursionId)) + .Select(x => x.GetViewModel) + .ToList(); + } + return new(); } public PlaceViewModel? GetElement(PlaceSearchModel model) { - if (string.IsNullOrEmpty(model.PlaceName) && !model.Id.HasValue) - { - return null; - } using var context = new TravelAgencyDatabase(); - return context.Places.Include(x => x.Excursion) - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.PlaceName) && x.PlaceName == model.PlaceName) || - (model.Id.HasValue && x.Id == model.Id)) + if (model.Id.HasValue) + { + return context.Places + .FirstOrDefault(x => x.Id.Equals(model.Id)) ?.GetViewModel; + } + if (!string.IsNullOrEmpty(model.PlaceName)) + { + return context.Places + .FirstOrDefault(x => x.PlaceName.Equals(model.PlaceName)) + ?.GetViewModel; + } + if (!string.IsNullOrEmpty(model.PlaceAddress)) + { + return context.Places + .FirstOrDefault(x => x.PlaceAddress.Equals(model.PlaceAddress)) + ?.GetViewModel; + } + return null; } public PlaceViewModel? Insert(PlaceBindingModel model) diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Implements/TourStorage.cs b/TravelAgency/TravelAgencyDatabaseImplement/Implements/TourStorage.cs index e690676..7b9a99d 100644 --- a/TravelAgency/TravelAgencyDatabaseImplement/Implements/TourStorage.cs +++ b/TravelAgency/TravelAgencyDatabaseImplement/Implements/TourStorage.cs @@ -20,16 +20,24 @@ namespace TravelAgencyDatabaseImplement.Implements public List GetFilteredList(TourSearchModel model) { - if (string.IsNullOrEmpty(model.TourName)) - { - return new(); - } using var context = new TravelAgencyDatabase(); - return context.Tours + if (!string.IsNullOrEmpty(model.TourName)) + { + return context.Tours .Include(x => x.User) .Where(x => x.TourName.Contains(model.TourName)) .Select(x => x.GetViewModel) .ToList(); + } + if (model.UserId.HasValue) + { + return context.Tours + .Include(x => x.User) + .Where(x => x.UserId.Equals(model.UserId)) + .Select(x => x.GetViewModel) + .ToList(); + } + return new(); } public TourViewModel? GetElement(TourSearchModel model) diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Implements/TripStorage.cs b/TravelAgency/TravelAgencyDatabaseImplement/Implements/TripStorage.cs index 7b55280..9a354a6 100644 --- a/TravelAgency/TravelAgencyDatabaseImplement/Implements/TripStorage.cs +++ b/TravelAgency/TravelAgencyDatabaseImplement/Implements/TripStorage.cs @@ -23,12 +23,10 @@ namespace TravelAgencyDatabaseImplement.Implements public List GetFilteredList(TripSearchModel model) { - if (string.IsNullOrEmpty(model.TripName)) - { - return new(); - } using var context = new TravelAgencyDatabase(); - return context.Trips + if (!string.IsNullOrEmpty(model.TripName)) + { + return context.Trips .Include(x => x.Guide) .Include(x => x.Places) .ThenInclude(x => x.Place) @@ -36,6 +34,19 @@ namespace TravelAgencyDatabaseImplement.Implements .ToList() .Select(x => x.GetViewModel) .ToList(); + } + if (model.GuideId.HasValue) + { + return context.Trips + .Include(x => x.Guide) + .Include(x => x.Places) + .ThenInclude(x => x.Place) + .Where(x => x.GuideId.Equals(model.GuideId)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + return new(); } public TripViewModel? GetElement(TripSearchModel model) diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Implements/UserStorage.cs b/TravelAgency/TravelAgencyDatabaseImplement/Implements/UserStorage.cs index bbdff45..3129fc9 100644 --- a/TravelAgency/TravelAgencyDatabaseImplement/Implements/UserStorage.cs +++ b/TravelAgency/TravelAgencyDatabaseImplement/Implements/UserStorage.cs @@ -18,27 +18,45 @@ namespace TravelAgencyDatabaseImplement.Implements public List GetFilteredList(UserSearchModel model) { - if (string.IsNullOrEmpty(model.Email)) + if (string.IsNullOrEmpty(model.UserFIO)) { return new(); } using var context = new TravelAgencyDatabase(); return context.Users - .Where(x => x.Email.Contains(model.Email)) + .Where(x => x.UserFIO.Contains(model.UserFIO)) .Select(x => x.GetViewModel) .ToList(); } public UserViewModel? GetElement(UserSearchModel model) { - if (string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password) && !model.Id.HasValue) - { - return null; - } using var context = new TravelAgencyDatabase(); - return context.Users.FirstOrDefault(x => - (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email && !string.IsNullOrEmpty(model.Password) && x.Password == model.Password) - || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + if (model.Id.HasValue) + { + return context.Users + .FirstOrDefault(x => x.Id.Equals(model.Id)) + ?.GetViewModel; + } + if (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password)) + { + return context.Users + .FirstOrDefault(x => x.Email.Equals(model.Email) && x.Password.Equals(model.Password)) + ?.GetViewModel; + } + if (!string.IsNullOrEmpty(model.Email)) + { + return context.Users + .FirstOrDefault(x => x.Email.Equals(model.Email)) + ?.GetViewModel; + } + if (!string.IsNullOrEmpty(model.PhoneNumber)) + { + return context.Users + .FirstOrDefault(x => x.PhoneNumber.Equals(model.PhoneNumber)) + ?.GetViewModel; + } + return null; } public UserViewModel? Insert(UserBindingModel model)