using HotelContracts.BindingModels; using HotelContracts.BusinessLogicsContracts; using HotelContracts.SearchModels; using HotelContracts.StoragesContracts; using HotelContracts.ViewModels; 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 IRoomStorage _roomStorage; private readonly IWorkerStorage _workerStorage; public RoomLogic(IRoomStorage roomStorage, IWorkerStorage workerStorage) { _roomStorage = roomStorage; _workerStorage = workerStorage; } public RoomViewModel? ReadElement(RoomSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _roomStorage.GetElement(model); if (element == null) { return null; } return element; } public List? ReadList(RoomSearchModel? model) { var list = model == null ? _roomStorage.GetFullList() : _roomStorage.GetFilteredList(model); if (list == null) { return null; } return list; } public bool Create(RoomBindingModel model) { CheckModel(model); if (_roomStorage.Insert(model) == null) { return false; } return true; } public bool Delete(RoomBindingModel model) { CheckModel(model, false); if (_roomStorage.Delete(model) == null) { return false; } return true; } public bool Update(RoomBindingModel model) { CheckModel(model); if (_roomStorage.Update(model) == null) { 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.Condition)) { throw new ArgumentNullException("Нет поля состояние", nameof(model.Condition)); } if (model.WorkerId <= 0) { throw new ArgumentNullException("Некорректный идентификатор рабочего", nameof(model.WorkerId)); } if (model.Number <= 0) { throw new ArgumentNullException("нет поля номера", nameof(model.Number)); } if (model.Floor <= 0) { throw new ArgumentNullException("нет поля этаж", nameof(model.Floor)); } if (model.NumberOfBeds <= 0) { throw new ArgumentNullException("нет поля количество спальных мест", nameof(model.NumberOfBeds)); } if (model.Cost <= 0) { throw new ArgumentNullException("нет поля цены", nameof(model.Cost)); } var elementNumber = _roomStorage.GetElement(new RoomSearchModel { Number = model.Number }); if (elementNumber != null && elementNumber.Id != model.Id) { throw new InvalidOperationException("Такой номер уже существует"); } } public bool ProvercaMade(RoomBindingModel model) { CheckModel(model, false); var room = ReadElement(new RoomSearchModel { Number = model.Number}); var workers = _workerStorage.GetFullList(); if(room == null) return false; foreach (var item in workers) { if (room.WorkerId == item.Id) { if(item.PostName == "Горничная" || item.PostName == "Горничный") { return true; } } } return false; } } }