102 lines
2.5 KiB
C#
102 lines
2.5 KiB
C#
|
using Microsoft.Extensions.Logging;
|
|||
|
using SchoolContracts.BindingModel;
|
|||
|
using SchoolContracts.BusinessLogicsContracts;
|
|||
|
using SchoolContracts.SearchModel;
|
|||
|
using SchoolContracts.StoragesContracts;
|
|||
|
using SchoolContracts.ViewModels;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace SchoolBusinessLogic.BusinessLogics
|
|||
|
{
|
|||
|
public class CircleLogic : ICircleLogic
|
|||
|
{
|
|||
|
private readonly ILogger _logger;
|
|||
|
private readonly ICircleStorage _circleStorage;
|
|||
|
public CircleLogic(ILogger<CircleLogic> logger, ICircleStorage circleStorage)
|
|||
|
{
|
|||
|
_logger = logger;
|
|||
|
_circleStorage = circleStorage;
|
|||
|
}
|
|||
|
|
|||
|
public bool Create(CircleBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model);
|
|||
|
if (_circleStorage.Insert(model) == null)
|
|||
|
{
|
|||
|
_logger.LogWarning("Insert operation failed");
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
public bool Delete(CircleBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model, false);
|
|||
|
_logger.LogInformation("Delete. Id: {Id}", model.Id);
|
|||
|
if (_circleStorage.Delete(model) == null)
|
|||
|
{
|
|||
|
_logger.LogWarning("Delete operation failed");
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
public CircleViewModel? ReadElement(CircleSearchModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
throw new ArgumentNullException(nameof(model));
|
|||
|
}
|
|||
|
_logger.LogInformation("ReadElement. Id: {Id}", model.Id);
|
|||
|
var element = _circleStorage.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<CircleViewModel>? ReadList(CircleSearchModel? model)
|
|||
|
{
|
|||
|
_logger.LogInformation("ReadList. Id: {Id}", model?.Id);
|
|||
|
var list = model == null ? _circleStorage.GetFullList() : _circleStorage.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(CircleBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model);
|
|||
|
if (_circleStorage.Update(model) == null)
|
|||
|
{
|
|||
|
_logger.LogWarning("Update operation failed");
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
private void CheckModel(CircleBindingModel model, bool withParams = true)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
throw new ArgumentException(nameof(model));
|
|||
|
}
|
|||
|
if (!withParams)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
_logger.LogInformation("Circle. Id: {Id}", model.Id);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|