PIbd-21_Shanygin_A.V_BaseData/Hotel/HotelBusinessLogic/BusinessLogics/BookingLogic.cs

128 lines
3.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using HotelContracts.BindingModels;
using HotelContracts.BusinessLogicsContracts;
using HotelContracts.SearchModels;
using HotelContracts.StoragesContracts;
using HotelContracts.ViewModels;
using HotelDataModels.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HotelBusinessLogic.BusinessLogics
{
public class BookingLogic : IBookingLogic
{
private readonly IBookingStorage _bookingStorage;
public BookingLogic(IBookingStorage bookingStorage)
{
_bookingStorage = bookingStorage;
}
public BookingViewModel? ReadElement(BookingSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _bookingStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public List<BookingViewModel>? ReadList(BookingSearchModel? model)
{
var list = model == null ? _bookingStorage.GetFullList() : _bookingStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public bool Create(BookingBindingModel model)
{
CheckModel(model);
if (_bookingStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(BookingBindingModel model)
{
CheckModel(model, false);
if (_bookingStorage.Delete(model) == null)
{
return false;
}
return true;
}
public bool Update(BookingBindingModel model)
{
CheckModel(model);
if (_bookingStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool TakeBookingInWork(BookingBindingModel model)
{
return ChangeStatus(model, AcceptanceStatus.Обработка);
}
public bool FinishBooking(BookingBindingModel model)
{
return ChangeStatus(model, AcceptanceStatus.Принимается);
}
public bool DeliveryBooking(BookingBindingModel model)
{
return ChangeStatus(model, AcceptanceStatus.Принят);
}
private void CheckModel(BookingBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.RoomId <= 0)
{
throw new ArgumentNullException("Некорректный идентификатор комнаты", nameof(model.RoomId));
}
if (model.ClientId <= 0)
{
throw new ArgumentNullException("Некорректный идентификатор клиента", nameof(model.ClientId));
}
}
private bool ChangeStatus(BookingBindingModel model, AcceptanceStatus newStatus)
{
CheckModel(model, false);
if (model.Status + 1 != newStatus)
{
return false;
}
model.Status = newStatus;
if (_bookingStorage.Update(model) == null)
{
model.Status--;
return false;
}
return true;
}
}
}