Files
PIbd-24_EredavkinRA_TheAvto…/AutoTazBusinessLogic/implementations/PostBusinessLogicContract.cs
2025-03-13 15:23:49 +04:00

119 lines
3.9 KiB
C#

using AvtoTAZContratcs.BusinessLogicsContracts;
using AvtoTAZContratcs.DataModels;
using AvtoTAZContratcs.Enums;
using AvtoTAZContratcs.Exceptions;
using AvtoTAZContratcs.Extensions;
using AvtoTAZContratcs.StoragesContracts;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text.Json;
namespace AutoTazBusinessLogic.implementations;
internal class PostBusinessLogicContract(IPostStorageContact postStorageContact, ILogger logger) : IPostBusinessLogicContract
{
private readonly IPostStorageContact _postStorageContact = postStorageContact;
private readonly ILogger _logger = logger;
public List<PostDataModel> GetAllPosts(bool onlyActive)
{
_logger.LogInformation("GetAllPosts with onlyActive: {onlyActive}", onlyActive);
return _postStorageContact.GetList(onlyActive) ?? throw new NullListException();
// Заглушка для TDD
// return [];
}
public List<PostDataModel> GetAllDataOfPost(string postId)
{
_logger.LogInformation("GetAllDataOfPost for postId: {postId}", postId);
if (postId.IsEmpty())
{
throw new ArgumentNullException(nameof(postId), "Post ID cannot be null or empty");
}
if (!postId.IsGuid())
{
throw new ValidationException("The value in the field postId is not a unique identifier");
}
return _postStorageContact.GetPostWithHistory(postId) ?? throw new NullListException();
// Заглушка для TDD
// return [];
}
public PostDataModel GetPostByData(string data)
{
_logger.LogInformation("GetPostByData with data: {data}", data);
if (data.IsEmpty())
{
throw new ArgumentNullException(nameof(data), "Data cannot be null or empty");
}
PostDataModel? result;
if (data.IsGuid())
{
result = _postStorageContact.GetElementById(data);
}
else
{
result = _postStorageContact.GetElementByName(data);
}
return result ?? throw new ElementNotFoundException($"Post with data '{data}' not found");
// Заглушка для TDD
// return new PostDataModel("", "", 0, false);
}
public void InsertPost(PostDataModel postDataModel)
{
_logger.LogInformation("InsertPost with data: {json}", JsonSerializer.Serialize(postDataModel));
ArgumentNullException.ThrowIfNull(postDataModel);
postDataModel.Validate();
_postStorageContact.AddElement(postDataModel);
// Заглушка для TDD
// return;
}
public void UpdatePost(PostDataModel postDataModel)
{
_logger.LogInformation("UpdatePost with data: {json}", JsonSerializer.Serialize(postDataModel));
ArgumentNullException.ThrowIfNull(postDataModel);
postDataModel.Validate();
_postStorageContact.UpdElement(postDataModel);
// Заглушка для TDD
// return;
}
public void DeletePost(string id)
{
_logger.LogInformation("DeletePost with id: {id}", id);
if (id.IsEmpty())
{
throw new ArgumentNullException(nameof(id), "ID cannot be null or empty");
}
if (!id.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
}
_postStorageContact.DelElement(id);
// Заглушка для TDD
// return;
}
public void RestorePost(string id)
{
_logger.LogInformation("RestorePost with id: {id}", id);
if (id.IsEmpty())
{
throw new ArgumentNullException(nameof(id), "ID cannot be null or empty");
}
if (!id.IsGuid())
{
throw new ValidationException("Id is not a unique identifier");
}
_postStorageContact.ResElement(id);
// Заглушка для TDD
// return;
}
}