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

142 lines
4.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 ManagerLogic : IManagerLogic
{
private readonly ILogger _logger;
private readonly IManagerStorage _managerStorage;
public ManagerLogic(ILogger<ManagerLogic> logger, IManagerStorage managerStorage)
{
_logger = logger;
_managerStorage = managerStorage;
}
public bool Create(ManagerBindingModel model)
{
CheckModel(model);
if (_managerStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(ManagerBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_managerStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public ManagerViewModel? ReadElement(ManagerSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Login: {Login}. Id: {Id}", model.Login, model.Id);
var element = _managerStorage.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<ManagerViewModel>? ReadList(ManagerSearchModel? model)
{
_logger.LogInformation("ReadList. Login: {Login}. Id: {Id}", model.Login, model.Id);
var list = model == null ? _managerStorage.GetFullList() : _managerStorage.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(ManagerBindingModel model)
{
CheckModel(model);
if (_managerStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(ManagerBindingModel 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 (string.IsNullOrEmpty(model.Login))
{
throw new ArgumentNullException("Нет логина у менеджера", nameof(model.Login));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Нет пароля у менеджера", nameof(model.Password));
}
if (string.IsNullOrEmpty(model.PhoneNumber))
{
throw new ArgumentNullException("У номер телефона у менеджера", nameof(model.PhoneNumber));
}
_logger.LogInformation("Manager. Login: {Login}. Password: {Password}. PhoneNumber: {PhoneNumber}. Id: {Id}", model.Login, model.Password, model.PhoneNumber, model.Id);
var element = _managerStorage.GetElement(new ManagerSearchModel { Login = model.Login , Password = model.Password});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Такой менеджер уже есть");
}
}
}
}