categorys
This commit is contained in:
@@ -24,5 +24,6 @@ namespace Webshop.Application.DTOs.Products
|
|||||||
public DateTimeOffset? LastModifiedDate { get; set; }
|
public DateTimeOffset? LastModifiedDate { get; set; }
|
||||||
public Guid? SupplierId { get; set; }
|
public Guid? SupplierId { get; set; }
|
||||||
public decimal? PurchasePrice { get; set; }
|
public decimal? PurchasePrice { get; set; }
|
||||||
|
public List<Guid> CategoryIds { get; set; } = new List<Guid>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Webshop.Application.DTOs.Categorys;
|
||||||
|
|
||||||
|
|
||||||
namespace Webshop.Application.DTOs.Products
|
namespace Webshop.Application.DTOs.Products
|
||||||
@@ -18,5 +19,6 @@ namespace Webshop.Application.DTOs.Products
|
|||||||
public int StockQuantity { get; set; }
|
public int StockQuantity { get; set; }
|
||||||
public string? ImageUrl { get; set; }
|
public string? ImageUrl { get; set; }
|
||||||
public string Slug { get; set; } = string.Empty;
|
public string Slug { get; set; } = string.Empty;
|
||||||
|
public List<CategoryDto> Categories { get; set; } = new List<CategoryDto>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,28 +1,35 @@
|
|||||||
// src/Webshop.Application/Services/Admin/AdminProductService.cs
|
// src/Webshop.Application/Services/Admin/AdminProductService.cs
|
||||||
|
using Microsoft.EntityFrameworkCore; // << NEU: F<>r Include() und FirstOrDefaultAsync() >>
|
||||||
using Webshop.Domain.Entities;
|
using Webshop.Domain.Entities;
|
||||||
using Webshop.Domain.Interfaces;
|
using Webshop.Domain.Interfaces;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System; // F<>r Guid
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Webshop.Application.DTOs.Products;
|
using Webshop.Application.DTOs.Products; // F<>r AdminProductDto
|
||||||
using Webshop.Application.Services.Admin.Interfaces; // F<>r Select
|
using Webshop.Application.Services.Admin.Interfaces; // F<>r IAdminProductService
|
||||||
|
using Webshop.Infrastructure.Data; // << NEU: F<>r ApplicationDbContext >>
|
||||||
|
|
||||||
namespace Webshop.Application.Services.Admin
|
namespace Webshop.Application.Services.Admin
|
||||||
{
|
{
|
||||||
public class AdminProductService : IAdminProductService // Sicherstellen, dass IAdminProductService implementiert wird
|
public class AdminProductService : IAdminProductService
|
||||||
{
|
{
|
||||||
private readonly IProductRepository _productRepository;
|
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;
|
_productRepository = productRepository;
|
||||||
|
_context = context; // << NEU >>
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<AdminProductDto>> GetAllAdminProductsAsync()
|
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
|
return products.Select(p => new AdminProductDto
|
||||||
{
|
{
|
||||||
Id = p.Id,
|
Id = p.Id,
|
||||||
@@ -40,13 +47,17 @@ namespace Webshop.Application.Services.Admin
|
|||||||
CreatedDate = p.CreatedDate,
|
CreatedDate = p.CreatedDate,
|
||||||
LastModifiedDate = p.LastModifiedDate,
|
LastModifiedDate = p.LastModifiedDate,
|
||||||
SupplierId = p.SupplierId,
|
SupplierId = p.SupplierId,
|
||||||
PurchasePrice = p.PurchasePrice
|
PurchasePrice = p.PurchasePrice,
|
||||||
|
CategoryIds = p.ProductCategories.Select(pc => pc.CategoryId).ToList() // << NEU >>
|
||||||
}).ToList();
|
}).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<AdminProductDto?> GetAdminProductByIdAsync(Guid id)
|
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;
|
if (product == null) return null;
|
||||||
|
|
||||||
return new AdminProductDto
|
return new AdminProductDto
|
||||||
@@ -66,16 +77,16 @@ namespace Webshop.Application.Services.Admin
|
|||||||
CreatedDate = product.CreatedDate,
|
CreatedDate = product.CreatedDate,
|
||||||
LastModifiedDate = product.LastModifiedDate,
|
LastModifiedDate = product.LastModifiedDate,
|
||||||
SupplierId = product.SupplierId,
|
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)
|
public async Task<AdminProductDto> CreateAdminProductAsync(AdminProductDto productDto)
|
||||||
{
|
{
|
||||||
// Konvertiere DTO zu Domain-Entit<69>t
|
|
||||||
var newProduct = new Product
|
var newProduct = new Product
|
||||||
{
|
{
|
||||||
Id = Guid.NewGuid(), // API generiert die ID immer neu
|
Id = Guid.NewGuid(),
|
||||||
Name = productDto.Name,
|
Name = productDto.Name,
|
||||||
Description = productDto.Description,
|
Description = productDto.Description,
|
||||||
SKU = productDto.SKU,
|
SKU = productDto.SKU,
|
||||||
@@ -87,26 +98,33 @@ namespace Webshop.Application.Services.Admin
|
|||||||
Weight = productDto.Weight,
|
Weight = productDto.Weight,
|
||||||
ImageUrl = productDto.ImageUrl,
|
ImageUrl = productDto.ImageUrl,
|
||||||
Slug = productDto.Slug,
|
Slug = productDto.Slug,
|
||||||
CreatedDate = DateTimeOffset.UtcNow, // Server setzt Erstellungsdatum
|
CreatedDate = DateTimeOffset.UtcNow,
|
||||||
LastModifiedDate = null, // Neues Produkt, noch nicht modifiziert
|
|
||||||
SupplierId = productDto.SupplierId,
|
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.Id = newProduct.Id;
|
||||||
productDto.CreatedDate = newProduct.CreatedDate;
|
|
||||||
return productDto;
|
return productDto;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> UpdateAdminProductAsync(AdminProductDto 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;
|
if (existingProduct == null) return false;
|
||||||
|
|
||||||
// Aktualisiere Eigenschaften des bestehenden Produkts
|
// Aktualisiere die direkten Eigenschaften des Produkts
|
||||||
existingProduct.Name = productDto.Name;
|
existingProduct.Name = productDto.Name;
|
||||||
existingProduct.Description = productDto.Description;
|
existingProduct.Description = productDto.Description;
|
||||||
existingProduct.SKU = productDto.SKU;
|
existingProduct.SKU = productDto.SKU;
|
||||||
@@ -118,11 +136,19 @@ namespace Webshop.Application.Services.Admin
|
|||||||
existingProduct.Weight = productDto.Weight;
|
existingProduct.Weight = productDto.Weight;
|
||||||
existingProduct.ImageUrl = productDto.ImageUrl;
|
existingProduct.ImageUrl = productDto.ImageUrl;
|
||||||
existingProduct.Slug = productDto.Slug;
|
existingProduct.Slug = productDto.Slug;
|
||||||
existingProduct.LastModifiedDate = DateTimeOffset.UtcNow; // Server setzt Modifikationsdatum
|
|
||||||
existingProduct.SupplierId = productDto.SupplierId;
|
existingProduct.SupplierId = productDto.SupplierId;
|
||||||
existingProduct.PurchasePrice = productDto.PurchasePrice;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,7 +157,7 @@ namespace Webshop.Application.Services.Admin
|
|||||||
var product = await _productRepository.GetProductByIdAsync(id);
|
var product = await _productRepository.GetProductByIdAsync(id);
|
||||||
if (product == null) return false;
|
if (product == null) return false;
|
||||||
|
|
||||||
await _productRepository.DeleteProductAsync(id);
|
await _productRepository.DeleteProductAsync(id); // << KORREKT: VERWENDET DeleteProductAsync >>
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,45 +1,62 @@
|
|||||||
|
// src/Webshop.Application/Services/Public/ProductService.cs
|
||||||
|
using Microsoft.EntityFrameworkCore; // << NEU: Für Include() und ThenInclude() >>
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Webshop.Application.DTOs.Products;
|
using Webshop.Application.DTOs.Categorys; // Für CategoryDto
|
||||||
using Webshop.Application.Services.Public.Interfaces;
|
using Webshop.Application.DTOs.Products; // Für ProductDto
|
||||||
using Webshop.Domain.Interfaces;
|
using Webshop.Application.Services.Public.Interfaces; // Für IProductService
|
||||||
|
using Webshop.Domain.Interfaces; // Für IProductRepository
|
||||||
|
using Webshop.Infrastructure.Data; // << NEU: Für ApplicationDbContext >>
|
||||||
|
|
||||||
namespace Webshop.Application.Services.Public
|
namespace Webshop.Application.Services.Public
|
||||||
{
|
{
|
||||||
public class ProductService : IProductService
|
public class ProductService : IProductService
|
||||||
{
|
{
|
||||||
private readonly IProductRepository _productRepository;
|
private readonly IProductRepository _productRepository;
|
||||||
|
private readonly ApplicationDbContext _context; // << NEU: Für direkten DB-Zugriff >>
|
||||||
|
|
||||||
public ProductService(IProductRepository productRepository)
|
public ProductService(IProductRepository productRepository, ApplicationDbContext context) // << NEU: DbContext injizieren >>
|
||||||
{
|
{
|
||||||
_productRepository = productRepository;
|
_productRepository = productRepository;
|
||||||
|
_context = context; // << NEU >>
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wir verwenden jetzt den von Ihnen vorgegebenen Namen
|
|
||||||
public async Task<IEnumerable<ProductDto>> GetAllProductsAsync()
|
public async Task<IEnumerable<ProductDto>> GetAllProductsAsync()
|
||||||
{
|
{
|
||||||
var products = await _productRepository.GetAllProductsAsync();
|
// Wir verwenden den DbContext, um Produkte und ihre Kategorien zu laden
|
||||||
|
var products = await _context.Products
|
||||||
|
.Include(p => p.ProductCategories) // Lade die Join-Tabelle
|
||||||
|
.ThenInclude(pc => pc.Category) // Lade die zugehörige Kategorie-Entität
|
||||||
|
.Where(p => p.IsActive) // Nur aktive Produkte
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
return products
|
return products.Select(p => new ProductDto
|
||||||
.Where(p => p.IsActive) // Nur aktive Produkte anzeigen
|
{
|
||||||
.Select(p => new ProductDto
|
Id = p.Id,
|
||||||
|
Name = p.Name,
|
||||||
|
Description = p.ShortDescription, // Oder p.Description, je nach Anforderung
|
||||||
|
Price = p.Price,
|
||||||
|
SKU = p.SKU,
|
||||||
|
ImageUrl = p.ImageUrl,
|
||||||
|
IsInStock = p.IsInStock,
|
||||||
|
Slug = p.Slug,
|
||||||
|
Categories = p.ProductCategories.Select(pc => new CategoryDto
|
||||||
{
|
{
|
||||||
Id = p.Id,
|
Id = pc.Category.Id,
|
||||||
Name = p.Name,
|
Name = pc.Category.Name,
|
||||||
Description = p.ShortDescription,
|
Slug = pc.Category.Slug
|
||||||
Price = p.Price,
|
// ... weitere CategoryDto-Felder bei Bedarf
|
||||||
SKU = p.SKU,
|
}).ToList()
|
||||||
ImageUrl = p.ImageUrl,
|
}).ToList();
|
||||||
IsInStock = p.IsInStock,
|
|
||||||
Slug = p.Slug // Mapping für Slug
|
|
||||||
}).ToList();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Diese Methode wird hinzugefügt, um das Interface zu erfüllen
|
|
||||||
public async Task<ProductDto?> GetProductBySlugAsync(string slug)
|
public async Task<ProductDto?> GetProductBySlugAsync(string slug)
|
||||||
{
|
{
|
||||||
var product = await _productRepository.GetBySlugAsync(slug);
|
var product = await _context.Products
|
||||||
|
.Include(p => p.ProductCategories)
|
||||||
|
.ThenInclude(pc => pc.Category)
|
||||||
|
.FirstOrDefaultAsync(p => p.Slug == slug && p.IsActive); // Nur aktives Produkt finden
|
||||||
|
|
||||||
if (product == null)
|
if (product == null)
|
||||||
{
|
{
|
||||||
@@ -50,12 +67,18 @@ namespace Webshop.Application.Services.Public
|
|||||||
{
|
{
|
||||||
Id = product.Id,
|
Id = product.Id,
|
||||||
Name = product.Name,
|
Name = product.Name,
|
||||||
Description = product.Description,
|
Description = product.Description, // Hier die volle Beschreibung
|
||||||
Price = product.Price,
|
Price = product.Price,
|
||||||
SKU = product.SKU,
|
SKU = product.SKU,
|
||||||
ImageUrl = product.ImageUrl,
|
ImageUrl = product.ImageUrl,
|
||||||
IsInStock = product.IsInStock,
|
IsInStock = product.IsInStock,
|
||||||
Slug = product.Slug // Mapping für Slug
|
Slug = product.Slug,
|
||||||
|
Categories = product.ProductCategories.Select(pc => new CategoryDto
|
||||||
|
{
|
||||||
|
Id = pc.Category.Id,
|
||||||
|
Name = pc.Category.Name,
|
||||||
|
Slug = pc.Category.Slug
|
||||||
|
}).ToList()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user