2023-04-06 17:58:51 +04:00
|
|
|
|
using HotelContracts.BindingModels;
|
|
|
|
|
using HotelContracts.BusinessLogicsContracts;
|
|
|
|
|
using HotelContracts.SearchModels;
|
2023-04-06 18:27:12 +04:00
|
|
|
|
using HotelContracts.StoragesContracts;
|
2023-04-06 17:58:51 +04:00
|
|
|
|
using HotelContracts.ViewModels;
|
2023-05-16 19:43:06 +04:00
|
|
|
|
using HotelDataModels.Models;
|
2023-04-06 18:27:12 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-04-06 17:58:51 +04:00
|
|
|
|
|
|
|
|
|
namespace HotelBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
public class MealPlanLogic : IMealPlanLogic
|
|
|
|
|
{
|
2023-04-06 18:27:12 +04:00
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IMealPlanStorage _mealPlanStorage;
|
|
|
|
|
|
|
|
|
|
public MealPlanLogic(ILogger<MealPlanLogic> logger, IMealPlanStorage mealPlanStorage)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_mealPlanStorage = mealPlanStorage;
|
|
|
|
|
}
|
2023-05-16 19:43:06 +04:00
|
|
|
|
|
|
|
|
|
public bool AddMemberToMealPlan(MealPlanSearchModel model, IMemberModel member)
|
|
|
|
|
{
|
|
|
|
|
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.MealPlanMembers[member.Id] = member;
|
|
|
|
|
|
|
|
|
|
_mealPlanStorage.Update(new()
|
|
|
|
|
{
|
|
|
|
|
Id = element.Id,
|
|
|
|
|
MealPlanName = element.MealPlanName,
|
|
|
|
|
MealPlanPrice = element.MealPlanPrice,
|
|
|
|
|
OrganiserId = element.OrganiserId,
|
|
|
|
|
MealPlanMembers = element.MealPlanMembers
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 17:58:51 +04:00
|
|
|
|
public bool Create(MealPlanBindingModel model)
|
|
|
|
|
{
|
2023-04-06 18:27:12 +04:00
|
|
|
|
CheckModel(model);
|
2023-05-16 19:43:06 +04:00
|
|
|
|
model.MealPlanMembers = new();
|
2023-04-06 18:27:12 +04:00
|
|
|
|
|
|
|
|
|
if (_mealPlanStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2023-04-06 17:58:51 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Delete(MealPlanBindingModel model)
|
|
|
|
|
{
|
2023-04-06 18:27:12 +04:00
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
|
|
|
|
|
|
|
|
|
if (_mealPlanStorage.Delete(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2023-04-06 17:58:51 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MealPlanViewModel? ReadElement(MealPlanSearchModel model)
|
|
|
|
|
{
|
2023-04-06 18:27:12 +04:00
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadElement. MealPlanName:{MealPlanName}.Id:{Id}", model.MealPlanName, model.Id);
|
|
|
|
|
|
|
|
|
|
var element = _mealPlanStorage.GetElement(model);
|
|
|
|
|
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadElement element not found");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
|
|
|
|
|
|
|
|
|
|
return element;
|
2023-04-06 17:58:51 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<MealPlanViewModel>? ReadList(MealPlanSearchModel? model)
|
|
|
|
|
{
|
2023-04-06 18:27:12 +04:00
|
|
|
|
_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;
|
2023-04-06 17:58:51 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Update(MealPlanBindingModel model)
|
|
|
|
|
{
|
2023-04-06 18:27:12 +04:00
|
|
|
|
CheckModel(model);
|
|
|
|
|
|
|
|
|
|
if (_mealPlanStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private 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("План питания с таким названием уже есть");
|
|
|
|
|
}
|
2023-04-06 17:58:51 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|