2023-04-08 19:57:10 +04:00
|
|
|
|
using BankContracts.BindingModels;
|
|
|
|
|
using BankContracts.BusinessLogicsContracts;
|
|
|
|
|
using BankContracts.SearchModels;
|
|
|
|
|
using BankContracts.StoragesContracts;
|
|
|
|
|
using BankContracts.ViewModels;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace BankBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
2023-04-08 22:43:54 +04:00
|
|
|
|
public class CreditLogic : ICreditLogic
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
2023-04-08 22:43:54 +04:00
|
|
|
|
private readonly ICreditStorage _dinnerStorage;
|
2023-04-08 19:57:10 +04:00
|
|
|
|
|
2023-04-08 22:43:54 +04:00
|
|
|
|
public CreditLogic(ILogger<CreditLogic> logger, ICreditStorage dinnerStorage)
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_dinnerStorage = dinnerStorage;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 22:43:54 +04:00
|
|
|
|
public bool Create(CreditBindingModel model)
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
|
|
|
|
|
if (_dinnerStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 22:43:54 +04:00
|
|
|
|
public bool Delete(CreditBindingModel model)
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
|
|
|
|
|
|
|
|
|
if (_dinnerStorage.Delete(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 22:43:54 +04:00
|
|
|
|
public CreditViewModel? ReadElement(CreditSearchModel model)
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadElement. DinnerName:{DinnerName}.Id:{Id}", model.DinnerName, model.Id);
|
|
|
|
|
|
|
|
|
|
var element = _dinnerStorage.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-08 22:43:54 +04:00
|
|
|
|
public List<CreditViewModel>? ReadList(CreditSearchModel? model)
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("ReadList. DinnerName:{DinnerName}.Id:{ Id}", model?.DinnerName, model?.Id);
|
|
|
|
|
|
|
|
|
|
var list = model == null ? _dinnerStorage.GetFullList() : _dinnerStorage.GetFilteredList(model);
|
|
|
|
|
|
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadList return null list");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 22:43:54 +04:00
|
|
|
|
public bool Update(CreditBindingModel model)
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
|
|
|
|
|
if (_dinnerStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-04-08 22:43:54 +04:00
|
|
|
|
private void CheckModel(CreditBindingModel model, bool withParams = true)
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(model.DinnerName))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Нет имени обеда", nameof(model.DinnerName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (model.DinnerPrice < 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Стоимость обеда не может быть меньше 0", nameof(model.DinnerPrice));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Dinner. DinnerName:{DinnerName}.DinnerPrice:{ DinnerPrice}. Id: { Id}", model.DinnerName, model.DinnerPrice, model.Id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|