AdminCategorie

This commit is contained in:
Tizian.Breuch
2025-09-25 13:41:40 +02:00
parent 5851072d64
commit 007da919da
3 changed files with 90 additions and 39 deletions

View File

@@ -1,4 +1,4 @@
// src/Webshop.Application/Services/Admin/Admincategorieservice.cs
// src/Webshop.Application/Services/Admin/AdminCategorieService.cs
using System;
using System.Collections.Generic;
using System.Linq;
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
using Webshop.Application.DTOs.Categorie;
using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces;
using Webshop.Application; // << NEU: F<>r ServiceResult >>
using Webshop.Application;
namespace Webshop.Application.Services.Admin
{
@@ -21,16 +21,21 @@ namespace Webshop.Application.Services.Admin
_fileStorageService = fileStorageService;
}
public async Task<IEnumerable<CategorieDto>> GetAllAsync()
public async Task<ServiceResult<IEnumerable<CategorieDto>>> GetAllAsync()
{
var categories = await _categorieRepository.GetAllAsync();
return categories.Select(MapToDto).ToList();
var dtos = categories.Select(MapToDto).ToList();
return ServiceResult.Ok<IEnumerable<CategorieDto>>(dtos);
}
public async Task<CategorieDto?> GetByIdAsync(Guid id)
public async Task<ServiceResult<CategorieDto>> GetByIdAsync(Guid id)
{
var categorie = await _categorieRepository.GetByIdAsync(id);
return categorie != null ? MapToDto(categorie) : null;
if (categorie == null)
{
return ServiceResult.Fail<CategorieDto>(ServiceResultType.NotFound, $"Kategorie mit ID '{id}' nicht gefunden.");
}
return ServiceResult.Ok(MapToDto(categorie));
}
public async Task<ServiceResult<CategorieDto>> CreateAsync(CreatecategorieDto categorieDto)
@@ -38,7 +43,7 @@ namespace Webshop.Application.Services.Admin
var existing = await _categorieRepository.GetBySlugAsync(categorieDto.Slug);
if (existing != null)
{
return ServiceResult.Fail<CategorieDto>(ServiceResultType.InvalidInput, "Eine Kategorie mit diesem Slug existiert bereits.");
return ServiceResult.Fail<CategorieDto>(ServiceResultType.Conflict, "Eine Kategorie mit diesem Slug existiert bereits.");
}
string? imageUrl = null;
@@ -62,19 +67,21 @@ namespace Webshop.Application.Services.Admin
await _categorieRepository.AddAsync(categorie);
var createdDto = MapToDto(categorie);
return ServiceResult.Ok(createdDto);
return ServiceResult.Ok(MapToDto(categorie));
}
public async Task<ServiceResult> UpdateAsync(UpdatecategorieDto categorieDto)
{
var existing = await _categorieRepository.GetByIdAsync(categorieDto.Id);
if (existing == null) return ServiceResult.Fail(ServiceResultType.NotFound, "Kategorie nicht gefunden.");
if (existing == null)
{
return ServiceResult.Fail(ServiceResultType.NotFound, $"Kategorie mit ID '{categorieDto.Id}' nicht gefunden.");
}
var slugExists = await _categorieRepository.GetBySlugAsync(categorieDto.Slug);
if (slugExists != null && slugExists.Id != categorieDto.Id)
{
return ServiceResult.Fail(ServiceResultType.InvalidInput, "Eine andere Kategorie mit diesem Slug existiert bereits.");
return ServiceResult.Fail(ServiceResultType.Conflict, "Eine andere Kategorie mit diesem Slug existiert bereits.");
}
string? imageUrl = existing.ImageUrl;
@@ -83,12 +90,12 @@ namespace Webshop.Application.Services.Admin
await using var stream = categorieDto.ImageFile.OpenReadStream();
imageUrl = await _fileStorageService.SaveFileAsync(stream, categorieDto.ImageFile.FileName, categorieDto.ImageFile.ContentType);
}
else if (string.IsNullOrEmpty(categorieDto.ImageUrl))
else if (string.IsNullOrEmpty(categorieDto.ImageUrl) && !string.IsNullOrEmpty(existing.ImageUrl))
{
// Hier k<>nnte Logik zum L<>schen der alten Datei stehen, falls gew<65>nscht
imageUrl = null;
}
existing.Name = categorieDto.Name;
existing.Slug = categorieDto.Slug;
existing.Description = categorieDto.Description;
@@ -105,13 +112,24 @@ namespace Webshop.Application.Services.Admin
public async Task<ServiceResult> DeleteAsync(Guid id)
{
var categorie = await _categorieRepository.GetByIdAsync(id);
if (categorie == null) return ServiceResult.Fail(ServiceResultType.NotFound, "Kategorie nicht gefunden.");
if (categorie == null)
{
return ServiceResult.Fail(ServiceResultType.NotFound, $"Kategorie mit ID '{id}' nicht gefunden.");
}
// Pr<50>fung auf Konflikt: Hat diese Kategorie untergeordnete Kategorien?
var allCategories = await _categorieRepository.GetAllAsync();
if (allCategories.Any(c => c.ParentcategorieId == id))
{
return ServiceResult.Fail(ServiceResultType.Conflict, "Kategorie kann nicht gel<65>scht werden, da sie als <20>bergeordnete Kategorie f<>r andere Kategorien dient.");
}
// Hier k<>nnte man auch pr<70>fen, ob Produkte dieser Kategorie zugeordnet sind.
await _categorieRepository.DeleteAsync(id);
return ServiceResult.Ok();
}
// Private Helper-Methode f<>r konsistentes Mapping
private CategorieDto MapToDto(Categorie c)
{
return new CategorieDto

View File

@@ -1,18 +1,18 @@
// src/Webshop.Application/Services/Admin/IAdmincategorieservice.cs
// src/Webshop.Application/Services/Admin/IAdminCategorieService.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application; // << HINZUF<55>GEN >>
using Webshop.Application;
using Webshop.Application.DTOs.Categorie;
namespace Webshop.Application.Services.Admin
{
public interface IAdminCategorieService
{
Task<IEnumerable<CategorieDto>> GetAllAsync();
Task<CategorieDto?> GetByIdAsync(Guid id);
Task<ServiceResult<IEnumerable<CategorieDto>>> GetAllAsync();
Task<ServiceResult<CategorieDto>> GetByIdAsync(Guid id);
Task<ServiceResult<CategorieDto>> CreateAsync(CreatecategorieDto categorieDto);
Task<ServiceResult> UpdateAsync(UpdatecategorieDto categorieDto); // << DTO-TYP GE<47>NDERT >>
Task<ServiceResult> UpdateAsync(UpdatecategorieDto categorieDto);
Task<ServiceResult> DeleteAsync(Guid id);
}
}