categorys

This commit is contained in:
Tizian.Breuch
2025-07-31 15:33:35 +02:00
parent ca1d3fda1d
commit ad7bfeaa40
4 changed files with 97 additions and 45 deletions

View File

@@ -1,28 +1,35 @@
// src/Webshop.Application/Services/Admin/AdminProductService.cs
using Microsoft.EntityFrameworkCore; // << NEU: F<>r Include() und FirstOrDefaultAsync() >>
using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces;
using System.Collections.Generic;
using System.Threading.Tasks;
using System; // F<>r Guid
using System;
using System.Linq;
using Webshop.Application.DTOs.Products;
using Webshop.Application.Services.Admin.Interfaces; // F<>r Select
using Webshop.Application.DTOs.Products; // F<>r AdminProductDto
using Webshop.Application.Services.Admin.Interfaces; // F<>r IAdminProductService
using Webshop.Infrastructure.Data; // << NEU: F<>r ApplicationDbContext >>
namespace Webshop.Application.Services.Admin
{
public class AdminProductService : IAdminProductService // Sicherstellen, dass IAdminProductService implementiert wird
public class AdminProductService : IAdminProductService
{
private readonly IProductRepository _productRepository;
private readonly ApplicationDbContext _context; // << NEU: F<>r direkten DB-Zugriff auf Join-Tabellen >>
public AdminProductService(IProductRepository productRepository)
public AdminProductService(IProductRepository productRepository, ApplicationDbContext context) // << NEU: DbContext injizieren >>
{
_productRepository = productRepository;
_context = context; // << NEU >>
}
public async Task<IEnumerable<AdminProductDto>> GetAllAdminProductsAsync()
{
var products = await _productRepository.GetAllProductsAsync();
// Wir verwenden den DbContext, um auch die Kategorien effizient mitzuladen
var products = await _context.Products
.Include(p => p.ProductCategories)
.ToListAsync();
return products.Select(p => new AdminProductDto
{
Id = p.Id,
@@ -40,13 +47,17 @@ namespace Webshop.Application.Services.Admin
CreatedDate = p.CreatedDate,
LastModifiedDate = p.LastModifiedDate,
SupplierId = p.SupplierId,
PurchasePrice = p.PurchasePrice
PurchasePrice = p.PurchasePrice,
CategoryIds = p.ProductCategories.Select(pc => pc.CategoryId).ToList() // << NEU >>
}).ToList();
}
public async Task<AdminProductDto?> GetAdminProductByIdAsync(Guid id)
{
var product = await _productRepository.GetProductByIdAsync(id);
var product = await _context.Products
.Include(p => p.ProductCategories) // << NEU: Lade die Join-Tabelle mit >>
.FirstOrDefaultAsync(p => p.Id == id);
if (product == null) return null;
return new AdminProductDto
@@ -66,16 +77,16 @@ namespace Webshop.Application.Services.Admin
CreatedDate = product.CreatedDate,
LastModifiedDate = product.LastModifiedDate,
SupplierId = product.SupplierId,
PurchasePrice = product.PurchasePrice
PurchasePrice = product.PurchasePrice,
CategoryIds = product.ProductCategories.Select(pc => pc.CategoryId).ToList() // << NEU: Mappe die CategoryIds >>
};
}
public async Task<AdminProductDto> CreateAdminProductAsync(AdminProductDto productDto)
{
// Konvertiere DTO zu Domain-Entit<69>t
var newProduct = new Product
{
Id = Guid.NewGuid(), // API generiert die ID immer neu
Id = Guid.NewGuid(),
Name = productDto.Name,
Description = productDto.Description,
SKU = productDto.SKU,
@@ -87,26 +98,33 @@ namespace Webshop.Application.Services.Admin
Weight = productDto.Weight,
ImageUrl = productDto.ImageUrl,
Slug = productDto.Slug,
CreatedDate = DateTimeOffset.UtcNow, // Server setzt Erstellungsdatum
LastModifiedDate = null, // Neues Produkt, noch nicht modifiziert
CreatedDate = DateTimeOffset.UtcNow,
SupplierId = productDto.SupplierId,
PurchasePrice = productDto.PurchasePrice
PurchasePrice = productDto.PurchasePrice,
ProductCategories = new List<ProductCategory>() // Initialisiere die Collection
};
await _productRepository.AddProductAsync(newProduct); // F<>gt in DB ein
// << NEU: F<>ge die Kategorien hinzu >>
foreach (var categoryId in productDto.CategoryIds)
{
newProduct.ProductCategories.Add(new ProductCategory { CategoryId = categoryId });
}
await _productRepository.AddProductAsync(newProduct); // << KORREKT: VERWENDET AddProductAsync >>
// Aktualisiere DTO mit der neuen ID und gib es zur<75>ck
productDto.Id = newProduct.Id;
productDto.CreatedDate = newProduct.CreatedDate;
return productDto;
}
public async Task<bool> UpdateAdminProductAsync(AdminProductDto productDto)
{
var existingProduct = await _productRepository.GetProductByIdAsync(productDto.Id);
var existingProduct = await _context.Products
.Include(p => p.ProductCategories) // Lade die aktuellen Zuweisungen
.FirstOrDefaultAsync(p => p.Id == productDto.Id);
if (existingProduct == null) return false;
// Aktualisiere Eigenschaften des bestehenden Produkts
// Aktualisiere die direkten Eigenschaften des Produkts
existingProduct.Name = productDto.Name;
existingProduct.Description = productDto.Description;
existingProduct.SKU = productDto.SKU;
@@ -118,11 +136,19 @@ namespace Webshop.Application.Services.Admin
existingProduct.Weight = productDto.Weight;
existingProduct.ImageUrl = productDto.ImageUrl;
existingProduct.Slug = productDto.Slug;
existingProduct.LastModifiedDate = DateTimeOffset.UtcNow; // Server setzt Modifikationsdatum
existingProduct.SupplierId = productDto.SupplierId;
existingProduct.PurchasePrice = productDto.PurchasePrice;
existingProduct.LastModifiedDate = DateTimeOffset.UtcNow;
await _productRepository.UpdateProductAsync(existingProduct);
// << NEU: Kategorien synchronisieren (alte l<>schen, neue hinzuf<75>gen) >>
existingProduct.ProductCategories.Clear();
foreach (var categoryId in productDto.CategoryIds)
{
existingProduct.ProductCategories.Add(new ProductCategory { ProductId = existingProduct.Id, CategoryId = categoryId });
}
// << ENDE NEUER TEIL >>
await _productRepository.UpdateProductAsync(existingProduct); // << KORREKT: VERWENDET UpdateProductAsync >>
return true;
}
@@ -131,7 +157,7 @@ namespace Webshop.Application.Services.Admin
var product = await _productRepository.GetProductByIdAsync(id);
if (product == null) return false;
await _productRepository.DeleteProductAsync(id);
await _productRepository.DeleteProductAsync(id); // << KORREKT: VERWENDET DeleteProductAsync >>
return true;
}
}