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 { public class RoomLogic : IRoomLogic { private readonly IRoomStorage _roomStorage; public RoomLogic(IRoomStorage roomStorage) { _roomStorage = roomStorage; } 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)); } } } }