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

104 lines
2.7 KiB
C#
Raw Normal View History

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 CircleLessonLogic : ICircleLessonLogic
{
private readonly ILogger _logger;
private readonly ICircleLessonStorage _circleLessonStorage;
public CircleLessonLogic(ILogger<CircleLessonLogic> logger, ICircleLessonStorage circleLessonStorage)
{
_logger= logger;
_circleLessonStorage= circleLessonStorage;
}
public bool Create(CircleLessonBindingModel model)
{
CheckModel(model);
if (_circleLessonStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(CircleLessonBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_circleLessonStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public CircleLessonViewModel? ReadElement(CircleLessonSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id: {Id}", model.Id);
var element = _circleLessonStorage.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<CircleLessonViewModel>? ReadList(CircleLessonSearchModel? model)
{
_logger.LogInformation("ReadList. Id: {Id}", model?.Id);
var list = model == null ? _circleLessonStorage.GetFullList() : _circleLessonStorage.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(CircleLessonBindingModel model)
{
CheckModel(model);
if (_circleLessonStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(CircleLessonBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentException(nameof(model));
}
if (!withParams)
{
return;
}
_logger.LogInformation("CircleLesson. Id: {Id}", model.Id);
}
}
}