From 8219bf8d98655c5d5a3ca3d6a6009f19c03f4fc6 Mon Sep 17 00:00:00 2001 From: "Tizian.Breuch" Date: Wed, 23 Jul 2025 21:29:43 +0200 Subject: [PATCH] AdminProdService --- .../Services/Admin/AdminProductService.cs | 135 ++++++++++++++++-- 1 file changed, 122 insertions(+), 13 deletions(-) diff --git a/Webshop.Application/Services/Admin/AdminProductService.cs b/Webshop.Application/Services/Admin/AdminProductService.cs index 021d550..234fd3a 100644 --- a/Webshop.Application/Services/Admin/AdminProductService.cs +++ b/Webshop.Application/Services/Admin/AdminProductService.cs @@ -1,15 +1,15 @@ -// Auto-generiert von CreateWebshopFiles.ps1 -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Webshop.Application.DTOs; +// src/Webshop.Application/Services/Admin/AdminProductService.cs +using Webshop.Application.DTOs; // AdminProductDto using Webshop.Domain.Entities; using Webshop.Domain.Interfaces; - +using System.Collections.Generic; +using System.Threading.Tasks; +using System; // Für Guid +using System.Linq; // Für Select namespace Webshop.Application.Services.Admin { - public class AdminProductService : IAdminProductService + public class AdminProductService : IAdminProductService // Sicherstellen, dass IAdminProductService implementiert wird { private readonly IProductRepository _productRepository; @@ -18,10 +18,119 @@ namespace Webshop.Application.Services.Admin _productRepository = productRepository; } - public async Task> GetAllAdminProductsAsync() { return new List(); } - public async Task GetAdminProductByIdAsync(Guid id) { return null; } - public async Task CreateAdminProductAsync(AdminProductDto productDto) { return null; } - public async Task UpdateAdminProductAsync(AdminProductDto productDto) { return false; } - public async Task DeleteAdminProductAsync(Guid id) { return false; } + public async Task> GetAllAdminProductsAsync() + { + var products = await _productRepository.GetAllProductsAsync(); + return products.Select(p => new AdminProductDto + { + Id = p.Id, + Name = p.Name, + Description = p.Description, + SKU = p.SKU, + Price = p.Price, + OldPrice = p.OldPrice, + IsActive = p.IsActive, + IsInStock = p.IsInStock, + StockQuantity = p.StockQuantity, + Weight = p.Weight, + ImageUrl = p.ImageUrl, + Slug = p.Slug, + CreatedDate = p.CreatedDate, + LastModifiedDate = p.LastModifiedDate, + SupplierId = p.SupplierId, + PurchasePrice = p.PurchasePrice + }).ToList(); + } + + public async Task GetAdminProductByIdAsync(Guid id) + { + var product = await _productRepository.GetProductByIdAsync(id); + if (product == null) return null; + + return new AdminProductDto + { + Id = product.Id, + Name = product.Name, + Description = product.Description, + SKU = product.SKU, + Price = product.Price, + OldPrice = product.OldPrice, + IsActive = product.IsActive, + IsInStock = product.IsInStock, + StockQuantity = product.StockQuantity, + Weight = product.Weight, + ImageUrl = product.ImageUrl, + Slug = product.Slug, + CreatedDate = product.CreatedDate, + LastModifiedDate = product.LastModifiedDate, + SupplierId = product.SupplierId, + PurchasePrice = product.PurchasePrice + }; + } + + public async Task CreateAdminProductAsync(AdminProductDto productDto) + { + // Konvertiere DTO zu Domain-Entität + var newProduct = new Product + { + Id = Guid.NewGuid(), // API generiert die ID immer neu + Name = productDto.Name, + Description = productDto.Description, + SKU = productDto.SKU, + Price = productDto.Price, + OldPrice = productDto.OldPrice, + IsActive = productDto.IsActive, + IsInStock = productDto.IsInStock, + StockQuantity = productDto.StockQuantity, + Weight = productDto.Weight, + ImageUrl = productDto.ImageUrl, + Slug = productDto.Slug, + CreatedDate = DateTimeOffset.UtcNow, // Server setzt Erstellungsdatum + LastModifiedDate = null, // Neues Produkt, noch nicht modifiziert + SupplierId = productDto.SupplierId, + PurchasePrice = productDto.PurchasePrice + }; + + await _productRepository.AddProductAsync(newProduct); // Fügt in DB ein + + // Aktualisiere DTO mit der neuen ID und gib es zurück + productDto.Id = newProduct.Id; + productDto.CreatedDate = newProduct.CreatedDate; + return productDto; + } + + public async Task UpdateAdminProductAsync(AdminProductDto productDto) + { + var existingProduct = await _productRepository.GetProductByIdAsync(productDto.Id); + if (existingProduct == null) return false; + + // Aktualisiere Eigenschaften des bestehenden Produkts + existingProduct.Name = productDto.Name; + existingProduct.Description = productDto.Description; + existingProduct.SKU = productDto.SKU; + existingProduct.Price = productDto.Price; + existingProduct.OldPrice = productDto.OldPrice; + existingProduct.IsActive = productDto.IsActive; + existingProduct.IsInStock = productDto.IsInStock; + existingProduct.StockQuantity = productDto.StockQuantity; + 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; + + await _productRepository.UpdateProductAsync(existingProduct); + return true; + } + + public async Task DeleteAdminProductAsync(Guid id) + { + var product = await _productRepository.GetProductByIdAsync(id); + if (product == null) return false; + + await _productRepository.DeleteProductAsync(id); + return true; + } } -} +} \ No newline at end of file