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(); cfg.CreateMap() .ForMember(dest => dest.BuyerPoints, opt => opt.MapFrom(src => src.Points)); }); _mapper = new Mapper(config); } public List 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(x))]; } catch (Exception ex) { _dbContext.ChangeTracker.Clear(); throw new StorageException(ex); } } public void AddElement(PointsDataModel salaryDataModel) { try { _dbContext.Pointss.Add(_mapper.Map(salaryDataModel)); _dbContext.SaveChanges(); } catch (Exception ex) { _dbContext.ChangeTracker.Clear(); throw new StorageException(ex); } } }