Files
PIBD-23_Coursach_YouAreProg…/YouAreProgrammerShop/YAPDatabase/Implementations/ComponentStorageContract.cs
2025-10-02 19:41:13 +04:00

165 lines
5.4 KiB
C#

using AutoMapper;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YAPContracts.DataModels;
using YAPContracts.Exceptions;
using YAPContracts.StorageContracts;
using YAPDatabase.Models;
namespace YAPDatabase.Implementations;
internal class ComponentStorageContract : IComponentStorageContract
{
private readonly YAPDbContext _dbContext;
private readonly Mapper _mapper;
public ComponentStorageContract(YAPDbContext dbContext)
{
_dbContext = dbContext;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<ComponentInProduct, ComponentInProductDataModel>();
cfg.CreateMap<ComponentInProductDataModel, ComponentInProduct>();
cfg.CreateMap<ComponentInProductSet, ComponentInProductSetDataModel>();
cfg.CreateMap<ComponentInProductSetDataModel, ComponentInProductSet>();
cfg.CreateMap<ComponentDataModel, Component>();
cfg.CreateMap<Component, ComponentDataModel>();
cfg.CreateMap<Purchase, PurchaseDataModel>() //Для отчета
.ForMember(dest => dest.Products, opt => opt.MapFrom(src => src.ProductsInPurchase))
.ForMember(dest => dest.ProductSets, opt => opt.MapFrom(src => src.ProductSetsInPurchase));;
cfg.CreateMap<ProductOrder, ProductOrderDataModel>(); //Для отчета
});
_mapper = new Mapper(config);
}
public void AddElement(ComponentDataModel component)
{
try
{
var entity = _mapper.Map<Component>(component);
_dbContext.Components.Add(entity);
_dbContext.SaveChanges();
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
}
}
public void DelElement(string id)
{
try
{
var component = GetComponentById(id) ?? throw new ElementNotFoundException(id);
_dbContext.Components.Remove(component);
_dbContext.SaveChanges();
}
catch (ElementNotFoundException ex)
{
_dbContext.ChangeTracker.Clear();
throw;
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
}
}
public ComponentDataModel? GetElementById(string id)
{
try
{
return _mapper.Map<ComponentDataModel>(GetComponentById(id));
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
}
}
public ComponentDataModel? GetElementByName(string name)
{
try
{
return _mapper.Map<ComponentDataModel>(_dbContext.Components.Include(x => x.ProductSets).FirstOrDefault(x => x.Name == name));
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
}
}
public List<ComponentDataModel> GetList(bool onlyActual = true)
{
try
{
var query = _dbContext.Components.Include(x => x.Products).Include(x => x.ProductSets).AsQueryable();
return [.. query.Select(x => _mapper.Map<ComponentDataModel>(x))];
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
}
}
public void UpdElement(ComponentDataModel component)
{
try
{
var newComponent = GetComponentById(component.Id) ?? throw new ElementNotFoundException(component.Id);
_dbContext.Components.Update(_mapper.Map(component, newComponent));
_dbContext.SaveChanges();
}
catch (ElementNotFoundException ex)
{
_dbContext.ChangeTracker.Clear();
throw;
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
}
}
private Component? GetComponentById(string id) => _dbContext.Components.Include(x => x.ProductSets).FirstOrDefault(x => x.Id == id);
public List<ComponentReportModel>? GetDataForReport(DateTime start, DateTime finish)
{
try
{
var data = _dbContext.Components
.Select(c => new ComponentReportModel
{
Component = _mapper.Map<ComponentDataModel>(c),
Purchases = c.Products
.SelectMany(cp => cp.Product.ProductsInPurchace).Where(pip => pip.Purchase.PurchaseDate >= start && pip.Purchase.PurchaseDate <= finish)
.Select(pip => _mapper.Map<PurchaseDataModel>(pip.Purchase))
.ToList(),
Orders = c.Products
.SelectMany(p => p.Product.ProductOrders).Where(po => po.OrderDate >= start && po.OrderDate <= finish)
.Select(ord => _mapper.Map<ProductOrderDataModel>(ord))
.ToList()
})
.ToList();
return data;
}
catch (Exception ex)
{
_dbContext.ChangeTracker.Clear();
throw new StorageException(ex);
}
}
}