categorie fix
This commit is contained in:
@@ -6,22 +6,115 @@ using System.Threading.Tasks;
|
||||
using Webshop.Application.DTOs.Categorie;
|
||||
using Webshop.Domain.Entities;
|
||||
using Webshop.Domain.Interfaces;
|
||||
using Webshop.Application; // << NEU: F<>r ServiceResult >>
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
{
|
||||
public class AdminCategorieService : IAdminCategorieService
|
||||
{
|
||||
private readonly ICategorieRepository _categorieRepository;
|
||||
private readonly IFileStorageService _fileStorageService;
|
||||
|
||||
public AdminCategorieService(ICategorieRepository categorieRepository)
|
||||
public AdminCategorieService(ICategorieRepository categorieRepository, IFileStorageService fileStorageService)
|
||||
{
|
||||
_categorieRepository = categorieRepository;
|
||||
_fileStorageService = fileStorageService;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<CategorieDto>> GetAllAsync()
|
||||
{
|
||||
var categories = await _categorieRepository.GetAllAsync();
|
||||
return categories.Select(c => new CategorieDto
|
||||
return categories.Select(MapToDto).ToList();
|
||||
}
|
||||
|
||||
public async Task<CategorieDto?> GetByIdAsync(Guid id)
|
||||
{
|
||||
var categorie = await _categorieRepository.GetByIdAsync(id);
|
||||
return categorie != null ? MapToDto(categorie) : null;
|
||||
}
|
||||
|
||||
public async Task<ServiceResult<CategorieDto>> CreateAsync(CreatecategorieDto categorieDto)
|
||||
{
|
||||
var existing = await _categorieRepository.GetBySlugAsync(categorieDto.Slug);
|
||||
if (existing != null)
|
||||
{
|
||||
return ServiceResult.Fail<CategorieDto>(ServiceResultType.InvalidInput, "Eine Kategorie mit diesem Slug existiert bereits.");
|
||||
}
|
||||
|
||||
string? imageUrl = null;
|
||||
if (categorieDto.ImageFile != null)
|
||||
{
|
||||
await using var stream = categorieDto.ImageFile.OpenReadStream();
|
||||
imageUrl = await _fileStorageService.SaveFileAsync(stream, categorieDto.ImageFile.FileName, categorieDto.ImageFile.ContentType);
|
||||
}
|
||||
|
||||
var categorie = new Categorie
|
||||
{
|
||||
Name = categorieDto.Name,
|
||||
Slug = categorieDto.Slug,
|
||||
Description = categorieDto.Description,
|
||||
ParentcategorieId = categorieDto.ParentcategorieId,
|
||||
ImageUrl = imageUrl,
|
||||
IsActive = categorieDto.IsActive,
|
||||
DisplayOrder = categorieDto.DisplayOrder,
|
||||
CreatedDate = DateTimeOffset.UtcNow
|
||||
};
|
||||
|
||||
await _categorieRepository.AddAsync(categorie);
|
||||
|
||||
var createdDto = MapToDto(categorie);
|
||||
return ServiceResult.Ok(createdDto);
|
||||
}
|
||||
|
||||
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.");
|
||||
|
||||
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.");
|
||||
}
|
||||
|
||||
string? imageUrl = existing.ImageUrl;
|
||||
if (categorieDto.ImageFile != null)
|
||||
{
|
||||
await using var stream = categorieDto.ImageFile.OpenReadStream();
|
||||
imageUrl = await _fileStorageService.SaveFileAsync(stream, categorieDto.ImageFile.FileName, categorieDto.ImageFile.ContentType);
|
||||
}
|
||||
else if (string.IsNullOrEmpty(categorieDto.ImageUrl))
|
||||
{
|
||||
imageUrl = null;
|
||||
}
|
||||
|
||||
|
||||
existing.Name = categorieDto.Name;
|
||||
existing.Slug = categorieDto.Slug;
|
||||
existing.Description = categorieDto.Description;
|
||||
existing.ParentcategorieId = categorieDto.ParentcategorieId;
|
||||
existing.ImageUrl = imageUrl;
|
||||
existing.IsActive = categorieDto.IsActive;
|
||||
existing.DisplayOrder = categorieDto.DisplayOrder;
|
||||
existing.LastModifiedDate = DateTimeOffset.UtcNow;
|
||||
|
||||
await _categorieRepository.UpdateAsync(existing);
|
||||
return ServiceResult.Ok();
|
||||
}
|
||||
|
||||
public async Task<ServiceResult> DeleteAsync(Guid id)
|
||||
{
|
||||
var categorie = await _categorieRepository.GetByIdAsync(id);
|
||||
if (categorie == null) return ServiceResult.Fail(ServiceResultType.NotFound, "Kategorie nicht gefunden.");
|
||||
|
||||
await _categorieRepository.DeleteAsync(id);
|
||||
return ServiceResult.Ok();
|
||||
}
|
||||
|
||||
// Private Helper-Methode f<>r konsistentes Mapping
|
||||
private CategorieDto MapToDto(Categorie c)
|
||||
{
|
||||
return new CategorieDto
|
||||
{
|
||||
Id = c.Id,
|
||||
Name = c.Name,
|
||||
@@ -31,99 +124,7 @@ namespace Webshop.Application.Services.Admin
|
||||
ImageUrl = c.ImageUrl,
|
||||
IsActive = c.IsActive,
|
||||
DisplayOrder = c.DisplayOrder
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
public async Task<CategorieDto?> GetByIdAsync(Guid id)
|
||||
{
|
||||
var categorie = await _categorieRepository.GetByIdAsync(id);
|
||||
if (categorie == null) return null;
|
||||
|
||||
return new CategorieDto
|
||||
{
|
||||
Id = categorie.Id,
|
||||
Name = categorie.Name,
|
||||
Slug = categorie.Slug,
|
||||
Description = categorie.Description,
|
||||
ParentcategorieId = categorie.ParentcategorieId,
|
||||
ImageUrl = categorie.ImageUrl,
|
||||
IsActive = categorie.IsActive,
|
||||
DisplayOrder = categorie.DisplayOrder
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<(CategorieDto? Createdcategorie, string? ErrorMessage)> CreateAsync(CreatecategorieDto categorieDto)
|
||||
{
|
||||
var existingcategorie = await _categorieRepository.GetBySlugAsync(categorieDto.Slug);
|
||||
if (existingcategorie != null)
|
||||
{
|
||||
return (null, "Eine Kategorie mit diesem Slug existiert bereits.");
|
||||
}
|
||||
|
||||
var categorie = new Categorie
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = categorieDto.Name,
|
||||
Slug = categorieDto.Slug,
|
||||
Description = categorieDto.Description,
|
||||
ParentcategorieId = categorieDto.ParentcategorieId,
|
||||
ImageUrl = categorieDto.ImageUrl,
|
||||
IsActive = categorieDto.IsActive,
|
||||
DisplayOrder = categorieDto.DisplayOrder,
|
||||
CreatedDate = DateTimeOffset.UtcNow
|
||||
};
|
||||
|
||||
await _categorieRepository.AddAsync(categorie);
|
||||
|
||||
var createdDto = new CategorieDto
|
||||
{
|
||||
Id = categorie.Id,
|
||||
Name = categorie.Name,
|
||||
Slug = categorie.Slug,
|
||||
Description = categorie.Description,
|
||||
ParentcategorieId = categorie.ParentcategorieId,
|
||||
ImageUrl = categorie.ImageUrl,
|
||||
IsActive = categorie.IsActive,
|
||||
DisplayOrder = categorie.DisplayOrder
|
||||
};
|
||||
|
||||
return (createdDto, null);
|
||||
}
|
||||
|
||||
public async Task<(bool Success, string? ErrorMessage)> UpdateAsync(Guid id, CreatecategorieDto categorieDto)
|
||||
{
|
||||
var existingcategorie = await _categorieRepository.GetByIdAsync(id);
|
||||
if (existingcategorie == null)
|
||||
{
|
||||
return (false, "Kategorie nicht gefunden.");
|
||||
}
|
||||
|
||||
var categorieWithSameSlug = await _categorieRepository.GetBySlugAsync(categorieDto.Slug);
|
||||
if (categorieWithSameSlug != null && categorieWithSameSlug.Id != id)
|
||||
{
|
||||
return (false, "Eine andere Kategorie mit diesem Slug existiert bereits.");
|
||||
}
|
||||
|
||||
existingcategorie.Name = categorieDto.Name;
|
||||
existingcategorie.Slug = categorieDto.Slug;
|
||||
existingcategorie.Description = categorieDto.Description;
|
||||
existingcategorie.ParentcategorieId = categorieDto.ParentcategorieId;
|
||||
existingcategorie.ImageUrl = categorieDto.ImageUrl;
|
||||
existingcategorie.IsActive = categorieDto.IsActive;
|
||||
existingcategorie.DisplayOrder = categorieDto.DisplayOrder;
|
||||
existingcategorie.LastModifiedDate = DateTimeOffset.UtcNow;
|
||||
|
||||
await _categorieRepository.UpdateAsync(existingcategorie);
|
||||
return (true, null);
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(Guid id)
|
||||
{
|
||||
var categorie = await _categorieRepository.GetByIdAsync(id);
|
||||
if (categorie == null) return false;
|
||||
|
||||
await _categorieRepository.DeleteAsync(id);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application; // << HINZUF<55>GEN >>
|
||||
using Webshop.Application.DTOs.Categorie;
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
@@ -10,8 +11,8 @@ namespace Webshop.Application.Services.Admin
|
||||
{
|
||||
Task<IEnumerable<CategorieDto>> GetAllAsync();
|
||||
Task<CategorieDto?> GetByIdAsync(Guid id);
|
||||
Task<(CategorieDto? Createdcategorie, string? ErrorMessage)> CreateAsync(CreatecategorieDto categorieDto);
|
||||
Task<(bool Success, string? ErrorMessage)> UpdateAsync(Guid id, CreatecategorieDto categorieDto);
|
||||
Task<bool> DeleteAsync(Guid id);
|
||||
Task<ServiceResult<CategorieDto>> CreateAsync(CreatecategorieDto categorieDto);
|
||||
Task<ServiceResult> UpdateAsync(UpdatecategorieDto categorieDto); // << DTO-TYP GE<47>NDERT >>
|
||||
Task<ServiceResult> DeleteAsync(Guid id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user