categorys

This commit is contained in:
Tizian.Breuch
2025-07-31 15:14:51 +02:00
parent b608d116f0
commit 50ae33c258
13 changed files with 377 additions and 95 deletions

View File

@@ -1,18 +1,77 @@
// Auto-generiert von CreateWebshopFiles.ps1
using Microsoft.AspNetCore.Mvc;
// src/Webshop.Api/Controllers/Admin/AdminCategoriesController.cs
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Categorys;
using Webshop.Application.Services.Admin;
namespace Webshop.Api.Controllers.Admin
{
[ApiController]
[Route("api/v1/admin/[controller]")]
[Route("api/v1/admin/categories")]
[Authorize(Roles = "Admin")]
public class AdminCategoriesController : ControllerBase
{
private readonly IAdminCategoryService _adminCategoryService;
public AdminCategoriesController(IAdminCategoryService adminCategoryService)
{
_adminCategoryService = adminCategoryService;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<CategoryDto>>> GetAllCategories()
{
var categories = await _adminCategoryService.GetAllAsync();
return Ok(categories);
}
[HttpGet("{id}")]
public async Task<ActionResult<CategoryDto>> GetCategoryById(Guid id)
{
var category = await _adminCategoryService.GetByIdAsync(id);
if (category == null) return NotFound();
return Ok(category);
}
[HttpPost]
public async Task<ActionResult<CategoryDto>> CreateCategory([FromBody] CreateCategoryDto categoryDto)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var (createdCategory, errorMessage) = await _adminCategoryService.CreateAsync(categoryDto);
if (createdCategory == null)
{
return BadRequest(new { Message = errorMessage });
}
return CreatedAtAction(nameof(GetCategoryById), new { id = createdCategory.Id }, createdCategory);
}
[HttpPut("{id}")]
public async Task<IActionResult> UpdateCategory(Guid id, [FromBody] CreateCategoryDto categoryDto)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var (success, errorMessage) = await _adminCategoryService.UpdateAsync(id, categoryDto);
if (!success)
{
return BadRequest(new { Message = errorMessage });
}
return NoContent();
}
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteCategory(Guid id)
{
var success = await _adminCategoryService.DeleteAsync(id);
if (!success) return NotFound();
return NoContent();
}
}
}

View File

@@ -1,18 +1,38 @@
// Auto-generiert von CreateWebshopFiles.ps1
using Microsoft.AspNetCore.Mvc;
// src/Webshop.Api/Controllers/Public/CategoriesController.cs
using Microsoft.AspNetCore.Authorization;
using System;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Categorys;
using Webshop.Application.Services.Public;
namespace Webshop.Api.Controllers.Public
{
[ApiController]
[Route("api/v1/public/[controller]")]
[Route("api/v1/public/categories")]
[AllowAnonymous]
public class CategoriesController : ControllerBase
{
private readonly ICategoryService _categoryService;
public CategoriesController(ICategoryService categoryService)
{
_categoryService = categoryService;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<CategoryDto>>> GetActiveCategories()
{
var categories = await _categoryService.GetAllActiveAsync();
return Ok(categories);
}
[HttpGet("{slug}")]
public async Task<ActionResult<CategoryDto>> GetCategoryBySlug(string slug)
{
var category = await _categoryService.GetBySlugAsync(slug);
if (category == null) return NotFound();
return Ok(category);
}
}
}

View File

@@ -81,6 +81,7 @@ builder.Services.AddScoped<IProductRepository, ProductRepository>();
builder.Services.AddScoped<ISupplierRepository, SupplierRepository>();
builder.Services.AddScoped<ICustomerRepository, CustomerRepository>();
builder.Services.AddScoped<IPaymentMethodRepository, PaymentMethodRepository>();
builder.Services.AddScoped<ICategoryRepository, CategoryRepository>();
// AUTH Services
builder.Services.AddScoped<IAuthService, AuthService>();
@@ -89,13 +90,14 @@ builder.Services.AddScoped<IAuthService, AuthService>();
builder.Services.AddScoped<IProductService, ProductService>();
builder.Services.AddScoped<IPaymentMethodService, PaymentMethodService>();
builder.Services.AddScoped<ICategoryService, CategoryService>();
builder.Services.AddScoped<ICategoryService, CategoryService>();
// ADMIN Services
builder.Services.AddScoped<IAdminUserService, AdminUserService>();
builder.Services.AddScoped<IAdminProductService, AdminProductService>();
builder.Services.AddScoped<IAdminSupplierService, AdminSupplierService>();
builder.Services.AddScoped<IAdminPaymentMethodService, AdminPaymentMethodService>();
//builder.Services.AddScoped<IAdminCategoryService, AdminCategoryService>(); // Hinzugef<65>gt f<>r Konsistenz
builder.Services.AddScoped<IAdminCategoryService, AdminCategoryService>(); // Hinzugef<65>gt f<>r Konsistenz
//builder.Services.AddScoped<IAdminDiscountService, AdminDiscountService>(); // Hinzugef<65>gt f<>r Konsistenz
//builder.Services.AddScoped<IAdminOrderService, AdminOrderService>(); // Hinzugef<65>gt f<>r Konsistenz
//builder.Services.AddScoped<IAdminSettingService, AdminSettingService>(); // Hinzugef<65>gt f<>r Konsistenz

View File

@@ -1,18 +1,129 @@
// Auto-generiert von CreateWebshopFiles.ps1
// src/Webshop.Application/Services/Admin/AdminCategoryService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Webshop.Application.Services.Admin.Interfaces;
using Webshop.Application.DTOs.Categorys;
using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces;
namespace Webshop.Application.Services.Admin
{
public class AdminCategoryService : IAdminCategoryService
{
// Fügen Sie hier Abhängigkeiten per Dependency Injection hinzu (z.B. Repositories)
private readonly ICategoryRepository _categoryRepository;
// public AdminCategoryService(IYourRepository repository) { }
public AdminCategoryService(ICategoryRepository categoryRepository)
{
_categoryRepository = categoryRepository;
}
// Fügen Sie hier Service-Methoden hinzu
public async Task<IEnumerable<CategoryDto>> GetAllAsync()
{
var categories = await _categoryRepository.GetAllAsync();
return categories.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

@@ -1,16 +1,17 @@
// Auto-generiert von CreateWebshopFiles.ps1
// src/Webshop.Application/Services/Admin/IAdminCategoryService.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application.DTOs;
using Webshop.Application.DTOs.Auth;
using Webshop.Application.DTOs.Users;
using Webshop.Application.DTOs.Categorys;
namespace Webshop.Application.Services.Admin.Interfaces
namespace Webshop.Application.Services.Admin
{
public interface IAdminCategoryService
{
// Fügen Sie hier Methodensignaturen hinzu
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);
}
}

View File

@@ -1,18 +1,57 @@
// Auto-generiert von CreateWebshopFiles.ps1
using System;
// src/Webshop.Application/Services/Public/CategoryService.cs
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Webshop.Application.Services.Public.Interfaces;
using Webshop.Application.DTOs.Categorys;
using Webshop.Domain.Interfaces;
namespace Webshop.Application.Services.Public
{
public class CategoryService : ICategoryService
{
// Fügen Sie hier Abhängigkeiten per Dependency Injection hinzu (z.B. Repositories)
private readonly ICategoryRepository _categoryRepository;
// public CategoryService(IYourRepository repository) { }
public CategoryService(ICategoryRepository categoryRepository)
{
_categoryRepository = categoryRepository;
}
// Fügen Sie hier Service-Methoden hinzu
public async Task<IEnumerable<CategoryDto>> GetAllActiveAsync()
{
var categories = await _categoryRepository.GetAllAsync();
// Hier könnte man eine Baumstruktur aufbauen, für den Anfang eine flache Liste
return categories
.Where(c => c.IsActive)
.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?> GetBySlugAsync(string slug)
{
var category = await _categoryRepository.GetBySlugAsync(slug);
if (category == null || !category.IsActive) 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
};
}
}
}

View File

@@ -1,16 +1,13 @@
// Auto-generiert von CreateWebshopFiles.ps1
using System;
// src/Webshop.Application/Services/Public/ICategoryService.cs
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application.DTOs;
using Webshop.Application.DTOs.Auth;
using Webshop.Application.DTOs.Users;
using Webshop.Application.DTOs.Categorys;
namespace Webshop.Application.Services.Public.Interfaces
namespace Webshop.Application.Services.Public
{
public interface ICategoryService
{
// Fügen Sie hier Methodensignaturen hinzu
Task<IEnumerable<CategoryDto>> GetAllActiveAsync();
Task<CategoryDto?> GetBySlugAsync(string slug);
}
}

View File

@@ -1,45 +1,51 @@
using System;
// src/Webshop.Domain/Entities/Category.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Webshop.Domain.Entities;
/// <summary>
/// Zum Gruppieren und Organisieren von Produkten.
/// </summary>
public class Category
namespace Webshop.Domain.Entities
{
[Key]
public Guid Id { get; set; }
/// <summary>
/// Zum Gruppieren und Organisieren von Produkten.
/// </summary>
public class Category
{
[Key]
public Guid Id { get; set; } = Guid.NewGuid(); // Hinzufügen von Default-Wert
[Required]
[MaxLength(255)]
public string Name { get; set; }
[Required]
[MaxLength(255)]
public string Name { get; set; } = string.Empty; // Hinzufügen von Default-Wert
[MaxLength(1000)]
public string? Description { get; set; }
[MaxLength(1000)]
public string? Description { get; set; }
// Unique-Constraint wird typischerweise via Fluent API konfiguriert
[Required]
[MaxLength(255)]
public string Slug { get; set; }
[Required]
[MaxLength(255)]
public string Slug { get; set; } = string.Empty; // Hinzufügen von Default-Wert
[ForeignKey(nameof(ParentCategory))]
public Guid? ParentCategoryId { get; set; }
[ForeignKey(nameof(ParentCategory))]
public Guid? ParentCategoryId { get; set; }
[MaxLength(2000)]
public string? ImageUrl { get; set; }
[MaxLength(2000)]
public string? ImageUrl { get; set; }
[Required]
public bool IsActive { get; set; }
[Required]
public bool IsActive { get; set; }
[Required]
public int DisplayOrder { get; set; }
[Required]
public int DisplayOrder { get; set; }
// Navigation Properties
public virtual Category? ParentCategory { get; set; }
public virtual ICollection<Category> SubCategories { get; set; } = new List<Category>();
public virtual ICollection<ProductCategory> ProductCategories { get; set; } = new List<ProductCategory>();
public virtual ICollection<CategoryDiscount> CategoryDiscounts { get; set; } = new List<CategoryDiscount>();
// << NEUE EIGENSCHAFTEN HINZUFÜGEN >>
public DateTimeOffset CreatedDate { get; set; } = DateTimeOffset.UtcNow;
public DateTimeOffset? LastModifiedDate { get; set; }
// << ENDE NEUE EIGENSCHAFTEN >>
// Navigation Properties
public virtual Category? ParentCategory { get; set; }
public virtual ICollection<Category> SubCategories { get; set; } = new List<Category>();
public virtual ICollection<ProductCategory> ProductCategories { get; set; } = new List<ProductCategory>();
public virtual ICollection<CategoryDiscount> CategoryDiscounts { get; set; } = new List<CategoryDiscount>();
}
}

View File

@@ -1,14 +1,18 @@
// Auto-generiert von CreateWebshopFiles.ps1
// src/Webshop.Domain/Interfaces/ICategoryRepository.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Domain.Entities;
namespace Webshop.Domain.Interfaces
{
public interface ICategoryRepository
{
// Fügen Sie hier Methodensignaturen hinzu
Task<IEnumerable<Category>> GetAllAsync();
Task<Category?> GetByIdAsync(Guid id);
Task<Category?> GetBySlugAsync(string slug);
Task AddAsync(Category category);
Task UpdateAsync(Category category);
Task DeleteAsync(Guid id);
}
}

View File

@@ -12,8 +12,8 @@ using Webshop.Infrastructure.Data;
namespace Webshop.Infrastructure.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20250729184055_InitialCreate")]
partial class InitialCreate
[Migration("20250731131435_addCategory")]
partial class addCategory
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@@ -225,6 +225,9 @@ namespace Webshop.Infrastructure.Migrations
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateTimeOffset>("CreatedDate")
.HasColumnType("timestamp with time zone");
b.Property<string>("Description")
.HasMaxLength(1000)
.HasColumnType("character varying(1000)");
@@ -239,6 +242,9 @@ namespace Webshop.Infrastructure.Migrations
b.Property<bool>("IsActive")
.HasColumnType("boolean");
b.Property<DateTimeOffset?>("LastModifiedDate")
.HasColumnType("timestamp with time zone");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(255)

View File

@@ -7,7 +7,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace Webshop.Infrastructure.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
public partial class addCategory : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
@@ -23,7 +23,9 @@ namespace Webshop.Infrastructure.Migrations
ParentCategoryId = table.Column<Guid>(type: "uuid", nullable: true),
ImageUrl = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true),
IsActive = table.Column<bool>(type: "boolean", nullable: false),
DisplayOrder = table.Column<int>(type: "integer", nullable: false)
DisplayOrder = table.Column<int>(type: "integer", nullable: false),
CreatedDate = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
LastModifiedDate = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true)
},
constraints: table =>
{

View File

@@ -222,6 +222,9 @@ namespace Webshop.Infrastructure.Migrations
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateTimeOffset>("CreatedDate")
.HasColumnType("timestamp with time zone");
b.Property<string>("Description")
.HasMaxLength(1000)
.HasColumnType("character varying(1000)");
@@ -236,6 +239,9 @@ namespace Webshop.Infrastructure.Migrations
b.Property<bool>("IsActive")
.HasColumnType("boolean");
b.Property<DateTimeOffset?>("LastModifiedDate")
.HasColumnType("timestamp with time zone");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(255)

View File

@@ -1,11 +1,11 @@
// Auto-generiert von CreateWebshopFiles.ps1
// src/Webshop.Infrastructure/Repositories/CategoryRepository.cs
using Microsoft.EntityFrameworkCore;
using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces;
using Webshop.Infrastructure.Data;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces;
using Webshop.Infrastructure.Data;
namespace Webshop.Infrastructure.Repositories
{
@@ -18,12 +18,41 @@ namespace Webshop.Infrastructure.Repositories
_context = context;
}
// Fügen Sie hier Repository-Methoden hinzu
// Beispiel:
// public async Task<IEnumerable<T>> GetAllAsync() { return await _context.Set<T>().ToListAsync(); }
// public async Task<T?> GetByIdAsync(Guid id) { return await _context.Set<T>().FindAsync(id); }
// public async Task AddAsync(T entity) { _context.Set<T>().Add(entity); await _context.SaveChangesAsync(); }
// public async Task UpdateAsync(T entity) { _context.Set<T>().Update(entity); await _context.SaveChangesAsync(); }
// public async Task DeleteAsync(Guid id) { var entity = await _context.Set<T>().FindAsync(id); if (entity != null) { _context.Set<T>().Remove(entity); await _context.SaveChangesAsync(); } }
public async Task<IEnumerable<Category>> GetAllAsync()
{
return await _context.Categories.ToListAsync();
}
public async Task<Category?> GetByIdAsync(Guid id)
{
return await _context.Categories.FindAsync(id);
}
public async Task<Category?> GetBySlugAsync(string slug)
{
return await _context.Categories.FirstOrDefaultAsync(c => c.Slug == slug);
}
public async Task AddAsync(Category category)
{
_context.Categories.Add(category);
await _context.SaveChangesAsync();
}
public async Task UpdateAsync(Category category)
{
_context.Categories.Update(category);
await _context.SaveChangesAsync();
}
public async Task DeleteAsync(Guid id)
{
var category = await _context.Categories.FindAsync(id);
if (category != null)
{
_context.Categories.Remove(category);
await _context.SaveChangesAsync();
}
}
}
}