This commit is contained in:
Tizian.Breuch
2025-08-01 15:11:42 +02:00
parent 96f082b38f
commit 55eeaca7f4
29 changed files with 452 additions and 452 deletions

View File

@@ -0,0 +1,129 @@
// src/Webshop.Application/Services/Admin/Admincategorieservice.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Webshop.Application.DTOs.categories;
using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces;
namespace Webshop.Application.Services.Admin
{
public class AdminCategorieService : IAdminCategorieService
{
private readonly IcategorieRepository _categorieRepository;
public AdminCategorieService(IcategorieRepository categorieRepository)
{
_categorieRepository = categorieRepository;
}
public async Task<IEnumerable<categorieDto>> GetAllAsync()
{
var categories = await _categorieRepository.GetAllAsync();
return categories.Select(c => new categorieDto
{
Id = c.Id,
Name = c.Name,
Slug = c.Slug,
Description = c.Description,
ParentcategorieId = c.ParentcategorieId,
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;
}
}
}

View File

@@ -1,129 +0,0 @@
// src/Webshop.Application/Services/Admin/AdminCategoryService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Categorys;
using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces;
namespace Webshop.Application.Services.Admin
{
public class AdminCategoryService : IAdminCategoryService
{
private readonly ICategoryRepository _categoryRepository;
public AdminCategoryService(ICategoryRepository categoryRepository)
{
_categoryRepository = categoryRepository;
}
public async Task<IEnumerable<CategoryDto>> GetAllAsync()
{
var categorys = await _categoryRepository.GetAllAsync();
return categorys.Select(c => new CategoryDto
{
Id = c.Id,
Name = c.Name,
Slug = c.Slug,
Description = c.Description,
ParentCategoryId = c.ParentCategoryId,
ImageUrl = c.ImageUrl,
IsActive = c.IsActive,
DisplayOrder = c.DisplayOrder
}).ToList();
}
public async Task<CategoryDto?> GetByIdAsync(Guid id)
{
var category = await _categoryRepository.GetByIdAsync(id);
if (category == null) return null;
return new CategoryDto
{
Id = category.Id,
Name = category.Name,
Slug = category.Slug,
Description = category.Description,
ParentCategoryId = category.ParentCategoryId,
ImageUrl = category.ImageUrl,
IsActive = category.IsActive,
DisplayOrder = category.DisplayOrder
};
}
public async Task<(CategoryDto? CreatedCategory, string? ErrorMessage)> CreateAsync(CreateCategoryDto categoryDto)
{
var existingCategory = await _categoryRepository.GetBySlugAsync(categoryDto.Slug);
if (existingCategory != null)
{
return (null, "Eine Kategorie mit diesem Slug existiert bereits.");
}
var category = new Category
{
Id = Guid.NewGuid(),
Name = categoryDto.Name,
Slug = categoryDto.Slug,
Description = categoryDto.Description,
ParentCategoryId = categoryDto.ParentCategoryId,
ImageUrl = categoryDto.ImageUrl,
IsActive = categoryDto.IsActive,
DisplayOrder = categoryDto.DisplayOrder,
CreatedDate = DateTimeOffset.UtcNow
};
await _categoryRepository.AddAsync(category);
var createdDto = new CategoryDto
{
Id = category.Id,
Name = category.Name,
Slug = category.Slug,
Description = category.Description,
ParentCategoryId = category.ParentCategoryId,
ImageUrl = category.ImageUrl,
IsActive = category.IsActive,
DisplayOrder = category.DisplayOrder
};
return (createdDto, null);
}
public async Task<(bool Success, string? ErrorMessage)> UpdateAsync(Guid id, CreateCategoryDto categoryDto)
{
var existingCategory = await _categoryRepository.GetByIdAsync(id);
if (existingCategory == null)
{
return (false, "Kategorie nicht gefunden.");
}
var categoryWithSameSlug = await _categoryRepository.GetBySlugAsync(categoryDto.Slug);
if (categoryWithSameSlug != null && categoryWithSameSlug.Id != id)
{
return (false, "Eine andere Kategorie mit diesem Slug existiert bereits.");
}
existingCategory.Name = categoryDto.Name;
existingCategory.Slug = categoryDto.Slug;
existingCategory.Description = categoryDto.Description;
existingCategory.ParentCategoryId = categoryDto.ParentCategoryId;
existingCategory.ImageUrl = categoryDto.ImageUrl;
existingCategory.IsActive = categoryDto.IsActive;
existingCategory.DisplayOrder = categoryDto.DisplayOrder;
existingCategory.LastModifiedDate = DateTimeOffset.UtcNow;
await _categoryRepository.UpdateAsync(existingCategory);
return (true, null);
}
public async Task<bool> DeleteAsync(Guid id)
{
var category = await _categoryRepository.GetByIdAsync(id);
if (category == null) return false;
await _categoryRepository.DeleteAsync(id);
return true;
}
}
}

View File

@@ -27,7 +27,7 @@ namespace Webshop.Application.Services.Admin
{
// Wir verwenden den DbContext, um auch die Kategorien effizient mitzuladen
var products = await _context.Products
.Include(p => p.Productcategorys)
.Include(p => p.Productcategories)
.ToListAsync();
return products.Select(p => new AdminProductDto
@@ -48,14 +48,14 @@ namespace Webshop.Application.Services.Admin
LastModifiedDate = p.LastModifiedDate,
SupplierId = p.SupplierId,
PurchasePrice = p.PurchasePrice,
CategoryIds = p.Productcategorys.Select(pc => pc.CategoryId).ToList() // << NEU >>
categorieIds = p.Productcategories.Select(pc => pc.categorieId).ToList() // << NEU >>
}).ToList();
}
public async Task<AdminProductDto?> GetAdminProductByIdAsync(Guid id)
{
var product = await _context.Products
.Include(p => p.Productcategorys) // << NEU: Lade die Join-Tabelle mit >>
.Include(p => p.Productcategories) // << NEU: Lade die Join-Tabelle mit >>
.FirstOrDefaultAsync(p => p.Id == id);
if (product == null) return null;
@@ -78,7 +78,7 @@ namespace Webshop.Application.Services.Admin
LastModifiedDate = product.LastModifiedDate,
SupplierId = product.SupplierId,
PurchasePrice = product.PurchasePrice,
CategoryIds = product.Productcategorys.Select(pc => pc.CategoryId).ToList() // << NEU: Mappe die CategoryIds >>
categorieIds = product.Productcategories.Select(pc => pc.categorieId).ToList() // << NEU: Mappe die categorieIds >>
};
}
@@ -101,13 +101,13 @@ namespace Webshop.Application.Services.Admin
CreatedDate = DateTimeOffset.UtcNow,
SupplierId = productDto.SupplierId,
PurchasePrice = productDto.PurchasePrice,
Productcategorys = new List<ProductCategory>() // Initialisiere die Collection
Productcategories = new List<Productcategorie>() // Initialisiere die Collection
};
// << NEU: F<>ge die Kategorien hinzu >>
foreach (var categoryId in productDto.CategoryIds)
foreach (var categorieId in productDto.categorieIds)
{
newProduct.Productcategorys.Add(new ProductCategory { CategoryId = categoryId });
newProduct.Productcategories.Add(new Productcategorie { categorieId = categorieId });
}
await _productRepository.AddProductAsync(newProduct); // << KORREKT: VERWENDET AddProductAsync >>
@@ -119,7 +119,7 @@ namespace Webshop.Application.Services.Admin
public async Task<bool> UpdateAdminProductAsync(AdminProductDto productDto)
{
var existingProduct = await _context.Products
.Include(p => p.Productcategorys) // Lade die aktuellen Zuweisungen
.Include(p => p.Productcategories) // Lade die aktuellen Zuweisungen
.FirstOrDefaultAsync(p => p.Id == productDto.Id);
if (existingProduct == null) return false;
@@ -141,10 +141,10 @@ namespace Webshop.Application.Services.Admin
existingProduct.LastModifiedDate = DateTimeOffset.UtcNow;
// << NEU: Kategorien synchronisieren (alte l<>schen, neue hinzuf<75>gen) >>
existingProduct.Productcategorys.Clear();
foreach (var categoryId in productDto.CategoryIds)
existingProduct.Productcategories.Clear();
foreach (var categorieId in productDto.categorieIds)
{
existingProduct.Productcategorys.Add(new ProductCategory { ProductId = existingProduct.Id, CategoryId = categoryId });
existingProduct.Productcategories.Add(new Productcategorie { ProductId = existingProduct.Id, categorieId = categorieId });
}
// << ENDE NEUER TEIL >>

View File

@@ -0,0 +1,17 @@
// src/Webshop.Application/Services/Admin/IAdmincategorieservice.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application.DTOs.categories;
namespace Webshop.Application.Services.Admin
{
public interface IAdminCategorieService
{
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);
}
}

View File

@@ -1,17 +0,0 @@
// src/Webshop.Application/Services/Admin/IAdminCategoryService.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Categorys;
namespace Webshop.Application.Services.Admin
{
public interface IAdminCategoryService
{
Task<IEnumerable<CategoryDto>> GetAllAsync();
Task<CategoryDto?> GetByIdAsync(Guid id);
Task<(CategoryDto? CreatedCategory, string? ErrorMessage)> CreateAsync(CreateCategoryDto categoryDto);
Task<(bool Success, string? ErrorMessage)> UpdateAsync(Guid id, CreateCategoryDto categoryDto);
Task<bool> DeleteAsync(Guid id);
}
}