From 767ea158e8e9a8cbf179fad2f531f00519dba3a2 Mon Sep 17 00:00:00 2001 From: Viltskaa Date: Fri, 7 Apr 2023 00:11:08 +0400 Subject: [PATCH] Add logic --- .../CleaningInstrumentsLogic.cs | 78 ++++++++++++++++ .../BusinessLogics/CleaningLogic.cs | 77 ++++++++++++++++ .../BusinessLogics/GuestLogic.cs | 81 +++++++++++++++++ .../BusinessLogics/MaitreLogic.cs | 88 +++++++++++++++++++ .../BusinessLogics/ReservationLogic.cs | 76 ++++++++++++++++ .../BusinessLogics/RoomLogic.cs | 80 +++++++++++++++++ .../HotelBusinessLogic.csproj | 8 ++ Hotel/HotelView/HotelView.csproj | 4 + 8 files changed, 492 insertions(+) create mode 100644 Hotel/HotelBusinessLogic/BusinessLogics/CleaningInstrumentsLogic.cs create mode 100644 Hotel/HotelBusinessLogic/BusinessLogics/CleaningLogic.cs create mode 100644 Hotel/HotelBusinessLogic/BusinessLogics/GuestLogic.cs create mode 100644 Hotel/HotelBusinessLogic/BusinessLogics/MaitreLogic.cs create mode 100644 Hotel/HotelBusinessLogic/BusinessLogics/ReservationLogic.cs create mode 100644 Hotel/HotelBusinessLogic/BusinessLogics/RoomLogic.cs diff --git a/Hotel/HotelBusinessLogic/BusinessLogics/CleaningInstrumentsLogic.cs b/Hotel/HotelBusinessLogic/BusinessLogics/CleaningInstrumentsLogic.cs new file mode 100644 index 0000000..c1c73c5 --- /dev/null +++ b/Hotel/HotelBusinessLogic/BusinessLogics/CleaningInstrumentsLogic.cs @@ -0,0 +1,78 @@ +using HotelContracts.BindingModels; +using HotelContracts.BusinessLogicsContracts; +using HotelContracts.SearchModels; +using HotelContracts.StoragesContracts; +using HotelContracts.ViewModels; +using HotelDataModels.Models; +using Microsoft.Extensions.Logging; + +namespace HotelBusinessLogic.BusinessLogics; + +public class CleaningInstrumentsLogic : ICleaningInstrumentsLogic +{ + private readonly ICleaningInstrumentsStorage _cleaningInstruments; + private readonly ILogger _logger; + + public CleaningInstrumentsLogic(ICleaningInstrumentsStorage cleaningInstruments, ILogger logger) + { + _cleaningInstruments = cleaningInstruments; + _logger = logger; + } + + public List? ReadList(CleaningInstrumentsSearchModel? model) + { + var list = model == null ? _cleaningInstruments.GetFullList() : _cleaningInstruments.GetFilteredList(model); + _logger.LogInformation("ReadList .Count:{Count}", list.Count); + return list; + } + + public CleaningInstrumentsViewModel? ReadElement(CleaningInstrumentsSearchModel model) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + _logger.LogInformation("ReadElement .Id:{Id}", model.Id); + var element = _cleaningInstruments.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(CleaningInstrumentsBindingModel model) + { + CheckModel(model); + if (_cleaningInstruments.Insert(model) != null) return true; + _logger.LogWarning("Insert operation failed"); + return false; + } + + public bool Update(CleaningInstrumentsBindingModel model) + { + CheckModel(model); + if (_cleaningInstruments.Update(model) != null) return true; + _logger.LogWarning("Update operation failed"); + return false; + } + + public bool Delete(CleaningInstrumentsBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete .Id:{Id}", model.Id); + if (_cleaningInstruments.Delete(model) != null) return true; + _logger.LogWarning("Delete operation failed"); + return false; + } + + private void CheckModel(CleaningInstrumentsBindingModel? model, bool withParams = true) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + if (!withParams) + return; + if (string.IsNullOrEmpty(model.Type)) + throw new ArgumentException("Type must be not null"); + } +} \ No newline at end of file diff --git a/Hotel/HotelBusinessLogic/BusinessLogics/CleaningLogic.cs b/Hotel/HotelBusinessLogic/BusinessLogics/CleaningLogic.cs new file mode 100644 index 0000000..79bc74e --- /dev/null +++ b/Hotel/HotelBusinessLogic/BusinessLogics/CleaningLogic.cs @@ -0,0 +1,77 @@ +using HotelContracts.BindingModels; +using HotelContracts.BusinessLogicsContracts; +using HotelContracts.SearchModels; +using HotelContracts.StoragesContracts; +using HotelContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace HotelBusinessLogic.BusinessLogics; + +public class CleaningLogic : ICleaningLogic +{ + private readonly ICleaningStorage _cleaningStorage; + private readonly ILogger _logger; + + public CleaningLogic(ICleaningStorage cleaningStorage, ILogger logger) + { + _logger = logger; + _cleaningStorage = cleaningStorage; + } + + public List? ReadList(CleaningSearchModel? model) + { + var list = model == null ? _cleaningStorage.GetFullList() : _cleaningStorage.GetFilteredList(model); + _logger.LogInformation("ReadList .Count:{Count}", list.Count); + return list; + } + + public CleaningViewModel? ReadElement(CleaningSearchModel model) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + _logger.LogInformation("ReadElement .Id:{Id}", model.Id); + var element = _cleaningStorage.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(CleaningBindingModel model) + { + CheckModel(model); + if (_cleaningStorage.Insert(model) != null) return true; + _logger.LogWarning("Insert operation failed"); + return false; + } + + public bool Update(CleaningBindingModel model) + { + CheckModel(model); + if (_cleaningStorage.Update(model) != null) return true; + _logger.LogWarning("Update operation failed"); + return false; + } + + public bool Delete(CleaningBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete .Id:{Id}", model.Id); + if (_cleaningStorage.Delete(model) != null) return true; + _logger.LogWarning("Delete operation failed"); + return false; + } + + private void CheckModel(CleaningBindingModel? model, bool withParams = true) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + if (!withParams) + return; + if (model.RoomId < 0) + throw new ArgumentException("RoomId must be more then 0"); + } +} \ No newline at end of file diff --git a/Hotel/HotelBusinessLogic/BusinessLogics/GuestLogic.cs b/Hotel/HotelBusinessLogic/BusinessLogics/GuestLogic.cs new file mode 100644 index 0000000..80767bd --- /dev/null +++ b/Hotel/HotelBusinessLogic/BusinessLogics/GuestLogic.cs @@ -0,0 +1,81 @@ +using HotelContracts.BindingModels; +using HotelContracts.BusinessLogicsContracts; +using HotelContracts.SearchModels; +using HotelContracts.StoragesContracts; +using HotelContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace HotelBusinessLogic.BusinessLogics; + +public class GuestLogic : IGuestLogic +{ + private readonly IGuestStorage _guestStorage; + private readonly ILogger _logger; + + public GuestLogic(IGuestStorage guestStorage, ILogger logger) + { + _logger = logger; + _guestStorage = guestStorage; + } + + public List? ReadList(GuestSearchModel? model) + { + var list = model == null ? _guestStorage.GetFullList() : _guestStorage.GetFilteredList(model); + _logger.LogInformation("ReadList .Count:{Count}", list.Count); + return list; + } + + public GuestViewModel? ReadElement(GuestSearchModel model) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + _logger.LogInformation("ReadElement .Id:{Id}", model.Id); + var element = _guestStorage.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(GuestBindingModel model) + { + CheckModel(model); + if (_guestStorage.Insert(model) != null) return true; + _logger.LogWarning("Insert operation failed"); + return false; + } + + public bool Update(GuestBindingModel model) + { + CheckModel(model); + if (_guestStorage.Update(model) != null) return true; + _logger.LogWarning("Update operation failed"); + return false; + } + + public bool Delete(GuestBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete .Id:{Id}", model.Id); + if (_guestStorage.Delete(model) != null) return true; + _logger.LogWarning("Delete operation failed"); + return false; + } + + private void CheckModel(GuestBindingModel? model, bool withParams = true) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + if (!withParams) + return; + if (string.IsNullOrEmpty(model.Name)) + throw new ArgumentException("Name must be not null"); + if (string.IsNullOrEmpty(model.SecondName)) + throw new ArgumentException("Second name must be not null"); + if (string.IsNullOrEmpty(model.LastName)) + throw new ArgumentException("Last name must be not null"); + } +} \ No newline at end of file diff --git a/Hotel/HotelBusinessLogic/BusinessLogics/MaitreLogic.cs b/Hotel/HotelBusinessLogic/BusinessLogics/MaitreLogic.cs new file mode 100644 index 0000000..d42e231 --- /dev/null +++ b/Hotel/HotelBusinessLogic/BusinessLogics/MaitreLogic.cs @@ -0,0 +1,88 @@ +using HotelContracts.BindingModels; +using HotelContracts.BusinessLogicsContracts; +using HotelContracts.SearchModels; +using HotelContracts.StoragesContracts; +using HotelContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace HotelBusinessLogic.BusinessLogics; + +public class MaitreLogic : IMaitreLogic +{ + private readonly IMaitreStorage _maitreStorage; + private readonly ILogger _logger; + + public MaitreLogic(IMaitreStorage maitreLogic, ILogger logger) + { + _logger = logger; + _maitreStorage = maitreLogic; + } + + public List? ReadList(MaitreSearchModel? model) + { + var list = model == null ? _maitreStorage.GetFullList() : _maitreStorage.GetFilteredList(model); + _logger.LogInformation("ReadList .Count:{Count}", list.Count); + return list; + } + + public MaitreViewModel? ReadElement(MaitreSearchModel model) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + _logger.LogInformation("ReadElement .Id:{Id}", model.Id); + var element = _maitreStorage.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(MaitreBindingModel model) + { + CheckModel(model); + if (_maitreStorage.Insert(model) != null) return true; + _logger.LogWarning("Insert operation failed"); + return false; + } + + public bool Update(MaitreBindingModel model) + { + CheckModel(model); + if (_maitreStorage.Update(model) != null) return true; + _logger.LogWarning("Update operation failed"); + return false; + } + + public bool Delete(MaitreBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete .Id:{Id}", model.Id); + if (_maitreStorage.Delete(model) != null) return true; + _logger.LogWarning("Delete operation failed"); + return false; + } + + private void CheckModel(MaitreBindingModel? model, bool withParams = true) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + if (!withParams) + return; + if (string.IsNullOrEmpty(model.Name)) + throw new ArgumentException("Name must be not null"); + if (string.IsNullOrEmpty(model.SecondName)) + throw new ArgumentException("Second name must be not null"); + if (string.IsNullOrEmpty(model.LastName)) + throw new ArgumentException("Last name must be not null"); + var Maitre = _maitreStorage.GetElement(new MaitreSearchModel { + Login = model.Login + }); + if (Maitre != null && Maitre.Id != model.Id) + { + throw new InvalidOperationException("Login is already exists"); + } + } +} \ No newline at end of file diff --git a/Hotel/HotelBusinessLogic/BusinessLogics/ReservationLogic.cs b/Hotel/HotelBusinessLogic/BusinessLogics/ReservationLogic.cs new file mode 100644 index 0000000..8c72a2a --- /dev/null +++ b/Hotel/HotelBusinessLogic/BusinessLogics/ReservationLogic.cs @@ -0,0 +1,76 @@ +using HotelContracts.BindingModels; +using HotelContracts.BusinessLogicsContracts; +using HotelContracts.SearchModels; +using HotelContracts.StoragesContracts; +using HotelContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace HotelBusinessLogic.BusinessLogics; + +public class ReservationLogic : IReservationLogic +{ + private readonly ILogger _logger; + private readonly IReservationStorage _reservationStorage; + + public ReservationLogic(IReservationStorage reservationStorage, ILogger logger) + { + _logger = logger; + _reservationStorage = reservationStorage; + } + + public List? ReadList(ReservationSearchModel? model) + { + var list = model == null ? _reservationStorage.GetFullList() : _reservationStorage.GetFilteredList(model); + _logger.LogInformation("ReadList .Count:{Count}", list.Count); + return list; + } + + public ReservationViewModel? ReadElement(ReservationSearchModel model) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + _logger.LogInformation("ReadElement .Id:{Id}", model.Id); + var element = _reservationStorage.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(ReservationBindingModel model) + { + CheckModel(model); + if (_reservationStorage.Insert(model) != null) return true; + _logger.LogWarning("Insert operation failed"); + return false; + } + + public bool Update(ReservationBindingModel model) + { + CheckModel(model); + if (_reservationStorage.Update(model) != null) return true; + _logger.LogWarning("Update operation failed"); + return false; + } + + public bool Delete(ReservationBindingModel model) + { + CheckModel(model); + _logger.LogInformation("Delete .Id:{Id}", model.Id); + if (_reservationStorage.Delete(model) != null) return true; + _logger.LogWarning("Delete operation failed"); + return false; + } + + private void CheckModel(ReservationBindingModel? model) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + if (model.StartDate > model.EndDate) + throw new ArgumentException("Start date must be early then end date"); + _logger.LogInformation("Reservation .Id:{Id}", model.Id); + } +} \ No newline at end of file diff --git a/Hotel/HotelBusinessLogic/BusinessLogics/RoomLogic.cs b/Hotel/HotelBusinessLogic/BusinessLogics/RoomLogic.cs new file mode 100644 index 0000000..beaa027 --- /dev/null +++ b/Hotel/HotelBusinessLogic/BusinessLogics/RoomLogic.cs @@ -0,0 +1,80 @@ +using HotelContracts.BindingModels; +using HotelContracts.BusinessLogicsContracts; +using HotelContracts.SearchModels; +using HotelContracts.StoragesContracts; +using HotelContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace HotelBusinessLogic.BusinessLogics; + +public class RoomLogic : IRoomLogic +{ + private readonly IRoomStorage _roomStorage; + private readonly ILogger _logger; + + public RoomLogic(IRoomStorage roomStorage, ILogger logger) + { + _logger = logger; + _roomStorage = roomStorage; + } + + public List? ReadList(RoomSearchModel? model) + { + var list = model == null ? _roomStorage.GetFullList() : _roomStorage.GetFilteredList(model); + _logger.LogInformation("ReadList .Count:{Count}", list.Count); + return list; + } + + public RoomViewModel? ReadElement(RoomSearchModel model) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + _logger.LogInformation("ReadElement .Id:{Id} .Type:{Type} .Cost:{Cost}", + model.Id, model.Type, model.Cost); + var element = _roomStorage.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(RoomBindingModel model) + { + CheckModel(model); + if (_roomStorage.Insert(model) != null) return true; + _logger.LogWarning("Insert operation failed"); + return false; + + } + + public bool Update(RoomBindingModel model) + { + CheckModel(model); + if (_roomStorage.Update(model) != null) return true; + _logger.LogWarning("Update operation failed"); + return false; + + } + + public bool Delete(RoomBindingModel model) + { + CheckModel(model); + _logger.LogInformation("Delete .Id:{Id}", model.Id); + if (_roomStorage.Delete(model) != null) return true; + _logger.LogWarning("Delete operation failed"); + return false; + } + + private void CheckModel(RoomBindingModel? model) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + if (string.IsNullOrEmpty(model.Type)) + throw new ArgumentException("Type must be not null"); + _logger.LogInformation("Room .Id:{Id} .Type:{Type} .Cost:{Cost}", + model.Id, model.Type, model.Cost); + } +} \ No newline at end of file diff --git a/Hotel/HotelBusinessLogic/HotelBusinessLogic.csproj b/Hotel/HotelBusinessLogic/HotelBusinessLogic.csproj index 132c02c..2eed333 100644 --- a/Hotel/HotelBusinessLogic/HotelBusinessLogic.csproj +++ b/Hotel/HotelBusinessLogic/HotelBusinessLogic.csproj @@ -6,4 +6,12 @@ enable + + + + + + + + diff --git a/Hotel/HotelView/HotelView.csproj b/Hotel/HotelView/HotelView.csproj index c78c9c7..cb306cf 100644 --- a/Hotel/HotelView/HotelView.csproj +++ b/Hotel/HotelView/HotelView.csproj @@ -6,4 +6,8 @@ enable + + + +