ISEbd-21_Tukaeva_A_A_School/School/SchoolBusinessLogic/BusinessLogics/CircleExpenseLogic.cs

104 lines
2.7 KiB
C#

using Microsoft.Extensions.Logging;
using SchoolContracts.BindingModels;
using SchoolContracts.BusinessLogicsContracts;
using SchoolContracts.SearchModel;
using SchoolContracts.StoragesContracts;
using SchoolContracts.ViewModels;
using SchoolDatabaseImplement.Implements;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolBusinessLogic.BusinessLogics
{
public class CircleExpenseLogic : ICircleExpenseLogic
{
private readonly ILogger _logger;
private readonly ICircleExpenseStorage _circleExpenseStorage;
public CircleExpenseLogic(ILogger<CircleExpenseLogic> logger, ICircleExpenseStorage circleExpenseStorage)
{
_logger= logger;
_circleExpenseStorage= circleExpenseStorage;
}
public bool Create(CircleExpenseBindingModel model)
{
CheckModel(model);
if (_circleExpenseStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(CircleExpenseBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_circleExpenseStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public CircleExpenseViewModel? ReadElement(CircleExpenseSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id: {Id}", model.Id);
var element = _circleExpenseStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement found. Id: {Id}", element.Id);
return element;
}
public List<CircleExpenseViewModel>? ReadList(CircleExpenseSearchModel? model)
{
_logger.LogInformation("ReadList. Id: {Id}", model?.Id);
var list = model == null ? _circleExpenseStorage.GetFullList() : _circleExpenseStorage.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(CircleExpenseBindingModel model)
{
CheckModel(model);
if (_circleExpenseStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(CircleExpenseBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentException(nameof(model));
}
if (!withParams)
{
return;
}
_logger.LogInformation("CircleExpense. Id: {Id}", model.Id);
}
}
}