forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
256 lines
10 KiB
C#
256 lines
10 KiB
C#
using AutoMapper;
|
|
using MagicCarpetContracts.AdapterContracts;
|
|
using MagicCarpetContracts.AdapterContracts.OperationResponses;
|
|
using MagicCarpetContracts.BindingModels;
|
|
using MagicCarpetContracts.BuisnessLogicContracts;
|
|
using MagicCarpetContracts.DataModels;
|
|
using MagicCarpetContracts.Exceptions;
|
|
using MagicCarpetContracts.Mapper;
|
|
using MagicCarpetContracts.Resources;
|
|
using MagicCarpetContracts.ViewModels;
|
|
using Microsoft.Extensions.Localization;
|
|
using System.Text.Json;
|
|
|
|
namespace MagicCarpetWebApi.Adapters;
|
|
|
|
internal class PostAdapter(IPostBusinessLogicContract postBusinessLogicContract, IStringLocalizer<Messages> localizer, ILogger<PostAdapter> logger) : IPostAdapter
|
|
{
|
|
private readonly IPostBusinessLogicContract _postBusinessLogicContract = postBusinessLogicContract;
|
|
|
|
private readonly IStringLocalizer<Messages> _localizer = localizer;
|
|
|
|
private readonly ILogger _logger = logger;
|
|
|
|
private readonly JsonSerializerOptions JsonSerializerOptions = new() { PropertyNameCaseInsensitive = true };
|
|
|
|
public PostOperationResponse GetList()
|
|
{
|
|
try
|
|
{
|
|
return PostOperationResponse.OK([.. _postBusinessLogicContract.GetAllPosts().Select(x => CustomMapper.MapObject<PostViewModel>(x))]);
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return PostOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return PostOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public PostOperationResponse GetHistory(string id)
|
|
{
|
|
try
|
|
{
|
|
return PostOperationResponse.OK([.. _postBusinessLogicContract.GetAllDataOfPost(id).Select(x => CustomMapper.MapObject<PostViewModel>(x))]);
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return PostOperationResponse.BadRequest(_localizer["AdapterMessageEmptyDate"]);
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return PostOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageValidationException"], ex.Message));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return PostOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return PostOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public PostOperationResponse GetElement(string data)
|
|
{
|
|
try
|
|
{
|
|
return PostOperationResponse.OK(CustomMapper.MapObject<PostViewModel>(_postBusinessLogicContract.GetPostByData(data)));
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return PostOperationResponse.BadRequest(_localizer["AdapterMessageEmptyDate"]);
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return PostOperationResponse.NotFound(string.Format(_localizer["AdapterMessageElementNotFoundException"], data));
|
|
}
|
|
catch (ElementDeletedException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementDeletedException");
|
|
return PostOperationResponse.NotFound(string.Format(_localizer["AdapterMessageElementDeletedException"], data));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return PostOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return PostOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public PostOperationResponse RegisterPost(PostBindingModel postModel)
|
|
{
|
|
try
|
|
{
|
|
_postBusinessLogicContract.InsertPost(CustomMapper.MapObject<PostDataModel>(postModel));
|
|
return PostOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return PostOperationResponse.BadRequest(_localizer["AdapterMessageEmptyDate"]);
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return PostOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageValidationException"], ex.Message));
|
|
}
|
|
catch (ElementExistsException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementExistsException");
|
|
return PostOperationResponse.BadRequest(ex.Message);
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return PostOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return PostOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public PostOperationResponse ChangePostInfo(PostBindingModel postModel)
|
|
{
|
|
try
|
|
{
|
|
_postBusinessLogicContract.UpdatePost(CustomMapper.MapObject<PostDataModel>(postModel));
|
|
return PostOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return PostOperationResponse.BadRequest(_localizer["AdapterMessageEmptyDate"]);
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return PostOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageValidationException"], ex.Message));
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return PostOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageElementNotFoundException"], postModel.Id));
|
|
}
|
|
catch (ElementExistsException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementExistsException");
|
|
return PostOperationResponse.BadRequest(ex.Message);
|
|
}
|
|
catch (ElementDeletedException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementDeletedException");
|
|
return PostOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageElementDeletedException"], postModel.Id));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return PostOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return PostOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public PostOperationResponse RemovePost(string id)
|
|
{
|
|
try
|
|
{
|
|
_postBusinessLogicContract.DeletePost(id);
|
|
return PostOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return PostOperationResponse.BadRequest(_localizer["AdapterMessageEmptyDate"]);
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return PostOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageValidationException"], ex.Message));
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return PostOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageElementNotFoundException"], id));
|
|
}
|
|
catch (ElementDeletedException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementDeletedException");
|
|
return PostOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageElementDeletedException"], id));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return PostOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return PostOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public PostOperationResponse RestorePost(string id)
|
|
{
|
|
try
|
|
{
|
|
_postBusinessLogicContract.RestorePost(id);
|
|
return PostOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return PostOperationResponse.BadRequest(_localizer["AdapterMessageEmptyDate"]);
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return PostOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageValidationException"], ex.Message));
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return PostOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageElementNotFoundException"], id));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return PostOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return PostOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
}
|