CourseWork_SchoolStudyAgain/SchoolAgainStudy/SchoolAgainStudyBusinessLogic/BusinessLogic/DiyLogic.cs

123 lines
4.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Extensions.Logging;
using SchoolAgainStudyContracts.BindingModel;
using SchoolAgainStudyContracts.BusinessLogicContracts;
using SchoolAgainStudyContracts.SearchModel;
using SchoolAgainStudyContracts.StorageContracts;
using SchoolAgainStudyContracts.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SchoolAgainStudyBusinessLogic.BusinessLogic
{
public class DiyLogic : IDiyLogic
{
private readonly ILogger _logger;
private readonly IDiyStorage _diyStorage;
public DiyLogic(ILogger<DiyLogic> logger, IDiyStorage diyStorage)
{
_logger = logger;
_diyStorage = diyStorage;
}
public List<DiyViewModel>? ReadList(DiySearchModel? model)
{
_logger.LogInformation("ReadList. Title:{Title}.Id:{ Id}", model?.Title, model?.Id);
var list = model == null ? _diyStorage.GetFullList() : _diyStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public DiyViewModel? ReadElement(DiySearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Title:{Title}. Id:{ Id}", model.Title, model.Id);
var element = _diyStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public bool Create(DiyBindingModel model)
{
CheckModel(model);
if (_diyStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(DiyBindingModel model)
{
CheckModel(model);
if (_diyStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(DiyBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_diyStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(DiyBindingModel model, bool withParams =
true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Title))
{
throw new ArgumentNullException("Не задано название", nameof(model.Title));
}
if (string.IsNullOrEmpty(model.Description))
{
throw new ArgumentNullException("Не задано описание", nameof(model.Description));
}
if (model.StudentId <= 0)
{
throw new ArgumentNullException("Неверно задан идентификатор школьника", nameof(model.StudentId));
}
if (model.TaskId <= 0)
{
throw new ArgumentNullException("Неверно задан идентификатор задания", nameof(model.TaskId));
}
_logger.LogInformation("Diy. Titel:{Title}.Description{Description}.StudentId:{StudentId}.TaskId{TaskId} Id: {Id}", model.Title,model.Description, model.StudentId,model.TaskId, model.Id);
var element = _diyStorage.GetElement(new DiySearchModel
{
Title = model.Title,
StudentId = model.StudentId
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Поделка с таким названием уже есть");
}
}
}
}