Files
FA_WEB/FaKrosnoEfDataModel/Services/ProductService.cs
Piotr Kus f58d2ab04c * Further improvements of Warehouse view
* Fixed issue with not saving Products updates
2025-05-24 21:47:32 +02:00

59 lines
2.0 KiB
C#

using AutoMapper;
using FaKrosnoEfDataModel.Dtos;
using FaKrosnoEfDataModel.Entities;
using Microsoft.EntityFrameworkCore;
namespace FaKrosnoEfDataModel.Services;
public class ProductService(FaKrosnoDbContext context, IMapper mapper)
: ServiceBase<ProductDto>(context, mapper), IProductService
{
public async Task<IEnumerable<ProductDto?>> GetEntities()
{
IList<ProductDto> products = (await GetAll()).ToList();
return products;
}
public async Task<IEnumerable<ProductDto?>> GetEntitiesToFix(string indexName)
{
IList<RecipientDto> recipients =
(await Context.Recipients.ToListAsync()).Select(x => Mapper.Map<RecipientDto>(x)).ToList();
IList<PurchaserDto> purchasers = (await Context.Purchasers.ToListAsync()).Select(x => Mapper.Map<PurchaserDto>(x)).ToList();
IList<ProductDto> products = (await GetAll()).ToList();
IEnumerable<ProductDto> productDtos = products.Where(x => x.FaIdx == indexName).ToList();
foreach (ProductDto productDto in productDtos)
{
RecipientDto? recipient = recipients.FirstOrDefault(x => x.ID == productDto.RecipientID);
if (recipient != null)
{
productDto.Recipient = recipient;
productDto.RecipientName = recipient.RecipientDesc;
}
PurchaserDto? purchaser = purchasers.FirstOrDefault(x => x.ID == productDto.Recipient.PurchaserID);
if (purchaser != null)
{
productDto.Recipient.Purchaser = purchaser;
}
}
return productDtos;
}
public async Task UpdateEntity(ProductDto entity)
{
Product? product = await Context.Products.FirstOrDefaultAsync(x => x.ID == entity.ID);
if (product != null)
{
product.FaIdx = entity.FaIdx;
Context.Products.Update(product);
await Context.SaveChangesAsync();
}
}
}