forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
63 lines
1.9 KiB
C#
63 lines
1.9 KiB
C#
using AutoMapper;
|
|
using MagicCarpetContracts.DataModels;
|
|
using MagicCarpetContracts.Exceptions;
|
|
using MagicCarpetContracts.StoragesContracts;
|
|
using MagicCarpetDatabase.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MagicCarpetDatabase.Implementations;
|
|
|
|
internal class SalaryStorageContract : ISalaryStorageContract
|
|
{
|
|
private readonly MagicCarpetDbContext _dbContext;
|
|
private readonly Mapper _mapper;
|
|
|
|
public SalaryStorageContract(MagicCarpetDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<Salary, SalaryDataModel>();
|
|
cfg.CreateMap<SalaryDataModel, Salary>()
|
|
.ForMember(dest => dest.EmployeeSalary, opt => opt.MapFrom(src => src.Salary));
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
|
|
public List<SalaryDataModel> GetList(DateTime startDate, DateTime endDate, string? employeeId = null)
|
|
{
|
|
try
|
|
{
|
|
var query = _dbContext.Salaries.Where(x => x.SalaryDate >= startDate && x.SalaryDate <= endDate);
|
|
if (employeeId is not null)
|
|
{
|
|
query = query.Where(x => x.EmployeeId == employeeId);
|
|
}
|
|
return [.. query.Select(x => _mapper.Map<SalaryDataModel>(x))];
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|