PIbd-23_Elatomtsev_L.K._Cou.../Bank/BankBusinessLogic/BusinessLogics/ProgramLogic.cs

122 lines
4.3 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 BankContracts.BindingModels;
using BankContracts.BusinessLogicsContracts;
using BankContracts.SearchModels;
using BankContracts.StoragesContracts;
using BankContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace BankBusinessLogic.BusinessLogics
{
public class ProgramLogic : IProgramLogic
{
private readonly ILogger _logger;
private readonly IProgramStorage _programStorage;
public ProgramLogic(ILogger<ProgramLogic> logger,
IProgramStorage programStorage)
{
_logger = logger;
_programStorage = programStorage;
}
public bool Create(ProgramBindingModel model)
{
CheckModel(model);
if (_programStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(ProgramBindingModel model)
{
CheckModel(model);
if (_programStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ProgramBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_programStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public List<ProgramViewModel>? ReadList(ProgramSearchModel? model)
{
_logger.LogInformation(
"ReadList. ProgramName: {ProgramName}. Id: {Id}",
model?.ProgramName, model?.Id);
var list = model == null ? _programStorage.GetFullList() :
_programStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public ProgramViewModel? ReadElement(ProgramSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation(
"ReadElement. ProgramName: {ProgramName}. Id: {Id}",
model.ProgramName, model.Id);
var element = _programStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id: {Id}", element.Id);
return element;
}
private void CheckModel(ProgramBindingModel model,
bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.ProgramName))
{
throw new ArgumentNullException(
"Отсутствует название кредитной программы",
nameof(model.ProgramName));
}
if (model.InterestRate < 0)
{
throw new ArgumentNullException(
"Процентная ставка должна быть не менее 0",
nameof(model.InterestRate));
}
_logger.LogInformation("Program. Id: {Id}. ProgramName: " +
"{ProgramName}. InterestRate: {InterestRate}.",
model.Id, model.ProgramName, model.InterestRate);
var element = _programStorage.GetElement(new ProgramSearchModel
{
ProgramName = model.ProgramName,
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException(
"Кредитная программа с таким названием уже существует");
}
}
}
}