адаптеры

This commit is contained in:
2025-03-25 18:04:21 +04:00
parent 5097441b78
commit 3a9c533507
15 changed files with 1475 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
using MagicCarpetContracts.AdapterContracts.OperationResponses;
using MagicCarpetContracts.BindingModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicCarpetContracts.AdapterContracts;
public interface IClientAdapter
{
ClientOperationResponse GetList();
ClientOperationResponse GetElement(string data);
ClientOperationResponse RegisterClient(ClientBindingModel clientModel);
ClientOperationResponse ChangeClientInfo(ClientBindingModel clientModel);
ClientOperationResponse RemoveClient(string id);
}

View File

@@ -0,0 +1,28 @@
using MagicCarpetContracts.AdapterContracts.OperationResponses;
using MagicCarpetContracts.BindingModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicCarpetContracts.AdapterContracts;
public interface IEmployeeAdapter
{
EmployeeOperationResponse GetList(bool includeDeleted);
EmployeeOperationResponse GetPostList(string id, bool includeDeleted);
EmployeeOperationResponse GetListByBirthDate(DateTime fromDate, DateTime toDate, bool includeDeleted);
EmployeeOperationResponse GetListByEmploymentDate(DateTime fromDate, DateTime toDate, bool includeDeleted);
EmployeeOperationResponse GetElement(string data);
EmployeeOperationResponse RegisterEmployee(EmployeeBindingModel employeeModel);
EmployeeOperationResponse ChangeEmployeeInfo(EmployeeBindingModel employeeModel);
EmployeeOperationResponse RemoveEmployee(string id);
}

View File

@@ -0,0 +1,26 @@
using MagicCarpetContracts.AdapterContracts.OperationResponses;
using MagicCarpetContracts.BindingModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicCarpetContracts.AdapterContracts;
public interface IPostAdapter
{
PostOperationResponse GetList();
PostOperationResponse GetHistory(string id);
PostOperationResponse GetElement(string data);
PostOperationResponse RegisterPost(PostBindingModel postModel);
PostOperationResponse ChangePostInfo(PostBindingModel postModel);
PostOperationResponse RemovePost(string id);
PostOperationResponse RestorePost(string id);
}

View File

@@ -0,0 +1,26 @@
using MagicCarpetContracts.AdapterContracts.OperationResponses;
using MagicCarpetContracts.BindingModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicCarpetContracts.AdapterContracts;
public interface ISaleAdapter
{
SaleOperationResponse GetList(DateTime fromDate, DateTime toDate);
SaleOperationResponse GetEmployeeList(string id, DateTime fromDate, DateTime toDate);
SaleOperationResponse GetClientList(string id, DateTime fromDate, DateTime toDate);
SaleOperationResponse GetCocktailList(string id, DateTime fromDate, DateTime toDate);
SaleOperationResponse GetElement(string id);
SaleOperationResponse MakeSale(SaleBindingModel saleModel);
SaleOperationResponse CancelSale(string id);
}

View File

@@ -0,0 +1,24 @@
using MagicCarpetContracts.AdapterContracts.OperationResponses;
using MagicCarpetContracts.BindingModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicCarpetContracts.AdapterContracts;
public interface ITourAdapter
{
TourOperationResponse GetList(bool includeDeleted);
TourOperationResponse GetHistory(string id);
TourOperationResponse GetElement(string data);
TourOperationResponse RegisterTour(TourBindingModel tourModel);
TourOperationResponse ChangeTourInfo(TourBindingModel tourModel);
TourOperationResponse RemoveTour(string id);
}

View File

@@ -0,0 +1,24 @@
using MagicCarpetContracts.Infrastructure;
using MagicCarpetContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicCarpetContracts.AdapterContracts.OperationResponses;
public class ClientOperationResponse : OperationResponse
{
public static ClientOperationResponse OK(List<ClientViewModel> data) => OK<ClientOperationResponse, List<ClientViewModel>>(data);
public static ClientOperationResponse OK(ClientViewModel data) => OK<ClientOperationResponse, ClientViewModel>(data);
public static ClientOperationResponse NoContent() => NoContent<ClientOperationResponse>();
public static ClientOperationResponse BadRequest(string message) => BadRequest<ClientOperationResponse>(message);
public static ClientOperationResponse NotFound(string message) => NotFound<ClientOperationResponse>(message);
public static ClientOperationResponse InternalServerError(string message) => InternalServerError<ClientOperationResponse>(message);
}

View File

@@ -0,0 +1,24 @@
using MagicCarpetContracts.Infrastructure;
using MagicCarpetContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicCarpetContracts.AdapterContracts.OperationResponses;
public class EmployeeOperationResponse : OperationResponse
{
public static EmployeeOperationResponse OK(List<EmployeeViewModel> data) => OK<EmployeeOperationResponse, List<EmployeeViewModel>>(data);
public static EmployeeOperationResponse OK(EmployeeViewModel data) => OK<EmployeeOperationResponse, EmployeeViewModel>(data);
public static EmployeeOperationResponse NoContent() => NoContent<EmployeeOperationResponse>();
public static EmployeeOperationResponse NotFound(string message) => NotFound<EmployeeOperationResponse>(message);
public static EmployeeOperationResponse BadRequest(string message) => BadRequest<EmployeeOperationResponse>(message);
public static EmployeeOperationResponse InternalServerError(string message) => InternalServerError<EmployeeOperationResponse>(message);
}

View File

@@ -0,0 +1,24 @@
using MagicCarpetContracts.Infrastructure;
using MagicCarpetContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicCarpetContracts.AdapterContracts.OperationResponses;
public class PostOperationResponse : OperationResponse
{
public static PostOperationResponse OK(List<PostViewModel> data) => OK<PostOperationResponse, List<PostViewModel>>(data);
public static PostOperationResponse OK(PostViewModel data) => OK<PostOperationResponse, PostViewModel>(data);
public static PostOperationResponse NoContent() => NoContent<PostOperationResponse>();
public static PostOperationResponse NotFound(string message) => NotFound<PostOperationResponse>(message);
public static PostOperationResponse BadRequest(string message) => BadRequest<PostOperationResponse>(message);
public static PostOperationResponse InternalServerError(string message) => InternalServerError<PostOperationResponse>(message);
}

View File

@@ -0,0 +1,24 @@
using MagicCarpetContracts.Infrastructure;
using MagicCarpetContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicCarpetContracts.AdapterContracts.OperationResponses;
public class SaleOperationResponse : OperationResponse
{
public static SaleOperationResponse OK(List<SaleViewModel> data) => OK<SaleOperationResponse, List<SaleViewModel>>(data);
public static SaleOperationResponse OK(SaleViewModel data) => OK<SaleOperationResponse, SaleViewModel>(data);
public static SaleOperationResponse NoContent() => NoContent<SaleOperationResponse>();
public static SaleOperationResponse NotFound(string message) => NotFound<SaleOperationResponse>(message);
public static SaleOperationResponse BadRequest(string message) => BadRequest<SaleOperationResponse>(message);
public static SaleOperationResponse InternalServerError(string message) => InternalServerError<SaleOperationResponse>(message);
}

View File

@@ -0,0 +1,26 @@
using MagicCarpetContracts.Infrastructure;
using MagicCarpetContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicCarpetContracts.AdapterContracts.OperationResponses;
public class TourOperationResponse : OperationResponse
{
public static TourOperationResponse OK(List<TourViewModel> data) => OK<TourOperationResponse, List<TourViewModel>>(data);
public static TourOperationResponse OK(List<TourHistoryViewModel> data) => OK<TourOperationResponse, List<TourHistoryViewModel>>(data);
public static TourOperationResponse OK(TourViewModel data) => OK<TourOperationResponse, TourViewModel>(data);
public static TourOperationResponse NoContent() => NoContent<TourOperationResponse>();
public static TourOperationResponse NotFound(string message) => NotFound<TourOperationResponse>(message);
public static TourOperationResponse BadRequest(string message) => BadRequest<TourOperationResponse>(message);
public static TourOperationResponse InternalServerError(string message) => InternalServerError<TourOperationResponse>(message);
}

View File

@@ -0,0 +1,188 @@
using AutoMapper;
using MagicCarpetContracts.AdapterContracts;
using MagicCarpetContracts.AdapterContracts.OperationResponses;
using MagicCarpetContracts.BindingModels;
using MagicCarpetContracts.BuisnessLogicContracts;
using MagicCarpetContracts.DataModels;
using MagicCarpetContracts.Exceptions;
using MagicCarpetContracts.ViewModels;
namespace MagicCarpetWebApi.Adapters;
public class ClientAdapter : IClientAdapter
{
private readonly IClientBusinessLogicContract _clientBusinessLogicContract;
private readonly ILogger _logger;
private readonly Mapper _mapper;
public ClientAdapter(IClientBusinessLogicContract clientBusinessLogicContract, ILogger<ClientAdapter> logger)
{
_clientBusinessLogicContract = clientBusinessLogicContract;
_logger = logger;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<ClientBindingModel, ClientDataModel>();
cfg.CreateMap<ClientDataModel, ClientViewModel>();
});
_mapper = new Mapper(config);
}
public ClientOperationResponse GetList()
{
try
{
return ClientOperationResponse.OK([.. _clientBusinessLogicContract.GetAllClients().Select(x => _mapper.Map<ClientViewModel>(x))]);
}
catch (NullListException)
{
_logger.LogError("NullListException");
return ClientOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return ClientOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return ClientOperationResponse.InternalServerError(ex.Message);
}
}
public ClientOperationResponse GetElement(string data)
{
try
{
return ClientOperationResponse.OK(_mapper.Map<ClientViewModel>(_clientBusinessLogicContract.GetClientByData(data)));
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return ClientOperationResponse.BadRequest("Data is empty");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return ClientOperationResponse.NotFound($"Not found element by data {data}");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return ClientOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return ClientOperationResponse.InternalServerError(ex.Message);
}
}
public ClientOperationResponse RegisterClient(ClientBindingModel clientModel)
{
try
{
_clientBusinessLogicContract.InsertClient(_mapper.Map<ClientDataModel>(clientModel));
return ClientOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return ClientOperationResponse.BadRequest("Data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return ClientOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementExistsException ex)
{
_logger.LogError(ex, "ElementExistsException");
return ClientOperationResponse.BadRequest(ex.Message);
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return ClientOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return ClientOperationResponse.InternalServerError(ex.Message);
}
}
public ClientOperationResponse ChangeClientInfo(ClientBindingModel clientModel)
{
try
{
_clientBusinessLogicContract.UpdateClient(_mapper.Map<ClientDataModel>(clientModel));
return ClientOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return ClientOperationResponse.BadRequest("Data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return ClientOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return ClientOperationResponse.BadRequest($"Not found element by Id {clientModel.Id}");
}
catch (ElementExistsException ex)
{
_logger.LogError(ex, "ElementExistsException");
return ClientOperationResponse.BadRequest(ex.Message);
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return ClientOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return ClientOperationResponse.InternalServerError(ex.Message);
}
}
public ClientOperationResponse RemoveClient(string id)
{
try
{
_clientBusinessLogicContract.DeleteClient(id);
return ClientOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return ClientOperationResponse.BadRequest("Id is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return ClientOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return ClientOperationResponse.BadRequest($"Not found element by id: {id}");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return ClientOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return ClientOperationResponse.InternalServerError(ex.Message);
}
}
}

View File

@@ -0,0 +1,278 @@
using AutoMapper;
using MagicCarpetContracts.AdapterContracts;
using MagicCarpetContracts.AdapterContracts.OperationResponses;
using MagicCarpetContracts.BindingModels;
using MagicCarpetContracts.BuisnessLogicContracts;
using MagicCarpetContracts.DataModels;
using MagicCarpetContracts.Exceptions;
using MagicCarpetContracts.ViewModels;
namespace MagicCarpetWebApi.Adapters;
public class EmployeeAdapter : IEmployeeAdapter
{
private readonly IEmployeeBusinessLogicContract _employeeBusinessLogicContract;
private readonly ILogger _logger;
private readonly Mapper _mapper;
public EmployeeAdapter(IEmployeeBusinessLogicContract employeeBusinessLogicContract, ILogger<EmployeeAdapter> logger)
{
_employeeBusinessLogicContract = employeeBusinessLogicContract;
_logger = logger;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<EmployeeBindingModel, EmployeeDataModel>();
cfg.CreateMap<EmployeeDataModel, EmployeeViewModel>();
});
_mapper = new Mapper(config);
}
public EmployeeOperationResponse GetList(bool includeDeleted)
{
try
{
return EmployeeOperationResponse.OK([.. _employeeBusinessLogicContract.GetAllEmployees(!includeDeleted).Select(x => _mapper.Map<EmployeeViewModel>(x))]);
}
catch (NullListException)
{
_logger.LogError("NullListException");
return EmployeeOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return EmployeeOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return EmployeeOperationResponse.InternalServerError(ex.Message);
}
}
public EmployeeOperationResponse GetPostList(string id, bool includeDeleted)
{
try
{
return EmployeeOperationResponse.OK([.. _employeeBusinessLogicContract.GetAllEmployeesByPost(id, !includeDeleted).Select(x => _mapper.Map<EmployeeViewModel>(x))]);
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return EmployeeOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (NullListException)
{
_logger.LogError("NullListException");
return EmployeeOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return EmployeeOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return EmployeeOperationResponse.InternalServerError(ex.Message);
}
}
public EmployeeOperationResponse GetListByBirthDate(DateTime fromDate, DateTime toDate, bool includeDeleted)
{
try
{
return EmployeeOperationResponse.OK([.. _employeeBusinessLogicContract.GetAllEmployeesByBirthDate(fromDate, toDate, !includeDeleted).Select(x => _mapper.Map<EmployeeViewModel>(x))]);
}
catch (IncorrectDatesException ex)
{
_logger.LogError(ex, "IncorrectDatesException");
return EmployeeOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
}
catch (NullListException)
{
_logger.LogError("NullListException");
return EmployeeOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return EmployeeOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return EmployeeOperationResponse.InternalServerError(ex.Message);
}
}
public EmployeeOperationResponse GetListByEmploymentDate(DateTime fromDate, DateTime toDate, bool includeDeleted)
{
try
{
return EmployeeOperationResponse.OK([.. _employeeBusinessLogicContract.GetAllEmployeesByEmploymentDate(fromDate, toDate, !includeDeleted).Select(_mapper.Map<EmployeeViewModel>)]);
}
catch (IncorrectDatesException ex)
{
_logger.LogError(ex, "IncorrectDatesException");
return EmployeeOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
}
catch (NullListException)
{
_logger.LogError("NullListException");
return EmployeeOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return EmployeeOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return EmployeeOperationResponse.InternalServerError(ex.Message);
}
}
public EmployeeOperationResponse GetElement(string data)
{
try
{
return EmployeeOperationResponse.OK(_mapper.Map<EmployeeViewModel>(_employeeBusinessLogicContract.GetEmployeeByData(data)));
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return EmployeeOperationResponse.BadRequest("Data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return EmployeeOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return EmployeeOperationResponse.NotFound($"Not found element by data {data}");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return EmployeeOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return EmployeeOperationResponse.InternalServerError(ex.Message);
}
}
public EmployeeOperationResponse RegisterEmployee(EmployeeBindingModel employeeModel)
{
try
{
_employeeBusinessLogicContract.InsertEmployee(_mapper.Map<EmployeeDataModel>(employeeModel));
return EmployeeOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return EmployeeOperationResponse.BadRequest("Data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return EmployeeOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementExistsException ex)
{
_logger.LogError(ex, "ElementExistsException");
return EmployeeOperationResponse.BadRequest(ex.Message);
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return EmployeeOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return EmployeeOperationResponse.InternalServerError(ex.Message);
}
}
public EmployeeOperationResponse ChangeEmployeeInfo(EmployeeBindingModel employeeModel)
{
try
{
_employeeBusinessLogicContract.UpdateEmployee(_mapper.Map<EmployeeDataModel>(employeeModel));
return EmployeeOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return EmployeeOperationResponse.BadRequest("Data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return EmployeeOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return EmployeeOperationResponse.BadRequest($"Not found element by Id {employeeModel.Id}");
}
catch (ElementExistsException ex)
{
_logger.LogError(ex, "ElementExistsException");
return EmployeeOperationResponse.BadRequest(ex.Message);
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return EmployeeOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return EmployeeOperationResponse.InternalServerError(ex.Message);
}
}
public EmployeeOperationResponse RemoveEmployee(string id)
{
try
{
_employeeBusinessLogicContract.DeleteEmployee(id);
return EmployeeOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return EmployeeOperationResponse.BadRequest("Id is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return EmployeeOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return EmployeeOperationResponse.BadRequest($"Not found element by id: {id}");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return EmployeeOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return EmployeeOperationResponse.InternalServerError(ex.Message);
}
}
}

View File

@@ -0,0 +1,266 @@
using AutoMapper;
using MagicCarpetContracts.AdapterContracts;
using MagicCarpetContracts.AdapterContracts.OperationResponses;
using MagicCarpetContracts.BindingModels;
using MagicCarpetContracts.BuisnessLogicContracts;
using MagicCarpetContracts.DataModels;
using MagicCarpetContracts.Exceptions;
using MagicCarpetContracts.ViewModels;
namespace MagicCarpetWebApi.Adapters;
public class PostAdapter : IPostAdapter
{
private readonly IPostBusinessLogicContract _postBusinessLogicContract;
private readonly ILogger _logger;
private readonly Mapper _mapper;
public PostAdapter(IPostBusinessLogicContract postBusinessLogicContract, ILogger<PostAdapter> logger)
{
_postBusinessLogicContract = postBusinessLogicContract;
_logger = logger;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<PostBindingModel, PostDataModel>();
cfg.CreateMap<PostDataModel, PostViewModel>();
});
_mapper = new Mapper(config);
}
public PostOperationResponse GetList()
{
try
{
return PostOperationResponse.OK([.. _postBusinessLogicContract.GetAllPosts().Select(x => _mapper.Map<PostViewModel>(x))]);
}
catch (NullListException)
{
_logger.LogError("NullListException");
return PostOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return PostOperationResponse.InternalServerError($"Error while working with data storage: {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 => _mapper.Map<PostViewModel>(x))]);
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return PostOperationResponse.BadRequest("Data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return PostOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return PostOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return PostOperationResponse.InternalServerError(ex.Message);
}
}
public PostOperationResponse GetElement(string data)
{
try
{
return PostOperationResponse.OK(_mapper.Map<PostViewModel>(_postBusinessLogicContract.GetPostByData(data)));
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return PostOperationResponse.BadRequest("Data is empty");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return PostOperationResponse.NotFound($"Not found element by data {data}");
}
catch (ElementDeletedException ex)
{
_logger.LogError(ex, "ElementDeletedException");
return PostOperationResponse.BadRequest($"Element by data: {data} was deleted");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return PostOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return PostOperationResponse.InternalServerError(ex.Message);
}
}
public PostOperationResponse RegisterPost(PostBindingModel postModel)
{
try
{
_postBusinessLogicContract.InsertPost(_mapper.Map<PostDataModel>(postModel));
return PostOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return PostOperationResponse.BadRequest("Data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return PostOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementExistsException ex)
{
_logger.LogError(ex, "ElementExistsException");
return PostOperationResponse.BadRequest(ex.Message);
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return PostOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return PostOperationResponse.InternalServerError(ex.Message);
}
}
public PostOperationResponse ChangePostInfo(PostBindingModel postModel)
{
try
{
_postBusinessLogicContract.UpdatePost(_mapper.Map<PostDataModel>(postModel));
return PostOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return PostOperationResponse.BadRequest("Data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return PostOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return PostOperationResponse.BadRequest($"Not found element by Id {postModel.Id}");
}
catch (ElementExistsException ex)
{
_logger.LogError(ex, "ElementExistsException");
return PostOperationResponse.BadRequest(ex.Message);
}
catch (ElementDeletedException ex)
{
_logger.LogError(ex, "ElementDeletedException");
return PostOperationResponse.BadRequest($"Element by id: {postModel.Id} was deleted");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return PostOperationResponse.BadRequest($"Error while working with data storage: {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("Id is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return PostOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return PostOperationResponse.BadRequest($"Not found element by id: {id}");
}
catch (ElementDeletedException ex)
{
_logger.LogError(ex, "ElementDeletedException");
return PostOperationResponse.BadRequest($"Element by id: {id} was deleted");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return PostOperationResponse.BadRequest($"Error while working with data storage: {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("Id is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return PostOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return PostOperationResponse.BadRequest($"Not found element by id: {id}");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return PostOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return PostOperationResponse.InternalServerError(ex.Message);
}
}
}

View File

@@ -0,0 +1,262 @@
using AutoMapper;
using MagicCarpetContracts.AdapterContracts;
using MagicCarpetContracts.AdapterContracts.OperationResponses;
using MagicCarpetContracts.BindingModels;
using MagicCarpetContracts.BuisnessLogicContracts;
using MagicCarpetContracts.DataModels;
using MagicCarpetContracts.Exceptions;
using MagicCarpetContracts.ViewModels;
namespace MagicCarpetWebApi.Adapters;
public class SaleAdapter : ISaleAdapter
{
private readonly ISaleBusinessLogicContract _saleBusinessLogicContract;
private readonly ILogger _logger;
private readonly Mapper _mapper;
public SaleAdapter(ISaleBusinessLogicContract saleBusinessLogicContract, ILogger<SaleAdapter> logger)
{
_saleBusinessLogicContract = saleBusinessLogicContract;
_logger = logger;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<SaleBindingModel, SaleDataModel>();
cfg.CreateMap<SaleDataModel, SaleViewModel>();
cfg.CreateMap<SaleTourBindingModel, SaleTourDataModel>();
cfg.CreateMap<SaleTourDataModel, SaleTourViewModel>();
});
_mapper = new Mapper(config);
}
public SaleOperationResponse GetList(DateTime fromDate, DateTime toDate)
{
try
{
return SaleOperationResponse.OK([.. _saleBusinessLogicContract.GetAllSalesByPeriod(fromDate, toDate).Select(x => _mapper.Map<SaleViewModel>(x))]);
}
catch (IncorrectDatesException ex)
{
_logger.LogError(ex, "IncorrectDatesException");
return SaleOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
}
catch (NullListException)
{
_logger.LogError("NullListException");
return SaleOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return SaleOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return SaleOperationResponse.InternalServerError(ex.Message);
}
}
public SaleOperationResponse GetEmployeeList(string id, DateTime fromDate, DateTime toDate)
{
try
{
return SaleOperationResponse.OK([.. _saleBusinessLogicContract.GetAllSalesByEmployeeByPeriod(id, fromDate, toDate).Select(x => _mapper.Map<SaleViewModel>(x))]);
}
catch (IncorrectDatesException ex)
{
_logger.LogError(ex, "IncorrectDatesException");
return SaleOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return SaleOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (NullListException)
{
_logger.LogError("NullListException");
return SaleOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return SaleOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return SaleOperationResponse.InternalServerError(ex.Message);
}
}
public SaleOperationResponse GetClientList(string id, DateTime fromDate, DateTime toDate)
{
try
{
return SaleOperationResponse.OK([.. _saleBusinessLogicContract.GetAllSalesByClientByPeriod(id, fromDate, toDate).Select(x => _mapper.Map<SaleViewModel>(x))]);
}
catch (IncorrectDatesException ex)
{
_logger.LogError(ex, "IncorrectDatesException");
return SaleOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return SaleOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (NullListException)
{
_logger.LogError("NullListException");
return SaleOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return SaleOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return SaleOperationResponse.InternalServerError(ex.Message);
}
}
public SaleOperationResponse GetCocktailList(string id, DateTime fromDate, DateTime toDate)
{
try
{
return SaleOperationResponse.OK([.. _saleBusinessLogicContract.GetAllSalesByTourByPeriod(id, fromDate, toDate).Select(x => _mapper.Map<SaleViewModel>(x))]);
}
catch (IncorrectDatesException ex)
{
_logger.LogError(ex, "IncorrectDatesException");
return SaleOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return SaleOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (NullListException)
{
_logger.LogError("NullListException");
return SaleOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return SaleOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return SaleOperationResponse.InternalServerError(ex.Message);
}
}
public SaleOperationResponse GetElement(string id)
{
try
{
return SaleOperationResponse.OK(_mapper.Map<SaleViewModel>(_saleBusinessLogicContract.GetSaleByData(id)));
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return SaleOperationResponse.BadRequest("Data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return SaleOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return SaleOperationResponse.NotFound($"Not found element by data {id}");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return SaleOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return SaleOperationResponse.InternalServerError(ex.Message);
}
}
public SaleOperationResponse MakeSale(SaleBindingModel saleModel)
{
try
{
var data = _mapper.Map<SaleDataModel>(saleModel);
_saleBusinessLogicContract.InsertSale(_mapper.Map<SaleDataModel>(saleModel));
return SaleOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return SaleOperationResponse.BadRequest("Data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return SaleOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return SaleOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return SaleOperationResponse.InternalServerError(ex.Message);
}
}
public SaleOperationResponse CancelSale(string id)
{
try
{
_saleBusinessLogicContract.CancelSale(id);
return SaleOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return SaleOperationResponse.BadRequest("Id is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return SaleOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return SaleOperationResponse.BadRequest($"Not found element by id: {id}");
}
catch (ElementDeletedException ex)
{
_logger.LogError(ex, "ElementDeletedException");
return SaleOperationResponse.BadRequest($"Element by id: {id} was deleted");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return SaleOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return SaleOperationResponse.InternalServerError(ex.Message);
}
}
}

View File

@@ -0,0 +1,233 @@
using AutoMapper;
using MagicCarpetContracts.AdapterContracts;
using MagicCarpetContracts.AdapterContracts.OperationResponses;
using MagicCarpetContracts.BindingModels;
using MagicCarpetContracts.BuisnessLogicContracts;
using MagicCarpetContracts.DataModels;
using MagicCarpetContracts.Exceptions;
using MagicCarpetContracts.ViewModels;
namespace MagicCarpetWebApi.Adapters;
public class TourAdapter : ITourAdapter
{
private readonly ITourBusinessLogicContract _tourBusinessLogicContract;
private readonly ILogger _logger;
private readonly Mapper _mapper;
public TourAdapter(ITourBusinessLogicContract tourBusinessLogicContract, ILogger<TourAdapter> logger)
{
_tourBusinessLogicContract = tourBusinessLogicContract;
_logger = logger;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<TourBindingModel, TourDataModel>();
cfg.CreateMap<TourDataModel, TourViewModel>();
cfg.CreateMap<TourHistoryDataModel, TourHistoryViewModel>();
});
_mapper = new Mapper(config);
}
public TourOperationResponse GetList(bool includeDeleted)
{
try
{
return TourOperationResponse.OK([.. _tourBusinessLogicContract.GetAllTours().Select(_mapper.Map<TourViewModel>)]);
}
catch (NullListException)
{
_logger.LogError("NullListException");
return TourOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return TourOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return TourOperationResponse.InternalServerError(ex.Message);
}
}
public TourOperationResponse GetHistory(string id)
{
try
{
return TourOperationResponse.OK([.. _tourBusinessLogicContract.GetTourHistoryByTour(id).Select(x => _mapper.Map<TourHistoryViewModel>(x))]);
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return TourOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (NullListException)
{
_logger.LogError("NullListException");
return TourOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return TourOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return TourOperationResponse.InternalServerError(ex.Message);
}
}
public TourOperationResponse GetElement(string data)
{
try
{
return TourOperationResponse.OK(_mapper.Map<TourViewModel>(_tourBusinessLogicContract.GetTourByData(data)));
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return TourOperationResponse.BadRequest("Data is empty");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return TourOperationResponse.NotFound($"Not found element by data {data}");
}
catch (ElementDeletedException ex)
{
_logger.LogError(ex, "ElementDeletedException");
return TourOperationResponse.BadRequest($"Element by data: {data} was deleted");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return TourOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return TourOperationResponse.InternalServerError(ex.Message);
}
}
public TourOperationResponse RegisterTour(TourBindingModel tourModel)
{
try
{
_tourBusinessLogicContract.InsertTour(_mapper.Map<TourDataModel>(tourModel));
return TourOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return TourOperationResponse.BadRequest("Data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return TourOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementExistsException ex)
{
_logger.LogError(ex, "ElementExistsException");
return TourOperationResponse.BadRequest(ex.Message);
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return TourOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return TourOperationResponse.InternalServerError(ex.Message);
}
}
public TourOperationResponse ChangeTourInfo(TourBindingModel tourModel)
{
try
{
_tourBusinessLogicContract.UpdateTour(_mapper.Map<TourDataModel>(tourModel));
return TourOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return TourOperationResponse.BadRequest("Data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return TourOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return TourOperationResponse.BadRequest($"Not found element by Id {tourModel.Id}");
}
catch (ElementExistsException ex)
{
_logger.LogError(ex, "ElementExistsException");
return TourOperationResponse.BadRequest(ex.Message);
}
catch (ElementDeletedException ex)
{
_logger.LogError(ex, "ElementDeletedException");
return TourOperationResponse.BadRequest($"Element by id: {tourModel.Id} was deleted");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return TourOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return TourOperationResponse.InternalServerError(ex.Message);
}
}
public TourOperationResponse RemoveTour(string id)
{
try
{
_tourBusinessLogicContract.DeleteTour(id);
return TourOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return TourOperationResponse.BadRequest("Id is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return TourOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return TourOperationResponse.BadRequest($"Not found element by id: {id}");
}
catch (ElementDeletedException ex)
{
_logger.LogError(ex, "ElementDeletedException");
return TourOperationResponse.BadRequest($"Element by id: {id} was deleted");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return TourOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return TourOperationResponse.InternalServerError(ex.Message);
}
}
}