SUBD_Forum/ForumView/ForumBusinessLogic/BusinessLogic/AnswerLogic.cs

128 lines
3.5 KiB
C#
Raw Normal View History

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 AnswerLogic : IAnswerLogic
{
private readonly ILogger _logger;
private readonly IAnswerStorage _commentStorage;
public AnswerLogic(ILogger<AnswerLogic> logger, IAnswerStorage answerStorage)
{
_logger = logger;
_commentStorage = answerStorage;
}
public bool Create(AnswerBindingModel model)
{
CheckModel(model);
if (_commentStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(AnswerBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_commentStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public AnswerViewModel? ReadElement(AnswerSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. AnswerDes:{AnswerDes}.Id:{ Id}", model.Comment, model.Id);
var element = _commentStorage.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<AnswerViewModel>? ReadList(AnswerSearchModel? model)
{
_logger.LogInformation("ReadList. AnswerDes:{AnswerDes}.Id:{ Id}", model?.Comment, model?.Id);
var list = model == null ? _commentStorage.GetFullList() : _commentStorage.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(AnswerBindingModel model)
{
CheckModel(model);
if (_commentStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(AnswerBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Comment))
{
throw new ArgumentNullException("Нет ответа", nameof(model.Comment));
}
_logger.LogInformation("Answer. AnswerDes:{AnswerDes}.ResponseDate:{ ResponseDate}. Id: { Id}", model.Comment, model.ResponseDate, model.Id);
}
}
}