130 lines
3.2 KiB
C#

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.BusinessLogic
{
public class RoomLogic : IRoomLogic
{
private readonly ILogger _logger;
private readonly IRoomStorage _roomStorage;
public RoomLogic(ILogger<RoomLogic> logger, IRoomStorage roomStorage)
{
_logger = logger;
_roomStorage = roomStorage;
}
public List<RoomViewModel>? ReadList(RoomSearchModel? model)
{
_logger.LogInformation("ReadList. RoomNumber:{RoomNumber}.Id:{ Id}", model?.RoomNumber, 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 RoomViewModel? ReadElement(RoomSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. RoomNumber:{RoomNumber}.Id:{Id}", model.RoomNumber, 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 bool Create(RoomBindingModel model)
{
CheckModel(model);
if (_roomStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(RoomBindingModel model)
{
CheckModel(model);
if (_roomStorage.Update(model) == null)
{
_logger.LogWarning("Update 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;
}
private void CheckModel(RoomBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.RoomNumber < 0)
{
throw new ArgumentNullException("Номер комнаты не может быть меньше 0", nameof(model.RoomNumber));
}
if (model.CountBeds < 0)
{
throw new ArgumentNullException("Количество спальных мест не может быть меньше 0", nameof(model.CountBeds));
}
if (model.RoomPrice < 0)
{
throw new ArgumentNullException("Цена комнаты не может быть меньше 0", nameof(model.RoomPrice));
}
_logger.LogInformation("Room. RoomNumber:{RoomNumber}.CountBeds:{CountBeds}.RoomPrice:{RoomPrice}. Id: {Id}", model.RoomNumber, model.CountBeds, model.RoomPrice, model.Id);
}
}
}