121 lines
3.7 KiB
C#
121 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using AutoMapper;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using PuferFishContracts.DataModels;
|
|
using PuferFishContracts.Exceptions;
|
|
using PuferFishContracts.StoragesContracts;
|
|
using PuferFishDataBase.Models;
|
|
|
|
namespace PuferFishDataBase.Implementations;
|
|
|
|
internal class SaleStorageContract : ISaleStorageContract
|
|
{
|
|
private readonly PuferFishDbContext _dbContext;
|
|
private readonly Mapper _mapper;
|
|
public SaleStorageContract(PuferFishDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<SaleProduct, SaleProductDataModel>();
|
|
cfg.CreateMap<SaleProductDataModel, SaleProduct>();
|
|
cfg.CreateMap<Sale, SaleDataModel>();
|
|
cfg.CreateMap<SaleDataModel, Sale>()
|
|
.ForMember(x => x.IsCancel, x => x.MapFrom(src =>
|
|
false))
|
|
.ForMember(x => x.SaleProducts, x => x.MapFrom(src =>
|
|
src.Products));
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
public List<SaleDataModel> GetList(DateTime? startDate = null, DateTime? endDate = null, string? workerId = null,
|
|
string? buyerId = null, string? productId = null)
|
|
{
|
|
try
|
|
{
|
|
var query = _dbContext.Sales.Include(x =>
|
|
x.SaleProducts).AsQueryable();
|
|
if (startDate is not null && endDate is not null)
|
|
{
|
|
query = query.Where(x => x.SaleDate >= startDate &&
|
|
x.SaleDate < endDate);
|
|
}
|
|
if (workerId is not null)
|
|
{
|
|
query = query.Where(x => x.WorkerId == workerId);
|
|
}
|
|
if (buyerId is not null)
|
|
{
|
|
query = query.Where(x => x.BuyerId == buyerId);
|
|
}
|
|
if (productId is not null)
|
|
{
|
|
query = query.Where(x => x.SaleProducts!.Any(y =>
|
|
y.ProductId == productId));
|
|
}
|
|
return [.. query.Select(x => _mapper.Map<SaleDataModel>(x))];
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
public SaleDataModel? GetElementById(string id)
|
|
{
|
|
try
|
|
{
|
|
return _mapper.Map<SaleDataModel>(GetSaleById(id));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
public void AddElement(SaleDataModel saleDataModel)
|
|
{
|
|
try
|
|
{
|
|
_dbContext.Sales.Add(_mapper.Map<Sale>(saleDataModel));
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
public void DelElement(string id)
|
|
{
|
|
try
|
|
{
|
|
var element = GetSaleById(id) ?? throw new
|
|
ElementNotFoundException(id);
|
|
if (element.IsCancel)
|
|
{
|
|
throw new ElementDeletedException(id);
|
|
}
|
|
element.IsCancel = true;
|
|
_dbContext.SaveChanges();
|
|
}
|
|
catch (Exception ex) when (ex is ElementDeletedException || ex is
|
|
ElementNotFoundException)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_dbContext.ChangeTracker.Clear();
|
|
throw new StorageException(ex);
|
|
}
|
|
}
|
|
private Sale? GetSaleById(string id) => _dbContext.Sales.FirstOrDefault(x
|
|
=> x.Id == id);
|
|
}
|