96 lines
3.2 KiB
C#
Raw Permalink Normal View History

2024-02-02 11:10:52 +04:00
using EkzamenContracts.BindingModels;
using EkzamenContracts.BusinessLogicsContracts;
using EkzamenContracts.SearchModels;
using EkzamenContracts.StoragesContract;
using EkzamenContracts.ViewModels;
using Microsoft.Extensions.Logging;
2024-02-02 11:10:52 +04:00
namespace EkzamenBusinessLogic
{
2024-02-02 11:10:52 +04:00
public class StudentLogic : IStudentLogic
{
private readonly ILogger _logger;
2024-02-02 11:10:52 +04:00
private readonly IStudentStorage _componentStorage;
public StudentLogic(ILogger<StudentLogic> logger, IStudentStorage componentStorage)
{
_logger = logger;
_componentStorage = componentStorage;
}
2024-02-02 11:10:52 +04:00
public List<StudentViewModel>? ReadList(StudentSearchModel? model)
{
_logger.LogInformation("ReadList. ComponentName:{ComponentName}.Id:{ Id} ",
2024-02-02 11:10:52 +04:00
model?.CreatedDateFrom, model?.Id);
var list = (model == null) ? _componentStorage.GetFullList() :
_componentStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
2024-02-02 11:10:52 +04:00
public StudentViewModel? ReadElement(StudentSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ComponentName:{ComponentName}.Id:{ Id}",
2024-02-02 11:10:52 +04:00
model.CreatedDateFrom, model.Id);
var element = _componentStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
2024-02-02 11:10:52 +04:00
public bool Create(StudentBindingModel model)
{
CheckModel(model);
if (_componentStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
2024-02-02 11:10:52 +04:00
public bool Update(StudentBindingModel model)
{
CheckModel(model);
if (_componentStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
2024-02-02 11:10:52 +04:00
public bool Delete(StudentBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_componentStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
2024-02-02 11:10:52 +04:00
private void CheckModel(StudentBindingModel model, bool withParams =
true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
2024-02-02 11:10:52 +04:00
if (!withParams)
{
return;
}
}
}
}