169 lines
6.0 KiB
C#
169 lines
6.0 KiB
C#
namespace YAPWebAPI.Adapters
|
|
{
|
|
using AutoMapper;
|
|
using Microsoft.Extensions.Logging;
|
|
using YAPContracts.BindingModels;
|
|
using YAPContracts.DataModels;
|
|
using YAPContracts.ViewModels;
|
|
using YAPContracts.BusinessLogicContracts;
|
|
using YAPContracts.Exceptions;
|
|
using YAPContracts.AdapterContracts.OperationResponses;
|
|
using YAPContracts.AdapterContracts;
|
|
|
|
public class PurchaseAdapter : IPurchaseAdapter
|
|
{
|
|
private readonly IPurchaseBusinessLogicContract _purchaseBusinessLogicContract;
|
|
private readonly ILogger _logger;
|
|
private readonly Mapper _mapper;
|
|
|
|
public PurchaseAdapter(IPurchaseBusinessLogicContract purchaseBusinessLogicContract, ILogger logger)
|
|
{
|
|
_purchaseBusinessLogicContract = purchaseBusinessLogicContract;
|
|
_logger = logger;
|
|
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<PurchaseBindingModel, PurchaseDataModel>();
|
|
cfg.CreateMap<PurchaseDataModel, PurchaseViewModel>();
|
|
cfg.CreateMap<PurchaseViewModel, PurchaseDataModel>();
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
|
|
public PurchaseOperationResponse GetList()
|
|
{
|
|
try
|
|
{
|
|
return PurchaseOperationResponse.OK([.. _purchaseBusinessLogicContract.GetAllPurchases()
|
|
.Select(x => _mapper.Map<PurchaseViewModel>(x))]);
|
|
}
|
|
catch (NullListException)
|
|
{
|
|
_logger.LogError("NullListException");
|
|
return PurchaseOperationResponse.NotFound("The list is not initialized");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return PurchaseOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public PurchaseOperationResponse GetElement(string id)
|
|
{
|
|
try
|
|
{
|
|
return PurchaseOperationResponse.OK(
|
|
_mapper.Map<PurchaseViewModel>(_purchaseBusinessLogicContract.GetPurchaseByData(id))
|
|
);
|
|
}
|
|
catch (ArgumentNullException)
|
|
{
|
|
return PurchaseOperationResponse.BadRequest("Id is empty");
|
|
}
|
|
catch (ElementNotFoundException)
|
|
{
|
|
return PurchaseOperationResponse.NotFound($"Not found purchase by Id {id}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return PurchaseOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public PurchaseOperationResponse GetByUserAndPeriod(string userId, DateTime fromDate, DateTime toDate)
|
|
{
|
|
try
|
|
{
|
|
return PurchaseOperationResponse.OK([.. _purchaseBusinessLogicContract
|
|
.GetPurchasesByUserByPeriod(userId, fromDate, toDate)
|
|
.Select(x => _mapper.Map<PurchaseViewModel>(x))]);
|
|
}
|
|
catch (IncorrectDatesException ex)
|
|
{
|
|
return PurchaseOperationResponse.BadRequest($"Incorrect date interval: {ex.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return PurchaseOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public PurchaseOperationResponse GetByPeriod(DateTime fromDate, DateTime toDate)
|
|
{
|
|
try
|
|
{
|
|
return PurchaseOperationResponse.OK([.. _purchaseBusinessLogicContract
|
|
.GetPurchasesByPeriod(fromDate, toDate)
|
|
.Select(x => _mapper.Map<PurchaseViewModel>(x))]);
|
|
}
|
|
catch (IncorrectDatesException ex)
|
|
{
|
|
return PurchaseOperationResponse.BadRequest($"Incorrect date interval: {ex.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return PurchaseOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public PurchaseOperationResponse InsertPurchase(PurchaseBindingModel model)
|
|
{
|
|
try
|
|
{
|
|
_purchaseBusinessLogicContract.InsertPurchase(_mapper.Map<PurchaseDataModel>(model));
|
|
return PurchaseOperationResponse.NoContent();
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
return PurchaseOperationResponse.BadRequest($"Incorrect data: {ex.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return PurchaseOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public PurchaseOperationResponse UpdatePurchase(PurchaseBindingModel model)
|
|
{
|
|
try
|
|
{
|
|
_purchaseBusinessLogicContract.UpdatePurchase(_mapper.Map<PurchaseDataModel>(model));
|
|
return PurchaseOperationResponse.NoContent();
|
|
}
|
|
catch (ElementNotFoundException)
|
|
{
|
|
return PurchaseOperationResponse.NotFound($"Purchase not found by Id {model.Id}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return PurchaseOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public PurchaseOperationResponse DeletePurchase(string id)
|
|
{
|
|
try
|
|
{
|
|
_purchaseBusinessLogicContract.DeletePurchase(id);
|
|
return PurchaseOperationResponse.NoContent();
|
|
}
|
|
catch (ElementNotFoundException)
|
|
{
|
|
return PurchaseOperationResponse.NotFound($"Purchase not found by Id {id}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return PurchaseOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|