PIbd-22_Karamushko_M_K_Univ.../University/UniversityBuisnessLogic/ActivityLogic.cs
2023-04-05 20:24:16 +04:00

103 lines
3.3 KiB
C#

using UniversityContracts.BindingModels;
using UniversityContracts.BuisnessLogicContracts;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace UniversityBuisnessLogic
{
public class ActivityLogic : IActivityLogic
{
private readonly ILogger _logger;
private readonly IActivityStorage _activityStorage;
public ActivityLogic(ILogger<ActivityLogic> logger, IActivityStorage activityStorage)
{
_logger = logger;
_activityStorage = activityStorage;
}
public bool Create(ActivityBindingModel model)
{
CheckModel(model);
if (_activityStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(ActivityBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_activityStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public ActivityViewModel? ReadElement(ActivitySearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id:{Id}", model.Id);
var element = _activityStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<ActivityViewModel>? ReadList(ActivitySearchModel? model)
{
_logger.LogInformation("ReadList. Id:{Id}", model?.Id);
var list = model == null ? _activityStorage.GetFullList() : _activityStorage.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(ActivityBindingModel model)
{
CheckModel(model);
if (_activityStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(ActivityBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.Number <= 0)
{
throw new ArgumentNullException("Номер занятия должен быть больше 0", nameof(model.Number));
}
_logger.LogInformation("Activity. Number:{ComponentName}. Id: {Id}", model.Number, model.Id);
}
}
}