218 lines
6.7 KiB
C#
218 lines
6.7 KiB
C#
using CanteenContracts.BindingModels;
|
||
using CanteenContracts.BusinessLogicsContracts;
|
||
using CanteenContracts.SearchModel;
|
||
using CanteenContracts.SearchModels;
|
||
using CanteenContracts.StoragesContracts;
|
||
using CanteenContracts.View;
|
||
using CanteenDataModels.Enums;
|
||
using CanteenDataModels.Models;
|
||
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;
|
||
}
|
||
|
||
public bool UpdateOrders(LunchBindingModel lunch, OrderBindingModel order)
|
||
{
|
||
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,
|
||
LunchOrders = _lunch.LunchOrders
|
||
|
||
}) == null)
|
||
{
|
||
_logger.LogWarning("Update operation failed");
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
public bool UpdateProducts(LunchBindingModel lunch, ProductBindingModel product, int count)
|
||
{
|
||
var _lunch = _lunchStorage.GetElement(new LunchSearchModel { Id = lunch.Id });
|
||
if (count == -1)
|
||
{
|
||
if (_lunch.LunchProducts.ContainsKey(product.Id))
|
||
{
|
||
_lunch.LunchProducts.Remove(product.Id);
|
||
}
|
||
}
|
||
else if (count > 0)
|
||
{
|
||
_lunch.LunchProducts[product.Id] = (product, count);
|
||
}
|
||
double allSum = 0;
|
||
foreach (var lunchProducts in _lunch.LunchProducts)
|
||
{
|
||
IProductModel _product = lunchProducts.Value.Item1;
|
||
int _count = lunchProducts.Value.Item2;
|
||
allSum += _product.Price * _count;
|
||
}
|
||
if (_lunchStorage.Update(new()
|
||
{
|
||
Id = _lunch.Id,
|
||
LunchName = _lunch.LunchName,
|
||
VisitorId = _lunch.VisitorId,
|
||
LunchProducts = _lunch.LunchProducts,
|
||
DateCreate = _lunch.DateCreate,
|
||
Status = _lunch.Status,
|
||
DateImplement = _lunch.DateImplement,
|
||
Sum = allSum
|
||
}) == null)
|
||
{
|
||
_logger.LogWarning("Update operation failed");
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
}
|
||
}
|