adminproduct
This commit is contained in:
@@ -5,8 +5,10 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Webshop.Application;
|
||||||
using Webshop.Application.DTOs.Products;
|
using Webshop.Application.DTOs.Products;
|
||||||
using Webshop.Application.Services.Admin.Interfaces;
|
using Webshop.Application.Services.Admin.Interfaces;
|
||||||
|
|
||||||
namespace Webshop.Api.Controllers.Admin
|
namespace Webshop.Api.Controllers.Admin
|
||||||
{
|
{
|
||||||
[ApiController]
|
[ApiController]
|
||||||
@@ -22,54 +24,93 @@ namespace Webshop.Api.Controllers.Admin
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<ActionResult<IEnumerable<AdminProductDto>>> GetAdminProducts()
|
[ProducesResponseType(typeof(IEnumerable<AdminProductDto>), StatusCodes.Status200OK)]
|
||||||
|
public async Task<IActionResult> GetAdminProducts()
|
||||||
{
|
{
|
||||||
var products = await _adminProductService.GetAllAdminProductsAsync();
|
var result = await _adminProductService.GetAllAdminProductsAsync();
|
||||||
return Ok(products);
|
return Ok(result.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
public async Task<ActionResult<AdminProductDto>> GetAdminProduct(Guid id)
|
[ProducesResponseType(typeof(AdminProductDto), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||||||
|
public async Task<IActionResult> GetAdminProduct(Guid id)
|
||||||
{
|
{
|
||||||
var product = await _adminProductService.GetAdminProductByIdAsync(id);
|
var result = await _adminProductService.GetAdminProductByIdAsync(id);
|
||||||
if (product == null) return NotFound();
|
|
||||||
return Ok(product);
|
return result.Type switch
|
||||||
|
{
|
||||||
|
ServiceResultType.Success => Ok(result.Value),
|
||||||
|
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
|
||||||
|
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[Consumes("multipart/form-data")]
|
[Consumes("multipart/form-data")]
|
||||||
public async Task<ActionResult<AdminProductDto>> CreateAdminProduct([FromForm] CreateAdminProductDto productDto)
|
[ProducesResponseType(typeof(AdminProductDto), StatusCodes.Status201Created)]
|
||||||
|
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status409Conflict)]
|
||||||
|
public async Task<IActionResult> CreateAdminProduct([FromForm] CreateAdminProductDto productDto)
|
||||||
{
|
{
|
||||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
return BadRequest(ModelState);
|
||||||
|
}
|
||||||
|
|
||||||
var createdProduct = await _adminProductService.CreateAdminProductAsync(productDto);
|
var result = await _adminProductService.CreateAdminProductAsync(productDto);
|
||||||
if (createdProduct == null) return BadRequest("Produkt konnte nicht erstellt werden.");
|
|
||||||
|
|
||||||
return CreatedAtAction(nameof(GetAdminProduct), new { id = createdProduct.Id }, createdProduct);
|
return result.Type switch
|
||||||
|
{
|
||||||
|
ServiceResultType.Success => CreatedAtAction(nameof(GetAdminProduct), new { id = result.Value!.Id }, result.Value),
|
||||||
|
ServiceResultType.Conflict => Conflict(new { Message = result.ErrorMessage }),
|
||||||
|
ServiceResultType.InvalidInput => BadRequest(new { Message = result.ErrorMessage }),
|
||||||
|
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[HttpPut("{id}")]
|
[HttpPut("{id}")]
|
||||||
[Consumes("multipart/form-data")]
|
[Consumes("multipart/form-data")]
|
||||||
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||||
|
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||||||
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status409Conflict)]
|
||||||
public async Task<IActionResult> UpdateAdminProduct(Guid id, [FromForm] UpdateAdminProductDto productDto)
|
public async Task<IActionResult> UpdateAdminProduct(Guid id, [FromForm] UpdateAdminProductDto productDto)
|
||||||
{
|
{
|
||||||
if (id != productDto.Id) return BadRequest("ID in URL und Body stimmen nicht <20>berein.");
|
if (id != productDto.Id)
|
||||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
{
|
||||||
|
return BadRequest(new { Message = "ID in der URL und im Body stimmen nicht <20>berein." });
|
||||||
|
}
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
return BadRequest(ModelState);
|
||||||
|
}
|
||||||
|
|
||||||
var success = await _adminProductService.UpdateAdminProductAsync(productDto);
|
var result = await _adminProductService.UpdateAdminProductAsync(productDto);
|
||||||
|
|
||||||
if (!success) return NotFound();
|
return result.Type switch
|
||||||
|
{
|
||||||
return NoContent();
|
ServiceResultType.Success => NoContent(),
|
||||||
|
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
|
||||||
|
ServiceResultType.Conflict => Conflict(new { Message = result.ErrorMessage }),
|
||||||
|
ServiceResultType.InvalidInput => BadRequest(new { Message = result.ErrorMessage }),
|
||||||
|
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[HttpDelete("{id}")]
|
[HttpDelete("{id}")]
|
||||||
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||||
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||||||
public async Task<IActionResult> DeleteAdminProduct(Guid id)
|
public async Task<IActionResult> DeleteAdminProduct(Guid id)
|
||||||
{
|
{
|
||||||
var success = await _adminProductService.DeleteAdminProductAsync(id);
|
var result = await _adminProductService.DeleteAdminProductAsync(id);
|
||||||
if (!success) return NotFound();
|
|
||||||
return NoContent();
|
return result.Type switch
|
||||||
|
{
|
||||||
|
ServiceResultType.Success => NoContent(),
|
||||||
|
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
|
||||||
|
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,7 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using Webshop.Application;
|
||||||
using Webshop.Application.DTOs.Products;
|
using Webshop.Application.DTOs.Products;
|
||||||
using Webshop.Application.Services.Admin.Interfaces;
|
using Webshop.Application.Services.Admin.Interfaces;
|
||||||
using Webshop.Infrastructure.Data;
|
using Webshop.Infrastructure.Data;
|
||||||
@@ -16,7 +17,7 @@ namespace Webshop.Application.Services.Admin
|
|||||||
{
|
{
|
||||||
private readonly IProductRepository _productRepository;
|
private readonly IProductRepository _productRepository;
|
||||||
private readonly IFileStorageService _fileStorageService;
|
private readonly IFileStorageService _fileStorageService;
|
||||||
private readonly ApplicationDbContext _context;
|
private readonly ApplicationDbContext _context; // For checks that might not be in the repository
|
||||||
|
|
||||||
public AdminProductService(
|
public AdminProductService(
|
||||||
IProductRepository productRepository,
|
IProductRepository productRepository,
|
||||||
@@ -28,7 +29,7 @@ namespace Webshop.Application.Services.Admin
|
|||||||
_context = context;
|
_context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<AdminProductDto>> GetAllAdminProductsAsync()
|
public async Task<ServiceResult<IEnumerable<AdminProductDto>>> GetAllAdminProductsAsync()
|
||||||
{
|
{
|
||||||
var products = await _context.Products
|
var products = await _context.Products
|
||||||
.Include(p => p.Productcategories)
|
.Include(p => p.Productcategories)
|
||||||
@@ -36,83 +37,46 @@ namespace Webshop.Application.Services.Admin
|
|||||||
.OrderBy(p => p.Name)
|
.OrderBy(p => p.Name)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
return products.Select(p => new AdminProductDto
|
var dtos = products.Select(MapToAdminDto).ToList();
|
||||||
{
|
return ServiceResult.Ok<IEnumerable<AdminProductDto>>(dtos);
|
||||||
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,
|
|
||||||
Slug = p.Slug,
|
|
||||||
CreatedDate = p.CreatedDate,
|
|
||||||
LastModifiedDate = p.LastModifiedDate,
|
|
||||||
SupplierId = p.SupplierId,
|
|
||||||
PurchasePrice = p.PurchasePrice,
|
|
||||||
categorieIds = p.Productcategories.Select(pc => pc.categorieId).ToList(),
|
|
||||||
Images = p.Images.OrderBy(i => i.DisplayOrder).Select(img => new ProductImageDto
|
|
||||||
{
|
|
||||||
Id = img.Id,
|
|
||||||
Url = img.Url,
|
|
||||||
IsMainImage = img.IsMainImage,
|
|
||||||
DisplayOrder = img.DisplayOrder
|
|
||||||
}).ToList()
|
|
||||||
}).ToList();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<AdminProductDto?> GetAdminProductByIdAsync(Guid id)
|
public async Task<ServiceResult<AdminProductDto>> GetAdminProductByIdAsync(Guid id)
|
||||||
{
|
{
|
||||||
var product = await _context.Products
|
var product = await _context.Products
|
||||||
.Include(p => p.Productcategories)
|
.Include(p => p.Productcategories)
|
||||||
.Include(p => p.Images)
|
.Include(p => p.Images)
|
||||||
.FirstOrDefaultAsync(p => p.Id == id);
|
.FirstOrDefaultAsync(p => p.Id == id);
|
||||||
|
|
||||||
if (product == null) return null;
|
if (product == null)
|
||||||
|
|
||||||
return new AdminProductDto
|
|
||||||
{
|
{
|
||||||
Id = product.Id,
|
return ServiceResult.Fail<AdminProductDto>(ServiceResultType.NotFound, $"Produkt mit ID '{id}' nicht gefunden.");
|
||||||
Name = product.Name,
|
}
|
||||||
Description = product.Description,
|
|
||||||
SKU = product.SKU,
|
return ServiceResult.Ok(MapToAdminDto(product));
|
||||||
Price = product.Price,
|
|
||||||
OldPrice = product.OldPrice,
|
|
||||||
IsActive = product.IsActive,
|
|
||||||
IsInStock = product.IsInStock,
|
|
||||||
StockQuantity = product.StockQuantity,
|
|
||||||
Weight = product.Weight,
|
|
||||||
Slug = product.Slug,
|
|
||||||
CreatedDate = product.CreatedDate,
|
|
||||||
LastModifiedDate = product.LastModifiedDate,
|
|
||||||
SupplierId = product.SupplierId,
|
|
||||||
PurchasePrice = product.PurchasePrice,
|
|
||||||
categorieIds = product.Productcategories.Select(pc => pc.categorieId).ToList(),
|
|
||||||
Images = product.Images.OrderBy(i => i.DisplayOrder).Select(img => new ProductImageDto
|
|
||||||
{
|
|
||||||
Id = img.Id,
|
|
||||||
Url = img.Url,
|
|
||||||
IsMainImage = img.IsMainImage,
|
|
||||||
DisplayOrder = img.DisplayOrder
|
|
||||||
}).ToList()
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<AdminProductDto?> CreateAdminProductAsync(CreateAdminProductDto productDto)
|
public async Task<ServiceResult<AdminProductDto>> CreateAdminProductAsync(CreateAdminProductDto productDto)
|
||||||
{
|
{
|
||||||
var images = new List<ProductImage>();
|
var skuExists = await _context.Products.AnyAsync(p => p.SKU == productDto.SKU);
|
||||||
|
if (skuExists)
|
||||||
|
{
|
||||||
|
return ServiceResult.Fail<AdminProductDto>(ServiceResultType.Conflict, $"Ein Produkt mit der SKU '{productDto.SKU}' existiert bereits.");
|
||||||
|
}
|
||||||
|
var slugExists = await _context.Products.AnyAsync(p => p.Slug == productDto.Slug);
|
||||||
|
if (slugExists)
|
||||||
|
{
|
||||||
|
return ServiceResult.Fail<AdminProductDto>(ServiceResultType.Conflict, $"Ein Produkt mit dem Slug '{productDto.Slug}' existiert bereits.");
|
||||||
|
}
|
||||||
|
|
||||||
// Hauptbild hochladen
|
var images = new List<ProductImage>();
|
||||||
|
// Bild-Upload Logik...
|
||||||
if (productDto.MainImageFile != null)
|
if (productDto.MainImageFile != null)
|
||||||
{
|
{
|
||||||
await using var stream = productDto.MainImageFile.OpenReadStream();
|
await using var stream = productDto.MainImageFile.OpenReadStream();
|
||||||
var url = await _fileStorageService.SaveFileAsync(stream, productDto.MainImageFile.FileName, productDto.MainImageFile.ContentType);
|
var url = await _fileStorageService.SaveFileAsync(stream, productDto.MainImageFile.FileName, productDto.MainImageFile.ContentType);
|
||||||
images.Add(new ProductImage { Url = url, IsMainImage = true, DisplayOrder = 1 });
|
images.Add(new ProductImage { Url = url, IsMainImage = true, DisplayOrder = 1 });
|
||||||
}
|
}
|
||||||
// Weitere Bilder hochladen
|
|
||||||
if (productDto.AdditionalImageFiles != null)
|
if (productDto.AdditionalImageFiles != null)
|
||||||
{
|
{
|
||||||
int order = 2;
|
int order = 2;
|
||||||
@@ -137,33 +101,46 @@ namespace Webshop.Application.Services.Admin
|
|||||||
OldPrice = productDto.OldPrice,
|
OldPrice = productDto.OldPrice,
|
||||||
SupplierId = productDto.SupplierId,
|
SupplierId = productDto.SupplierId,
|
||||||
PurchasePrice = productDto.PurchasePrice,
|
PurchasePrice = productDto.PurchasePrice,
|
||||||
IsFeatured = productDto.IsFeatured, // << NEU >>
|
IsFeatured = productDto.IsFeatured,
|
||||||
FeaturedDisplayOrder = productDto.FeaturedDisplayOrder, // << NEU >>
|
FeaturedDisplayOrder = productDto.FeaturedDisplayOrder,
|
||||||
Images = images,
|
Images = images,
|
||||||
Productcategories = productDto.CategorieIds.Select(cId => new Productcategorie { categorieId = cId }).ToList()
|
Productcategories = productDto.CategorieIds.Select(cId => new Productcategorie { categorieId = cId }).ToList()
|
||||||
};
|
};
|
||||||
|
|
||||||
await _productRepository.AddProductAsync(newProduct);
|
await _productRepository.AddProductAsync(newProduct);
|
||||||
|
|
||||||
return await GetAdminProductByIdAsync(newProduct.Id);
|
return ServiceResult.Ok(MapToAdminDto(newProduct));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> UpdateAdminProductAsync(UpdateAdminProductDto productDto)
|
public async Task<ServiceResult> UpdateAdminProductAsync(UpdateAdminProductDto productDto)
|
||||||
{
|
{
|
||||||
var existingProduct = await _context.Products
|
var existingProduct = await _context.Products
|
||||||
.Include(p => p.Images)
|
.Include(p => p.Images)
|
||||||
.Include(p => p.Productcategories)
|
.Include(p => p.Productcategories)
|
||||||
.FirstOrDefaultAsync(p => p.Id == productDto.Id);
|
.FirstOrDefaultAsync(p => p.Id == productDto.Id);
|
||||||
|
|
||||||
if (existingProduct == null) return false;
|
if (existingProduct == null)
|
||||||
|
{
|
||||||
|
return ServiceResult.Fail(ServiceResultType.NotFound, $"Produkt mit ID '{productDto.Id}' nicht gefunden.");
|
||||||
|
}
|
||||||
|
|
||||||
// Bilder l<>schen
|
var skuExists = await _context.Products.AnyAsync(p => p.SKU == productDto.SKU && p.Id != productDto.Id);
|
||||||
|
if (skuExists)
|
||||||
|
{
|
||||||
|
return ServiceResult.Fail(ServiceResultType.Conflict, $"Ein anderes Produkt mit der SKU '{productDto.SKU}' existiert bereits.");
|
||||||
|
}
|
||||||
|
var slugExists = await _context.Products.AnyAsync(p => p.Slug == productDto.Slug && p.Id != productDto.Id);
|
||||||
|
if (slugExists)
|
||||||
|
{
|
||||||
|
return ServiceResult.Fail(ServiceResultType.Conflict, $"Ein anderes Produkt mit dem Slug '{productDto.Slug}' existiert bereits.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bild-Management Logik...
|
||||||
if (productDto.ImagesToDelete != null && productDto.ImagesToDelete.Any())
|
if (productDto.ImagesToDelete != null && productDto.ImagesToDelete.Any())
|
||||||
{
|
{
|
||||||
var imagesToRemove = existingProduct.Images.Where(img => productDto.ImagesToDelete.Contains(img.Id)).ToList();
|
var imagesToRemove = existingProduct.Images.Where(img => productDto.ImagesToDelete.Contains(img.Id)).ToList();
|
||||||
_context.ProductImages.RemoveRange(imagesToRemove);
|
_context.ProductImages.RemoveRange(imagesToRemove);
|
||||||
}
|
}
|
||||||
// Hauptbild aktualisieren/hochladen
|
|
||||||
if (productDto.MainImageFile != null)
|
if (productDto.MainImageFile != null)
|
||||||
{
|
{
|
||||||
var existingMainImage = existingProduct.Images.FirstOrDefault(img => img.IsMainImage);
|
var existingMainImage = existingProduct.Images.FirstOrDefault(img => img.IsMainImage);
|
||||||
@@ -173,7 +150,6 @@ namespace Webshop.Application.Services.Admin
|
|||||||
var url = await _fileStorageService.SaveFileAsync(stream, productDto.MainImageFile.FileName, productDto.MainImageFile.ContentType);
|
var url = await _fileStorageService.SaveFileAsync(stream, productDto.MainImageFile.FileName, productDto.MainImageFile.ContentType);
|
||||||
existingProduct.Images.Add(new ProductImage { Url = url, IsMainImage = true, DisplayOrder = 1 });
|
existingProduct.Images.Add(new ProductImage { Url = url, IsMainImage = true, DisplayOrder = 1 });
|
||||||
}
|
}
|
||||||
// Weitere Bilder hinzuf<75>gen
|
|
||||||
if (productDto.AdditionalImageFiles != null && productDto.AdditionalImageFiles.Any())
|
if (productDto.AdditionalImageFiles != null && productDto.AdditionalImageFiles.Any())
|
||||||
{
|
{
|
||||||
int displayOrder = (existingProduct.Images.Any() ? existingProduct.Images.Max(i => i.DisplayOrder) : 0) + 1;
|
int displayOrder = (existingProduct.Images.Any() ? existingProduct.Images.Max(i => i.DisplayOrder) : 0) + 1;
|
||||||
@@ -185,7 +161,7 @@ namespace Webshop.Application.Services.Admin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Basisdaten aktualisieren
|
// Basisdaten aktualisieren...
|
||||||
existingProduct.Name = productDto.Name;
|
existingProduct.Name = productDto.Name;
|
||||||
existingProduct.Description = productDto.Description;
|
existingProduct.Description = productDto.Description;
|
||||||
existingProduct.SKU = productDto.SKU;
|
existingProduct.SKU = productDto.SKU;
|
||||||
@@ -198,8 +174,8 @@ namespace Webshop.Application.Services.Admin
|
|||||||
existingProduct.SupplierId = productDto.SupplierId;
|
existingProduct.SupplierId = productDto.SupplierId;
|
||||||
existingProduct.PurchasePrice = productDto.PurchasePrice;
|
existingProduct.PurchasePrice = productDto.PurchasePrice;
|
||||||
existingProduct.LastModifiedDate = DateTimeOffset.UtcNow;
|
existingProduct.LastModifiedDate = DateTimeOffset.UtcNow;
|
||||||
existingProduct.IsFeatured = productDto.IsFeatured; // << NEU >>
|
existingProduct.IsFeatured = productDto.IsFeatured;
|
||||||
existingProduct.FeaturedDisplayOrder = productDto.FeaturedDisplayOrder; // << NEU >>
|
existingProduct.FeaturedDisplayOrder = productDto.FeaturedDisplayOrder;
|
||||||
|
|
||||||
// Kategorien synchronisieren
|
// Kategorien synchronisieren
|
||||||
existingProduct.Productcategories.Clear();
|
existingProduct.Productcategories.Clear();
|
||||||
@@ -212,16 +188,51 @@ namespace Webshop.Application.Services.Admin
|
|||||||
}
|
}
|
||||||
|
|
||||||
await _productRepository.UpdateProductAsync(existingProduct);
|
await _productRepository.UpdateProductAsync(existingProduct);
|
||||||
return true;
|
return ServiceResult.Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> DeleteAdminProductAsync(Guid id)
|
public async Task<ServiceResult> DeleteAdminProductAsync(Guid id)
|
||||||
{
|
{
|
||||||
var product = await _productRepository.GetProductByIdAsync(id);
|
var product = await _productRepository.GetProductByIdAsync(id);
|
||||||
if (product == null) return false;
|
if (product == null)
|
||||||
|
{
|
||||||
|
return ServiceResult.Fail(ServiceResultType.NotFound, $"Produkt mit ID '{id}' nicht gefunden.");
|
||||||
|
}
|
||||||
|
|
||||||
await _productRepository.DeleteProductAsync(id);
|
await _productRepository.DeleteProductAsync(id);
|
||||||
return true;
|
return ServiceResult.Ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
private AdminProductDto MapToAdminDto(Product product)
|
||||||
|
{
|
||||||
|
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,
|
||||||
|
Slug = product.Slug,
|
||||||
|
CreatedDate = product.CreatedDate,
|
||||||
|
LastModifiedDate = product.LastModifiedDate,
|
||||||
|
SupplierId = product.SupplierId,
|
||||||
|
PurchasePrice = product.PurchasePrice,
|
||||||
|
IsFeatured = product.IsFeatured,
|
||||||
|
FeaturedDisplayOrder = product.FeaturedDisplayOrder,
|
||||||
|
categorieIds = product.Productcategories.Select(pc => pc.categorieId).ToList(),
|
||||||
|
Images = product.Images.OrderBy(i => i.DisplayOrder).Select(img => new ProductImageDto
|
||||||
|
{
|
||||||
|
Id = img.Id,
|
||||||
|
Url = img.Url,
|
||||||
|
IsMainImage = img.IsMainImage,
|
||||||
|
DisplayOrder = img.DisplayOrder
|
||||||
|
}).ToList()
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,16 +2,17 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Webshop.Application;
|
||||||
using Webshop.Application.DTOs.Products;
|
using Webshop.Application.DTOs.Products;
|
||||||
|
|
||||||
namespace Webshop.Application.Services.Admin.Interfaces
|
namespace Webshop.Application.Services.Admin.Interfaces
|
||||||
{
|
{
|
||||||
public interface IAdminProductService
|
public interface IAdminProductService
|
||||||
{
|
{
|
||||||
Task<IEnumerable<AdminProductDto>> GetAllAdminProductsAsync();
|
Task<ServiceResult<IEnumerable<AdminProductDto>>> GetAllAdminProductsAsync();
|
||||||
Task<AdminProductDto?> GetAdminProductByIdAsync(Guid id);
|
Task<ServiceResult<AdminProductDto>> GetAdminProductByIdAsync(Guid id);
|
||||||
Task<AdminProductDto?> CreateAdminProductAsync(CreateAdminProductDto productDto); // << DTO-TYP GEÄNDERT >>
|
Task<ServiceResult<AdminProductDto>> CreateAdminProductAsync(CreateAdminProductDto productDto);
|
||||||
Task<bool> UpdateAdminProductAsync(UpdateAdminProductDto productDto); // << DTO-TYP GEÄNDERT >>
|
Task<ServiceResult> UpdateAdminProductAsync(UpdateAdminProductDto productDto);
|
||||||
Task<bool> DeleteAdminProductAsync(Guid id);
|
Task<ServiceResult> DeleteAdminProductAsync(Guid id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user