PIbd-21_BatylkinaAO_MusoevD.../Canteen/CanteenBusinessLogic/BusinessLogics/CookLogic.cs

137 lines
4.1 KiB
C#
Raw Permalink Normal View History

2023-04-09 13:00:51 +04:00
using CanteenContracts.BindingModels;
using CanteenContracts.BusinessLogicsContracts;
using CanteenContracts.SearchModel;
using CanteenContracts.StoragesContracts;
using CanteenContracts.View;
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 CookLogic : ICookLogic
{
private readonly ILogger _logger;
private readonly ICookStorage _cookStorage;
public CookLogic(ILogger<CookLogic> logger, ICookStorage cookStorage)
{
_logger = logger;
_cookStorage = cookStorage;
}
public bool Create(CookBindingModel model)
{
CheckModel(model);
if (_cookStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(CookBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_cookStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public CookViewModel? ReadElement(CookSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. FIO: {FIO}. Id: {Id}", model.FIO, model.Id);
var element = _cookStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id: {Id}", element.Id);
return element;
}
public List<CookViewModel>? ReadList(CookSearchModel? model)
{
_logger.LogInformation("ReadList. FIO: {FIO}. Id: {Id}", model?.FIO, model?.Id);
var list = model == null ? _cookStorage.GetFullList() : _cookStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public bool Update(CookBindingModel model)
{
CheckModel(model);
if (_cookStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(CookBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.FIO))
{
throw new ArgumentNullException("Нет ФИО у повара", nameof(model.FIO));
}
if (model.ManagerId <= 0)
{
2023-05-18 01:40:11 +04:00
throw new ArgumentNullException("id менеджера должен быть больше 0", nameof(model.ManagerId));
2023-04-09 13:00:51 +04:00
}
if (string.IsNullOrEmpty(model.Position))
{
throw new ArgumentNullException("У повара нет должности", nameof(model.Position));
}
_logger.LogInformation("Cook. FIO: {FIO}. ManagerId: {ManagerId}. Position: {Position}. Id: {Id}", model.FIO, model.ManagerId, model.Position, model.Id);
var element = _cookStorage.GetElement(new CookSearchModel { Id = model.Id });
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Такой повар уже есть");
}
}
}
}