Case_accounting/CaseAccounting/CaseAccountingBusinessLogics/BusinessLogics/ContractLogic.cs

118 lines
3.9 KiB
C#
Raw Normal View History

2023-04-07 03:03:14 +04:00
using CaseAccountingContracts.BindingModels;
using CaseAccountingContracts.BusinessLogicContracts;
using CaseAccountingContracts.SearchModels;
using CaseAccountingContracts.StoragesContracts;
using CaseAccountingContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CaseAccountingBusinessLogic.BusinessLogics
{
public class ContractLogic : IContractLogic
{
private readonly ILogger _logger;
private readonly IContractStorage _contractStorage;
public ContractLogic(ILogger<ContractLogic> logger, IContractStorage contractStorage)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_contractStorage = contractStorage ?? throw new ArgumentNullException(nameof(logger));
2023-04-07 03:03:14 +04:00
}
public bool Create(ContractBindingModel model)
{
CheckModel(model);
if (_contractStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(ContractBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_contractStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public ContractViewModel? ReadElement(ContractSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id:{ Id}", model.Id);
var element = _contractStorage.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<ContractViewModel>? ReadList(ContractSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{ Id}", model?.Id);
var list = model == null ? _contractStorage.GetFullList() : _contractStorage.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(ContractBindingModel model)
{
CheckModel(model);
if (_contractStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(ContractBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.UserId < 0)
{
throw new ArgumentNullException("Некорректный идентификатор пользователя",
nameof(model.UserId));
}
if (model.Coast <= 0)
{
throw new ArgumentNullException("Некорректная стоимость услуги",
nameof(model.Coast));
}
if (model.Service == string.Empty)
{
throw new ArgumentNullException("Некорректное название услуги",
nameof(model.Service));
}
}
}
}