171 lines
4.2 KiB
C#
171 lines
4.2 KiB
C#
using HotelContracts.BindingModels;
|
||
using HotelContracts.BusinessLogicsContracts;
|
||
using HotelContracts.SearchModels;
|
||
using HotelContracts.StoragesContracts;
|
||
using HotelContracts.ViewModels;
|
||
using HotelDataModels.Models;
|
||
using Microsoft.Extensions.Logging;
|
||
|
||
namespace HotelBusinessLogic.BusinessLogic
|
||
{
|
||
public class MealPlanLogic : IMealPlanLogic
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly IMealPlanStorage _mealPlanStorage;
|
||
|
||
public MealPlanLogic (ILogger<MealPlanLogic> logger, IMealPlanStorage mealPlanStorage)
|
||
{
|
||
_logger = logger;
|
||
_mealPlanStorage = mealPlanStorage;
|
||
}
|
||
|
||
public List<MealPlanViewModel>? ReadList(MealPlanSearchModel? model)
|
||
{
|
||
_logger.LogInformation("ReadList. MealPlanName:{MealPlanName}.Id:{ Id}", model?.MealPlanName, model?.Id);
|
||
|
||
var list = model == null ? _mealPlanStorage.GetFullList() : _mealPlanStorage.GetFilteredList(model);
|
||
|
||
if (list == null)
|
||
{
|
||
_logger.LogWarning("ReadList return null list");
|
||
return null;
|
||
}
|
||
|
||
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||
return list;
|
||
}
|
||
|
||
public MealPlanViewModel? ReadElement(MealPlanSearchModel? model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
|
||
_logger.LogInformation("ReadList. MealPlanName:{MealPlanName}.Id:{ Id}", model?.MealPlanName, model?.Id);
|
||
|
||
var element = _mealPlanStorage.GetElement(model);
|
||
|
||
if (element == null)
|
||
{
|
||
_logger.LogInformation("ReadElement element not found");
|
||
return null;
|
||
}
|
||
|
||
_logger.LogInformation("ReadElement find: Id:{Id}", model.Id);
|
||
|
||
return element;
|
||
}
|
||
|
||
public bool Create(MealPlanBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
model.MealPlanParticipants = new();
|
||
|
||
if (_mealPlanStorage.Insert == null)
|
||
{
|
||
_logger.LogWarning("Insert error");
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
public bool Update(MealPlanBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
|
||
if (_mealPlanStorage.Update == null)
|
||
{
|
||
_logger.LogWarning("Update error");
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
public bool Delete(MealPlanBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
|
||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||
|
||
if (_mealPlanStorage.Delete == null)
|
||
{
|
||
_logger.LogWarning("Delete error");
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
public bool AddMemberToMealPlan(MealPlanSearchModel model, IParticipantModel participant)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
|
||
_logger.LogInformation("AddMemberToMealPlan. MealPlanName:{MealPlanName}.Id:{ Id}", model.MealPlanName, model.Id);
|
||
|
||
var element = _mealPlanStorage.GetElement(model);
|
||
|
||
if (element == null)
|
||
{
|
||
_logger.LogWarning("AddMemberToMealPlan element not found");
|
||
return false;
|
||
}
|
||
|
||
_logger.LogInformation("AddMemberToMealPlan find. Id:{Id}", element.Id);
|
||
|
||
element.MealPlanParticipants[participant.Id] = participant;
|
||
|
||
_mealPlanStorage.Update(new()
|
||
{
|
||
Id = element.Id,
|
||
MealPlanName = element.MealPlanName,
|
||
MealPlanPrice = element.MealPlanPrice,
|
||
OrganiserId = element.OrganiserId,
|
||
MealPlanParticipants = element.MealPlanParticipants
|
||
});
|
||
|
||
return true;
|
||
}
|
||
|
||
public void CheckModel(MealPlanBindingModel model, bool withParams = true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
|
||
if (string.IsNullOrEmpty(model.MealPlanName))
|
||
{
|
||
throw new ArgumentNullException("Нет названия плана питания", nameof(model.MealPlanName));
|
||
}
|
||
|
||
if (model.MealPlanPrice < 0)
|
||
{
|
||
throw new ArgumentNullException("Стоимость плана питания не может быть меньше 0", nameof(model.MealPlanPrice));
|
||
}
|
||
|
||
_logger.LogInformation("MealPlan. MealPlanName:{MealPlanName}.MealPlanPrice:{ MealPlanPrice}. Id: { Id}", model.MealPlanName, model.MealPlanPrice, model.Id);
|
||
|
||
var element = _mealPlanStorage.GetElement(new MealPlanSearchModel
|
||
{
|
||
MealPlanName = model.MealPlanName,
|
||
});
|
||
|
||
if (element != null && element.Id != model.Id)
|
||
{
|
||
throw new InvalidOperationException("План питания с таким названием уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|