From 91cedbacc0cf8a5bd15bd1bce782e1c6d32049a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B0=D1=88=D0=B8=D0=BD=20=D0=9C=D0=B0=D0=BA=D1=81?= =?UTF-8?q?=D0=B8=D0=BC?= Date: Fri, 7 Apr 2023 00:25:26 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20RoomLogic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/RoomLogic.cs | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 Hotel/HotelBusinessLogic/BusinessLogics/RoomLogic.cs diff --git a/Hotel/HotelBusinessLogic/BusinessLogics/RoomLogic.cs b/Hotel/HotelBusinessLogic/BusinessLogics/RoomLogic.cs new file mode 100644 index 0000000..8f42701 --- /dev/null +++ b/Hotel/HotelBusinessLogic/BusinessLogics/RoomLogic.cs @@ -0,0 +1,136 @@ +using HotelContracts.BindingModels; +using HotelContracts.BusinessLogicsContracts; +using HotelContracts.SearchModels; +using HotelContracts.StoragesContracts; +using HotelContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HotelBusinessLogic.BusinessLogics +{ + public class RoomLogic : IRoomLogic + { + private readonly ILogger _logger; + private readonly IRoomStorage _roomStorage; + + public RoomLogic(ILogger logger, IRoomStorage roomStorage) + { + _logger = logger; + _roomStorage = roomStorage; + } + public bool Create(RoomBindingModel model) + { + CheckModel(model); + + if (_roomStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + + return true; + } + + public bool Delete(RoomBindingModel model) + { + CheckModel(model, false); + + _logger.LogInformation("Delete. Id:{Id}", model.Id); + + if (_roomStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + + return true; + } + + public RoomViewModel? ReadElement(RoomSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + _logger.LogInformation("ReadElement. RoomName:{RoomName}.Id:{Id}", model.RoomName, model.Id); + + 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 List? ReadList(RoomSearchModel? model) + { + _logger.LogInformation("ReadList. RoomName:{RoomName}.Id:{ Id}", model?.RoomName, model?.Id); + + var list = model == null ? _roomStorage.GetFullList() : _roomStorage.GetFilteredList(model); + + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + + return list; + } + + public bool Update(RoomBindingModel model) + { + CheckModel(model); + + if (_roomStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + + return true; + } + + private void CheckModel(RoomBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + if (!withParams) + { + return; + } + + if (string.IsNullOrEmpty(model.RoomName)) + { + throw new ArgumentNullException("Нет названия комнате", nameof(model.RoomName)); + } + + + if (string.IsNullOrEmpty(model.RoomFrame)) + { + throw new ArgumentNullException("Нет названия корпусу", nameof(model.RoomFrame)); + } + + if (model.RoomPrice < 0) + { + throw new ArgumentNullException("Стоимость комнаты не может быть меньше 0", nameof(model.RoomPrice)); + } + + _logger.LogInformation("Room. RoomName:{RoomName}.RoomFrame:{ RoomFrame}.RoomPrice:{ RoomPrice}. Id: { Id}", model.RoomName, model.RoomFrame, model.RoomPrice, model.Id); + } + } +}