2023-05-05 18:44:46 +04:00

144 lines
4.1 KiB
C#

using ForumContracts.BindingModels;
using ForumContracts.BusinessLogicContracts;
using ForumContracts.SearchModels;
using ForumContracts.StorageContracts;
using ForumContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ForumBusinessLogic.BusinessLogic
{
public class QuestionLogic : IQuestionLogic
{
private readonly ILogger _logger;
private readonly IQuestionStorage _questionStorage;
public QuestionLogic(ILogger<QuestionLogic> logger, IQuestionStorage questionStorage)
{
_logger = logger;
_questionStorage = questionStorage;
}
public bool Create(QuestionBindingModel model)
{
CheckModel(model);
if (_questionStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(QuestionBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_questionStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public QuestionViewModel? ReadElement(QuestionSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. QuestionDes:{QuestionDes}.Id:{ Id}", model.QuestionDes, model.Id);
var element = _questionStorage.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<QuestionViewModel>? ReadList(QuestionSearchModel? model)
{
_logger.LogInformation("ReadList. QuestionDes:{QuestionDes}.Id:{ Id}", model?.QuestionDes, model?.Id);
var list = model == null ? _questionStorage.GetFullList() : _questionStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public List<QuestionViewModel>? HardRequest(QuestionSearchModel? model)
{
return _questionStorage.HardRequest(new QuestionSearchModel
{
DateFrom = model.DateFrom,
DateTo = model.DateTo
})
.Select(x => new QuestionViewModel
{
CreateDate = x.CreateDate,
QuestionDes = x.QuestionDes,
UserName = x.UserName,
CategoryName = x.CategoryName
})
.ToList();
}
public bool Update(QuestionBindingModel model)
{
CheckModel(model);
if (_questionStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(QuestionBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.QuestionDes))
{
throw new ArgumentNullException("Нет вопроса", nameof(model.QuestionDes));
}
_logger.LogInformation("Question. QuestionDes:{QuestionDes}.CreateDate:{ CreateDate}. Id: { Id}", model.QuestionDes, model.CreateDate, model.Id);
}
}
}