AdminProdService
This commit is contained in:
@@ -1,15 +1,15 @@
|
|||||||
// Auto-generiert von CreateWebshopFiles.ps1
|
// src/Webshop.Application/Services/Admin/AdminProductService.cs
|
||||||
using System;
|
using Webshop.Application.DTOs; // AdminProductDto
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Webshop.Application.DTOs;
|
|
||||||
using Webshop.Domain.Entities;
|
using Webshop.Domain.Entities;
|
||||||
using Webshop.Domain.Interfaces;
|
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
|
namespace Webshop.Application.Services.Admin
|
||||||
{
|
{
|
||||||
public class AdminProductService : IAdminProductService
|
public class AdminProductService : IAdminProductService // Sicherstellen, dass IAdminProductService implementiert wird
|
||||||
{
|
{
|
||||||
private readonly IProductRepository _productRepository;
|
private readonly IProductRepository _productRepository;
|
||||||
|
|
||||||
@@ -18,10 +18,119 @@ namespace Webshop.Application.Services.Admin
|
|||||||
_productRepository = productRepository;
|
_productRepository = productRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<AdminProductDto>> GetAllAdminProductsAsync() { return new List<AdminProductDto>(); }
|
public async Task<IEnumerable<AdminProductDto>> GetAllAdminProductsAsync()
|
||||||
public async Task<AdminProductDto?> GetAdminProductByIdAsync(Guid id) { return null; }
|
{
|
||||||
public async Task<AdminProductDto> CreateAdminProductAsync(AdminProductDto productDto) { return null; }
|
var products = await _productRepository.GetAllProductsAsync();
|
||||||
public async Task<bool> UpdateAdminProductAsync(AdminProductDto productDto) { return false; }
|
return products.Select(p => new AdminProductDto
|
||||||
public async Task<bool> DeleteAdminProductAsync(Guid id) { return false; }
|
{
|
||||||
|
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<AdminProductDto?> 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<AdminProductDto> CreateAdminProductAsync(AdminProductDto productDto)
|
||||||
|
{
|
||||||
|
// Konvertiere DTO zu Domain-Entit<69>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<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);
|
||||||
|
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<bool> DeleteAdminProductAsync(Guid id)
|
||||||
|
{
|
||||||
|
var product = await _productRepository.GetProductByIdAsync(id);
|
||||||
|
if (product == null) return false;
|
||||||
|
|
||||||
|
await _productRepository.DeleteProductAsync(id);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user