58 lines
1.7 KiB
C#
58 lines
1.7 KiB
C#
using AutoMapper;
|
|
using WildPlumContracts.DataModels;
|
|
using WildPlumContracts.Exceptions;
|
|
using WildPlumContracts.StorageContracts;
|
|
using WildPlumDatabase.Models;
|
|
|
|
namespace WildPlumDatabase.Implementations;
|
|
|
|
public class SalaryStorageContract : ISalaryStorageContract
|
|
{
|
|
private readonly WildPlumDbContext _dbContext;
|
|
private readonly Mapper _mapper;
|
|
|
|
public SalaryStorageContract(WildPlumDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<Salary, SalaryDataModel>();
|
|
cfg.CreateMap<SalaryDataModel, Salary>()
|
|
.ForMember(dest => dest.Amount, opt => opt.MapFrom(src => src.Amount));
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
|
|
public void AddElement(SalaryDataModel salaryDataModel)
|
|
{
|
|
try
|
|
{
|
|
_dbContext.Salaries.Add(_mapper.Map<Salary>(salaryDataModel));
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
public List<SalaryDataModel> GetList(DateTime startDate, DateTime endDate, string? workerId = null)
|
|
{
|
|
try
|
|
{
|
|
var query = _dbContext.Salaries.Where(x => x.SalaryDate >= startDate && x.SalaryDate <= endDate);
|
|
if (workerId is not null)
|
|
{
|
|
query = query.Where(x => x.WorkerId == workerId);
|
|
}
|
|
return [.. query.Select(x => _mapper.Map<SalaryDataModel>(x))];
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
}
|