forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
124 lines
4.2 KiB
C#
124 lines
4.2 KiB
C#
using AutoMapper;
|
|
using MagicCarpetContracts.DataModels;
|
|
using MagicCarpetContracts.Exceptions;
|
|
using MagicCarpetContracts.StoragesContracts;
|
|
using MagicCarpetDatabase.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MagicCarpetDatabase.Implementations;
|
|
|
|
public class SaleStorageContract : ISaleStorageContract
|
|
{
|
|
private readonly MagicCarpetDbContext _dbContext;
|
|
private readonly Mapper _mapper;
|
|
|
|
public SaleStorageContract(MagicCarpetDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<Client, ClientDataModel>();
|
|
cfg.CreateMap<Tour, TourDataModel>();
|
|
cfg.CreateMap<Employee, EmployeeDataModel>();
|
|
cfg.CreateMap<SaleTour, SaleTourDataModel>();
|
|
cfg.CreateMap<SaleTourDataModel, SaleTour>()
|
|
.ForMember(x => x.Tour, x => x.Ignore());
|
|
cfg.CreateMap<Sale, SaleDataModel>();
|
|
cfg.CreateMap<SaleDataModel, Sale>()
|
|
.ForMember(x => x.IsCancel, x => x.MapFrom(src => false))
|
|
.ForMember(x => x.SaleTours, x => x.MapFrom(src => src.Tours))
|
|
.ForMember(x => x.Employee, x => x.Ignore())
|
|
.ForMember(x => x.Client, x => x.Ignore());
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
|
|
public List<SaleDataModel> GetList(DateTime? startDate = null, DateTime? endDate = null, string? employeeId = null, string? clientId = null, string? tourId = null)
|
|
{
|
|
try
|
|
{
|
|
var query = _dbContext.Sales
|
|
.Include(r => r.Employee)
|
|
.Include(r => r.Client)
|
|
.Include(r => r.SaleTours)!
|
|
.ThenInclude(d => d.Tour)
|
|
.AsQueryable();
|
|
if (startDate.HasValue)
|
|
query = query.Where(x => x.SaleDate >= DateTime.SpecifyKind(startDate ?? DateTime.UtcNow, DateTimeKind.Utc));
|
|
if (endDate.HasValue)
|
|
query = query.Where(x => x.SaleDate <= DateTime.SpecifyKind(endDate ?? DateTime.UtcNow, DateTimeKind.Utc));
|
|
if (employeeId != null)
|
|
query = query.Where(x => x.EmployeeId == employeeId);
|
|
if (clientId != null)
|
|
query = query.Where(x => x.ClientId == clientId);
|
|
if (tourId != null)
|
|
query = query.Where(x => x.SaleTours!.Any(y => y.TourId == tourId));
|
|
var s = query.ToList();
|
|
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.Include(x => x.Client).Include(x => x.Employee).Include(x => x.SaleTours)!.ThenInclude(x => x.Tour).FirstOrDefault(x => x.Id == id);
|
|
}
|