132 lines
4.3 KiB
C#
132 lines
4.3 KiB
C#
using AutoMapper;
|
|
using Microsoft.Extensions.Logging;
|
|
using YAPContracts.BindingModels;
|
|
using YAPContracts.DataModels;
|
|
using YAPContracts.ViewModels;
|
|
using YAPContracts.BusinessLogicContracts;
|
|
using YAPContracts.Exceptions;
|
|
using YAPContracts.AdapterContracts;
|
|
|
|
namespace YAPWebApplication.Adapters
|
|
{
|
|
public class PurchaseAdapter : IPurchaseAdapter
|
|
{
|
|
private readonly IPurchaseBusinessLogicContract _purchaseBL;
|
|
private readonly ILogger _logger;
|
|
private readonly Mapper _mapper;
|
|
public PurchaseAdapter(IPurchaseBusinessLogicContract purchaseBL, ILogger logger)
|
|
{
|
|
_purchaseBL = purchaseBL;
|
|
_logger = logger;
|
|
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<PurchaseBindingModel, PurchaseDataModel>();
|
|
cfg.CreateMap<PurchaseDataModel, PurchaseViewModel>();
|
|
cfg.CreateMap<PurchaseViewModel, PurchaseDataModel>();
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
|
|
public List<PurchaseViewModel> GetList()
|
|
{
|
|
try
|
|
{
|
|
var purchases = _purchaseBL.GetAllPurchases();
|
|
return purchases.Select(x => _mapper.Map<PurchaseViewModel>(x)).ToList();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error in Purchase GetList");
|
|
throw new Exception("Error retrieving purchases list", ex);
|
|
}
|
|
|
|
}
|
|
|
|
public PurchaseViewModel? GetElement(string id)
|
|
{
|
|
try
|
|
{
|
|
var purchase = _purchaseBL.GetPurchaseByData(id);
|
|
return purchase != null ? _mapper.Map<PurchaseViewModel>(purchase) : null;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error in Purchase GetElement");
|
|
throw new Exception("Error retrieving purchase", ex);
|
|
}
|
|
}
|
|
|
|
public void Register(PurchaseBindingModel model)
|
|
{
|
|
try
|
|
{
|
|
var dataModel = _mapper.Map<PurchaseDataModel>(model);
|
|
_purchaseBL.InsertPurchase(dataModel);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error in Purchase Register");
|
|
throw new Exception("Error creating purchase", ex);
|
|
}
|
|
}
|
|
|
|
public void Update(PurchaseBindingModel model)
|
|
{
|
|
try
|
|
{
|
|
var dataModel = _mapper.Map<PurchaseDataModel>(model);
|
|
_purchaseBL.UpdatePurchase(dataModel);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error in Purchase Update");
|
|
throw new Exception("Error updating purchase", ex);
|
|
}
|
|
}
|
|
|
|
public void Delete(string id)
|
|
{
|
|
try
|
|
{
|
|
_purchaseBL.DeletePurchase(id);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error in Purchase Delete");
|
|
throw new Exception("Error deleting purchase", ex);
|
|
}
|
|
}
|
|
|
|
public List<PurchaseViewModel> GetByUserAndPeriod(string userId, DateTime fromDate, DateTime toDate)
|
|
{
|
|
try
|
|
{
|
|
var purchases = _purchaseBL.GetPurchasesByUserByPeriod(userId, fromDate, toDate);
|
|
return purchases.Select(x => _mapper.Map<PurchaseViewModel>(x)).ToList();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error in Purchase GetByUserAndPeriod");
|
|
throw new Exception("Error retrieving purchases list by user and period", ex);
|
|
}
|
|
}
|
|
|
|
public List<PurchaseViewModel> GetByPeriod(DateTime fromDate, DateTime toDate)
|
|
{
|
|
try
|
|
{
|
|
var purchases = _purchaseBL.GetPurchasesByPeriod(fromDate, toDate);
|
|
return purchases.Select(x => _mapper.Map<PurchaseViewModel>(x)).ToList();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error in Purchase GetByPeriod");
|
|
throw new Exception("Error retrieving purchases list by period", ex);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|