PIbd-22_Karamushko_M_K_Univ.../University/UniversityBuisnessLogic/StatementLogic.cs

102 lines
3.4 KiB
C#
Raw Normal View History

2023-04-05 20:24:16 +04:00
using UniversityContracts.BindingModels;
using UniversityContracts.BuisnessLogicContracts;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace UniversityBuisnessLogicf
{
public class StatementLogic : IStatementLogic
{
private readonly ILogger _logger;
private readonly IStatementStorage _statementStorage;
public StatementLogic(ILogger<StatementLogic> logger, IStatementStorage statementStorage)
{
_logger = logger;
_statementStorage = statementStorage;
}
public bool Create(StatementBindingModel model)
{
CheckModel(model);
if (_statementStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(StatementBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_statementStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public StatementViewModel? ReadElement(StatementSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id:{Id}", model.Id);
var element = _statementStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
f
public List<StatementViewModel>? ReadList(StatementSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{Id}", model?.Id);
var list = model == null ? _statementStorage.GetFullList() : _statementStorage.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(StatementBindingModel model)
{
CheckModel(model);
if (_statementStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(StatementBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.HoursCount <= 0)
{
throw new ArgumentNullException("Количество часов в ведомости должно быть больше 0", nameof(model.HoursCount));
}
_logger.LogInformation("Statement. HoursCount:{HoursCount}. Id: { Id}", model.HoursCount, model.Id);
}
}
}