63 lines
1.8 KiB
C#
63 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using AutoMapper;
|
|
using PuferFishContracts.DataModels;
|
|
using PuferFishContracts.Exceptions;
|
|
using PuferFishContracts.StoragesContracts;
|
|
using PuferFishDataBase.Models;
|
|
|
|
namespace PuferFishDataBase.Implementations;
|
|
|
|
internal class PointsStorageContract : IPointsStorageContract
|
|
{
|
|
private readonly PuferFishDbContext _dbContext;
|
|
private readonly Mapper _mapper;
|
|
|
|
public PointsStorageContract(PuferFishDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<Points, PointsDataModel>();
|
|
cfg.CreateMap<PointsDataModel, Points>()
|
|
.ForMember(dest => dest.BuyerPoints, opt =>
|
|
opt.MapFrom(src => src.Points));
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
public List<PointsDataModel> GetList(DateTime startDate, DateTime endDate, string? buyerId = null)
|
|
{
|
|
try
|
|
{
|
|
var query = _dbContext.Pointss.Where(x => x.PointsDate >=
|
|
startDate && x.PointsDate <= endDate);
|
|
if (buyerId is not null)
|
|
{
|
|
query = query.Where(x => x.BuyerId == buyerId);
|
|
}
|
|
return [.. query.Select(x => _mapper.Map<PointsDataModel>(x))];
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
public void AddElement(PointsDataModel salaryDataModel)
|
|
{
|
|
try
|
|
{
|
|
_dbContext.Pointss.Add(_mapper.Map<Points>(salaryDataModel));
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
}
|