Files
PIBD-23_Coursach_YouAreProg…/YouAreProgrammerShop/YAPBusinessLogic/Implementations/CommentBusinessLogicContract.cs

139 lines
5.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.Extensions.Logging;
using YAPContracts.BusinessLogicContracts;
using YAPContracts.DataModels;
using YAPContracts.Exceptions;
using YAPContracts.Extentions;
using YAPContracts.StorageContracts;
namespace YAPBusinessLogic.Implementations
{
internal class CommentBusinessLogicContract(ICommentStorageContract commentStorageContract, ILogger logger) : ICommentBusinessLogicContract
{
private readonly ILogger _logger = logger;
private readonly ICommentStorageContract _commentStorageContract = commentStorageContract;
public List<CommentDataModel> GetAllComments()
{
_logger.LogInformation("GetAllComments method called");
var comments = _commentStorageContract.GetList();
return comments ?? throw new NullListException();
}
public CommentDataModel? GetCommentByData(string data)
{
_logger.LogInformation("GetCommentByData method called with data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data));
}
if (!data.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
}
var comment = _commentStorageContract.GetElementById(data);
return comment ?? throw new ElementNotFoundException(data);
}
public List<CommentDataModel> GetCommentsByProductSet(string productSetId)
{
_logger.LogInformation("GetCommentsByProductSet method called with productSetId: {productSetId}", productSetId);
if (productSetId.IsEmpty())
{
throw new ArgumentNullException(nameof(productSetId));
}
if (!productSetId.IsGuid())
{
throw new ValidationException("ProductSetId is not a unique identifier");
}
var comments = _commentStorageContract.GetList(productSetId: productSetId);
return comments ?? throw new NullListException();
}
public List<CommentDataModel> GetCommentsByProductSetByPeriod(string productSetId, DateTime fromDate, DateTime toDate)
{
_logger.LogInformation("GetCommentsByProductSetByPeriod method called with productSetId: {productSetId}, fromDate: {fromDate}, toDate: {toDate}", productSetId, fromDate, toDate);
if (productSetId.IsEmpty())
{
throw new ArgumentNullException(nameof(productSetId));
}
if (!productSetId.IsGuid())
{
throw new ValidationException("ProductSetId is not a unique identifier");
}
if (fromDate.IsNotLaterThan(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
var comments = _commentStorageContract.GetList(fromDate, toDate, productSetId: productSetId);
return comments ?? throw new NullListException();
}
public List<CommentDataModel> GetCommentsByUserByPeriod(string userId, DateTime fromDate, DateTime toDate)
{
_logger.LogInformation($"GetCommentsByUserSetByPeriod method called with userId: {userId}, fromDate: {fromDate}, toDate: {toDate}", userId, fromDate, toDate);
if (userId.IsEmpty())
{
throw new ArgumentNullException(nameof(userId));
}
if (!userId.IsGuid())
{
throw new ValidationException("UserId is not a unique identifier");
}
if (fromDate.IsNotLaterThan(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
var comments = _commentStorageContract.GetList(fromDate, toDate, userId: userId);
return comments ?? throw new NullListException();
}
public List<CommentDataModel> GetCommentsByPeriod(DateTime fromDate, DateTime toDate)
{
_logger.LogInformation($"GetCommentsByPeriod method called with fromDate: {fromDate}, toDate: {toDate}", fromDate, toDate);
if (fromDate.IsNotLaterThan(toDate))
{
throw new IncorrectDatesException(fromDate, toDate);
}
var comments = _commentStorageContract.GetList(fromDate, toDate);
return comments ?? throw new NullListException();
}
public void InsertComment(CommentDataModel comment)
{
_logger.LogInformation("Insert data: {json}", JsonSerializer.Serialize(comment));
ArgumentNullException.ThrowIfNull(comment);
comment.Validate();
_commentStorageContract.AddElement(comment);
}
public void UpdateComment(CommentDataModel comment)
{
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(comment));
ArgumentNullException.ThrowIfNull(comment);
comment.Validate();
_commentStorageContract.UpdateElement(comment);
}
public void DeleteComment(string id)
{
_logger.LogInformation($"Delete data by id: {id}", id);
if (id.IsEmpty())
{
throw new ArgumentNullException(nameof(id));
}
if (!id.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
}
_commentStorageContract.DeleteElement(id);
}
}
}