2023-04-09 13:00:51 +04:00
|
|
|
|
using CanteenContracts.BindingModels;
|
|
|
|
|
using CanteenContracts.BusinessLogicsContracts;
|
|
|
|
|
using CanteenContracts.SearchModel;
|
|
|
|
|
using CanteenContracts.SearchModels;
|
|
|
|
|
using CanteenContracts.StoragesContracts;
|
|
|
|
|
using CanteenContracts.View;
|
|
|
|
|
using CanteenDataModels.Enums;
|
2023-05-19 03:39:58 +04:00
|
|
|
|
using CanteenDataModels.Models;
|
2023-04-09 13:00:51 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace CanteenBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
public class LunchLogic : ILunchLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly ILunchStorage _lunchStorage;
|
|
|
|
|
|
|
|
|
|
public LunchLogic(ILogger<LunchLogic> logger, ILunchStorage lunchStorage)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_lunchStorage = lunchStorage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<LunchViewModel>? ReadList(LunchSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("ReadList. Id: {Id}", model?.Id);
|
|
|
|
|
|
|
|
|
|
var list = model == null ? _lunchStorage.GetFullList() : _lunchStorage.GetFilteredList(model);
|
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadList return null list");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LunchViewModel? ReadElement(LunchSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadElement. Id: {Id}", model.Id);
|
|
|
|
|
|
|
|
|
|
var element = _lunchStorage.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(LunchBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
|
|
|
|
|
if (_lunchStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Update(LunchBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
|
|
|
|
|
if (_lunchStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Delete(LunchBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Delete. Id: {Id}", model.Id);
|
|
|
|
|
if (_lunchStorage.Delete(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckModel(LunchBindingModel model, bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(model.LunchName))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Нет названия обеда", nameof(model.LunchName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (model.VisitorId <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("id посетителя должен быть больше 0", nameof(model.VisitorId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Lunch. LunchName: {LunchName}. Sum: {Sum}. VisitorId: {VisitorId}. Id: {Id}", model.LunchName, model.Sum, model.VisitorId, model.Id);
|
|
|
|
|
|
|
|
|
|
var element = _lunchStorage.GetElement(new LunchSearchModel { Id = model.Id });
|
|
|
|
|
if (element != null && element.Id != model.Id)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Обед с таким id уже есть");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Finish(LunchBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
|
|
|
|
|
if (_lunchStorage.GetElement(new LunchSearchModel { Id = model.Id })?.Status != LunchStatus.Создан)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Invalid lunch status");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
model.Status = LunchStatus.Окончен;
|
|
|
|
|
model.DateImplement = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
if (_lunchStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-20 00:18:48 +04:00
|
|
|
|
public bool UpdateOrders(LunchBindingModel lunch, OrderBindingModel order)
|
2023-04-09 13:00:51 +04:00
|
|
|
|
{
|
2023-05-20 00:18:48 +04:00
|
|
|
|
var _lunch = _lunchStorage.GetElement(new LunchSearchModel { Id = lunch.Id});
|
|
|
|
|
_lunch.LunchOrders[order.Id] = order as IOrderModel;
|
|
|
|
|
if (_lunchStorage.Update(new()
|
|
|
|
|
{
|
|
|
|
|
Id = _lunch.Id,
|
|
|
|
|
LunchName = _lunch.LunchName,
|
|
|
|
|
Sum = _lunch.Sum,
|
|
|
|
|
VisitorId = _lunch.VisitorId,
|
2023-06-21 14:52:08 +04:00
|
|
|
|
DateImplement = _lunch.DateImplement,
|
|
|
|
|
LunchOrders = _lunch.LunchOrders,
|
|
|
|
|
Status = _lunch.Status,
|
2023-05-20 00:18:48 +04:00
|
|
|
|
|
|
|
|
|
}) == null)
|
2023-04-09 13:00:51 +04:00
|
|
|
|
{
|
2023-05-20 00:18:48 +04:00
|
|
|
|
_logger.LogWarning("Update operation failed");
|
2023-04-09 13:00:51 +04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-20 00:18:48 +04:00
|
|
|
|
public bool UpdateProducts(LunchBindingModel lunch, ProductBindingModel product, int count)
|
|
|
|
|
{
|
|
|
|
|
var _lunch = _lunchStorage.GetElement(new LunchSearchModel { Id = lunch.Id });
|
2023-05-20 12:11:48 +04:00
|
|
|
|
double allSum = 0;
|
|
|
|
|
foreach (var lunchProducts in _lunch.LunchProducts)
|
|
|
|
|
{
|
|
|
|
|
IProductModel _product = lunchProducts.Value.Item1;
|
|
|
|
|
int _count = lunchProducts.Value.Item2;
|
|
|
|
|
allSum += _product.Price * _count;
|
|
|
|
|
}
|
2023-05-20 00:18:48 +04:00
|
|
|
|
if (count == -1)
|
2023-04-09 13:00:51 +04:00
|
|
|
|
{
|
2023-05-20 00:18:48 +04:00
|
|
|
|
if (_lunch.LunchProducts.ContainsKey(product.Id))
|
|
|
|
|
{
|
|
|
|
|
_lunch.LunchProducts.Remove(product.Id);
|
2023-05-20 12:11:48 +04:00
|
|
|
|
allSum -= product.Price * _lunch.LunchProducts[product.Id].Item2;
|
2023-05-20 00:18:48 +04:00
|
|
|
|
}
|
2023-04-09 13:00:51 +04:00
|
|
|
|
}
|
2023-05-20 00:18:48 +04:00
|
|
|
|
else if (count > 0)
|
2023-04-09 13:00:51 +04:00
|
|
|
|
{
|
2023-05-20 00:18:48 +04:00
|
|
|
|
_lunch.LunchProducts[product.Id] = (product, count);
|
2023-05-20 12:11:48 +04:00
|
|
|
|
allSum += product.Price * count;
|
2023-04-09 13:00:51 +04:00
|
|
|
|
}
|
2023-05-20 12:11:48 +04:00
|
|
|
|
|
2023-05-20 00:18:48 +04:00
|
|
|
|
if (_lunchStorage.Update(new()
|
2023-04-09 13:00:51 +04:00
|
|
|
|
{
|
2023-05-20 00:18:48 +04:00
|
|
|
|
Id = _lunch.Id,
|
|
|
|
|
LunchName = _lunch.LunchName,
|
|
|
|
|
VisitorId = _lunch.VisitorId,
|
|
|
|
|
LunchProducts = _lunch.LunchProducts,
|
|
|
|
|
DateCreate = _lunch.DateCreate,
|
|
|
|
|
Status = _lunch.Status,
|
|
|
|
|
DateImplement = _lunch.DateImplement,
|
|
|
|
|
Sum = allSum
|
|
|
|
|
}) == null)
|
2023-04-09 13:00:51 +04:00
|
|
|
|
{
|
2023-05-20 00:18:48 +04:00
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
2023-04-09 13:00:51 +04:00
|
|
|
|
}
|
2023-05-19 03:39:58 +04:00
|
|
|
|
|
2023-05-20 00:18:48 +04:00
|
|
|
|
return true;
|
2023-05-19 03:39:58 +04:00
|
|
|
|
}
|
2023-04-09 13:00:51 +04:00
|
|
|
|
}
|
|
|
|
|
}
|