2025-02-18 11:43:34 +03:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using PapaCarloContracts.BuisinessLogicsContracts;
|
2025-02-17 14:16:47 +03:00
|
|
|
|
using PapaCarloContracts.DataModels;
|
2025-02-18 16:41:53 +03:00
|
|
|
|
using PapaCarloContracts.Exceptions;
|
|
|
|
|
using PapaCarloContracts.Extensions;
|
2025-02-18 11:43:34 +03:00
|
|
|
|
using PapaCarloContracts.StoragesContracts;
|
2025-02-17 14:16:47 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2025-02-18 16:41:53 +03:00
|
|
|
|
using System.Text.Json;
|
2025-02-17 14:16:47 +03:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace PapaCarloBusinessLogic.Implementations;
|
|
|
|
|
|
2025-02-18 11:43:34 +03:00
|
|
|
|
internal class PostBusinessLogicContract(IPostStorageContract postStorageContract, ILogger logger) : IPostBusinessLogicContract
|
2025-02-17 14:16:47 +03:00
|
|
|
|
{
|
2025-02-18 16:41:53 +03:00
|
|
|
|
private readonly ILogger _logger = logger;
|
|
|
|
|
private readonly IPostStorageContract _postStorageContract = postStorageContract;
|
|
|
|
|
|
|
|
|
|
public List<PostDataModel> GetAllPosts(bool onlyActive = true)
|
2025-02-17 14:16:47 +03:00
|
|
|
|
{
|
2025-02-18 16:41:53 +03:00
|
|
|
|
_logger.LogInformation("GetAllPosts params: {onlyActive}", onlyActive);
|
|
|
|
|
return _postStorageContract.GetList(onlyActive) ?? throw new NullListException();
|
2025-02-17 14:16:47 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<PostDataModel> GetAllDataOfPost(string postId)
|
|
|
|
|
{
|
2025-02-18 16:41:53 +03:00
|
|
|
|
_logger.LogInformation("GetAllDataOfPost for {postId}", postId);
|
|
|
|
|
if (postId.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(postId));
|
|
|
|
|
}
|
|
|
|
|
if (!postId.IsGuid())
|
|
|
|
|
{
|
|
|
|
|
throw new ValidationException("The value in the field postId is not a unique identifier.");
|
|
|
|
|
}
|
|
|
|
|
return _postStorageContract.GetPostWithHistory(postId) ?? throw new NullListException();
|
2025-02-17 14:16:47 +03:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-18 16:41:53 +03:00
|
|
|
|
public PostDataModel GetPostByData(string data)
|
2025-02-17 14:16:47 +03:00
|
|
|
|
{
|
2025-02-18 16:41:53 +03:00
|
|
|
|
_logger.LogInformation("Get element by data: {data}", data);
|
|
|
|
|
if (data.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(data));
|
|
|
|
|
}
|
|
|
|
|
if (data.IsGuid())
|
|
|
|
|
{
|
|
|
|
|
return _postStorageContract.GetElementById(data) ?? throw new ElementNotFoundException(data);
|
|
|
|
|
}
|
|
|
|
|
return _postStorageContract.GetElementByName(data) ?? throw new ElementNotFoundException(data);
|
2025-02-17 14:16:47 +03:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-18 16:41:53 +03:00
|
|
|
|
public void InsertPost(PostDataModel postDataModel)
|
2025-02-17 14:16:47 +03:00
|
|
|
|
{
|
2025-02-18 16:41:53 +03:00
|
|
|
|
_logger.LogInformation("New data: {json}", JsonSerializer.Serialize(postDataModel));
|
|
|
|
|
ArgumentNullException.ThrowIfNull(postDataModel);
|
|
|
|
|
postDataModel.Validate();
|
|
|
|
|
_postStorageContract.AddElement(postDataModel);
|
2025-02-17 14:16:47 +03:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-18 16:41:53 +03:00
|
|
|
|
public void UpdatePost(PostDataModel postDataModel)
|
2025-02-17 14:16:47 +03:00
|
|
|
|
{
|
2025-02-18 16:41:53 +03:00
|
|
|
|
_logger.LogInformation("Update data: {json}", JsonSerializer.Serialize(postDataModel));
|
|
|
|
|
ArgumentNullException.ThrowIfNull(postDataModel);
|
|
|
|
|
postDataModel.Validate();
|
|
|
|
|
_postStorageContract.UpdElement(postDataModel);
|
2025-02-17 14:16:47 +03:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-18 16:41:53 +03:00
|
|
|
|
public void DeletePost(string id)
|
2025-02-17 14:16:47 +03:00
|
|
|
|
{
|
2025-02-18 16:41:53 +03:00
|
|
|
|
_logger.LogInformation("Delete by id: {id}", id);
|
|
|
|
|
if (id.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(id));
|
|
|
|
|
}
|
|
|
|
|
if (!id.IsGuid())
|
|
|
|
|
{
|
|
|
|
|
throw new ValidationException("Id is not a unique identifier");
|
|
|
|
|
}
|
|
|
|
|
_postStorageContract.DelElement(id);
|
2025-02-17 14:16:47 +03:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-18 16:41:53 +03:00
|
|
|
|
public void RestorePost(string id)
|
2025-02-17 14:16:47 +03:00
|
|
|
|
{
|
2025-02-18 16:41:53 +03:00
|
|
|
|
_logger.LogInformation("Restore by id: {id}", id);
|
|
|
|
|
if (id.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(id));
|
|
|
|
|
}
|
|
|
|
|
if (!id.IsGuid())
|
|
|
|
|
{
|
|
|
|
|
throw new ValidationException("Id is not a unique identifier");
|
|
|
|
|
}
|
|
|
|
|
_postStorageContract.ResElement(id);
|
2025-02-17 14:16:47 +03:00
|
|
|
|
}
|
2025-02-18 16:41:53 +03:00
|
|
|
|
}
|