From 50ae33c258116846f87e41888799236dfc5753ac Mon Sep 17 00:00:00 2001 From: "Tizian.Breuch" Date: Thu, 31 Jul 2025 15:14:51 +0200 Subject: [PATCH] categorys --- .../Admin/AdminCategoriesController.cs | 69 +++++++++- .../Public/CategoriesController.cs | 32 ++++- Webshop.Api/Program.cs | 4 +- .../Services/Admin/AdminCategoryService.cs | 125 +++++++++++++++++- .../Admin/Interfaces/IAdminCategoryService.cs | 17 +-- .../Services/Public/CategoryService.cs | 55 ++++++-- .../Public/Interfaces/ICategoryService.cs | 15 +-- Webshop.Domain/Entities/Category.cs | 68 +++++----- .../Interfaces/ICategoryRepository.cs | 12 +- ...=> 20250731131435_addCategory.Designer.cs} | 10 +- ...reate.cs => 20250731131435_addCategory.cs} | 6 +- .../ApplicationDbContextModelSnapshot.cs | 6 + .../Repositories/CategoryRepository.cs | 53 ++++++-- 13 files changed, 377 insertions(+), 95 deletions(-) rename Webshop.Infrastructure/Migrations/{20250729184055_InitialCreate.Designer.cs => 20250731131435_addCategory.Designer.cs} (99%) rename Webshop.Infrastructure/Migrations/{20250729184055_InitialCreate.cs => 20250731131435_addCategory.cs} (99%) diff --git a/Webshop.Api/Controllers/Admin/AdminCategoriesController.cs b/Webshop.Api/Controllers/Admin/AdminCategoriesController.cs index a9479c4..71a5600 100644 --- a/Webshop.Api/Controllers/Admin/AdminCategoriesController.cs +++ b/Webshop.Api/Controllers/Admin/AdminCategoriesController.cs @@ -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>> GetAllCategories() + { + var categories = await _adminCategoryService.GetAllAsync(); + return Ok(categories); + } + + [HttpGet("{id}")] + public async Task> GetCategoryById(Guid id) + { + var category = await _adminCategoryService.GetByIdAsync(id); + if (category == null) return NotFound(); + return Ok(category); + } + + [HttpPost] + public async Task> 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 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 DeleteCategory(Guid id) + { + var success = await _adminCategoryService.DeleteAsync(id); + if (!success) return NotFound(); + return NoContent(); + } } -} +} \ No newline at end of file diff --git a/Webshop.Api/Controllers/Public/CategoriesController.cs b/Webshop.Api/Controllers/Public/CategoriesController.cs index 5cf9af6..c16b20e 100644 --- a/Webshop.Api/Controllers/Public/CategoriesController.cs +++ b/Webshop.Api/Controllers/Public/CategoriesController.cs @@ -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>> GetActiveCategories() + { + var categories = await _categoryService.GetAllActiveAsync(); + return Ok(categories); + } + + [HttpGet("{slug}")] + public async Task> GetCategoryBySlug(string slug) + { + var category = await _categoryService.GetBySlugAsync(slug); + if (category == null) return NotFound(); + return Ok(category); + } } -} +} \ No newline at end of file diff --git a/Webshop.Api/Program.cs b/Webshop.Api/Program.cs index 251e4eb..4b6b8ce 100644 --- a/Webshop.Api/Program.cs +++ b/Webshop.Api/Program.cs @@ -81,6 +81,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); // AUTH Services builder.Services.AddScoped(); @@ -89,13 +90,14 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); // ADMIN Services builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); -//builder.Services.AddScoped(); // Hinzugefügt für Konsistenz +builder.Services.AddScoped(); // Hinzugefügt für Konsistenz //builder.Services.AddScoped(); // Hinzugefügt für Konsistenz //builder.Services.AddScoped(); // Hinzugefügt für Konsistenz //builder.Services.AddScoped(); // Hinzugefügt für Konsistenz diff --git a/Webshop.Application/Services/Admin/AdminCategoryService.cs b/Webshop.Application/Services/Admin/AdminCategoryService.cs index 6615d23..be1b4b8 100644 --- a/Webshop.Application/Services/Admin/AdminCategoryService.cs +++ b/Webshop.Application/Services/Admin/AdminCategoryService.cs @@ -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> 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 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 DeleteAsync(Guid id) + { + var category = await _categoryRepository.GetByIdAsync(id); + if (category == null) return false; + + await _categoryRepository.DeleteAsync(id); + return true; + } } -} +} \ No newline at end of file diff --git a/Webshop.Application/Services/Admin/Interfaces/IAdminCategoryService.cs b/Webshop.Application/Services/Admin/Interfaces/IAdminCategoryService.cs index 4025c9d..c425709 100644 --- a/Webshop.Application/Services/Admin/Interfaces/IAdminCategoryService.cs +++ b/Webshop.Application/Services/Admin/Interfaces/IAdminCategoryService.cs @@ -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> GetAllAsync(); + Task GetByIdAsync(Guid id); + Task<(CategoryDto? CreatedCategory, string? ErrorMessage)> CreateAsync(CreateCategoryDto categoryDto); + Task<(bool Success, string? ErrorMessage)> UpdateAsync(Guid id, CreateCategoryDto categoryDto); + Task DeleteAsync(Guid id); } -} +} \ No newline at end of file diff --git a/Webshop.Application/Services/Public/CategoryService.cs b/Webshop.Application/Services/Public/CategoryService.cs index df8d106..52fe8e0 100644 --- a/Webshop.Application/Services/Public/CategoryService.cs +++ b/Webshop.Application/Services/Public/CategoryService.cs @@ -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> 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 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 + }; + } } -} +} \ No newline at end of file diff --git a/Webshop.Application/Services/Public/Interfaces/ICategoryService.cs b/Webshop.Application/Services/Public/Interfaces/ICategoryService.cs index 185e0a2..8be654d 100644 --- a/Webshop.Application/Services/Public/Interfaces/ICategoryService.cs +++ b/Webshop.Application/Services/Public/Interfaces/ICategoryService.cs @@ -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> GetAllActiveAsync(); + Task GetBySlugAsync(string slug); } -} +} \ No newline at end of file diff --git a/Webshop.Domain/Entities/Category.cs b/Webshop.Domain/Entities/Category.cs index b6bd12d..c948764 100644 --- a/Webshop.Domain/Entities/Category.cs +++ b/Webshop.Domain/Entities/Category.cs @@ -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; - -/// -/// Zum Gruppieren und Organisieren von Produkten. -/// -public class Category +namespace Webshop.Domain.Entities { - [Key] - public Guid Id { get; set; } + /// + /// Zum Gruppieren und Organisieren von Produkten. + /// + 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 SubCategories { get; set; } = new List(); - public virtual ICollection ProductCategories { get; set; } = new List(); - public virtual ICollection CategoryDiscounts { get; set; } = new List(); + // << 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 SubCategories { get; set; } = new List(); + public virtual ICollection ProductCategories { get; set; } = new List(); + public virtual ICollection CategoryDiscounts { get; set; } = new List(); + } } \ No newline at end of file diff --git a/Webshop.Domain/Interfaces/ICategoryRepository.cs b/Webshop.Domain/Interfaces/ICategoryRepository.cs index d577a2f..9034032 100644 --- a/Webshop.Domain/Interfaces/ICategoryRepository.cs +++ b/Webshop.Domain/Interfaces/ICategoryRepository.cs @@ -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> GetAllAsync(); + Task GetByIdAsync(Guid id); + Task GetBySlugAsync(string slug); + Task AddAsync(Category category); + Task UpdateAsync(Category category); + Task DeleteAsync(Guid id); } -} +} \ No newline at end of file diff --git a/Webshop.Infrastructure/Migrations/20250729184055_InitialCreate.Designer.cs b/Webshop.Infrastructure/Migrations/20250731131435_addCategory.Designer.cs similarity index 99% rename from Webshop.Infrastructure/Migrations/20250729184055_InitialCreate.Designer.cs rename to Webshop.Infrastructure/Migrations/20250731131435_addCategory.Designer.cs index 8e14d05..1f05d57 100644 --- a/Webshop.Infrastructure/Migrations/20250729184055_InitialCreate.Designer.cs +++ b/Webshop.Infrastructure/Migrations/20250731131435_addCategory.Designer.cs @@ -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 { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -225,6 +225,9 @@ namespace Webshop.Infrastructure.Migrations .ValueGeneratedOnAdd() .HasColumnType("uuid"); + b.Property("CreatedDate") + .HasColumnType("timestamp with time zone"); + b.Property("Description") .HasMaxLength(1000) .HasColumnType("character varying(1000)"); @@ -239,6 +242,9 @@ namespace Webshop.Infrastructure.Migrations b.Property("IsActive") .HasColumnType("boolean"); + b.Property("LastModifiedDate") + .HasColumnType("timestamp with time zone"); + b.Property("Name") .IsRequired() .HasMaxLength(255) diff --git a/Webshop.Infrastructure/Migrations/20250729184055_InitialCreate.cs b/Webshop.Infrastructure/Migrations/20250731131435_addCategory.cs similarity index 99% rename from Webshop.Infrastructure/Migrations/20250729184055_InitialCreate.cs rename to Webshop.Infrastructure/Migrations/20250731131435_addCategory.cs index ce9fe41..da289de 100644 --- a/Webshop.Infrastructure/Migrations/20250729184055_InitialCreate.cs +++ b/Webshop.Infrastructure/Migrations/20250731131435_addCategory.cs @@ -7,7 +7,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace Webshop.Infrastructure.Migrations { /// - public partial class InitialCreate : Migration + public partial class addCategory : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) @@ -23,7 +23,9 @@ namespace Webshop.Infrastructure.Migrations ParentCategoryId = table.Column(type: "uuid", nullable: true), ImageUrl = table.Column(type: "character varying(2000)", maxLength: 2000, nullable: true), IsActive = table.Column(type: "boolean", nullable: false), - DisplayOrder = table.Column(type: "integer", nullable: false) + DisplayOrder = table.Column(type: "integer", nullable: false), + CreatedDate = table.Column(type: "timestamp with time zone", nullable: false), + LastModifiedDate = table.Column(type: "timestamp with time zone", nullable: true) }, constraints: table => { diff --git a/Webshop.Infrastructure/Migrations/ApplicationDbContextModelSnapshot.cs b/Webshop.Infrastructure/Migrations/ApplicationDbContextModelSnapshot.cs index 0528b75..fdd306e 100644 --- a/Webshop.Infrastructure/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/Webshop.Infrastructure/Migrations/ApplicationDbContextModelSnapshot.cs @@ -222,6 +222,9 @@ namespace Webshop.Infrastructure.Migrations .ValueGeneratedOnAdd() .HasColumnType("uuid"); + b.Property("CreatedDate") + .HasColumnType("timestamp with time zone"); + b.Property("Description") .HasMaxLength(1000) .HasColumnType("character varying(1000)"); @@ -236,6 +239,9 @@ namespace Webshop.Infrastructure.Migrations b.Property("IsActive") .HasColumnType("boolean"); + b.Property("LastModifiedDate") + .HasColumnType("timestamp with time zone"); + b.Property("Name") .IsRequired() .HasMaxLength(255) diff --git a/Webshop.Infrastructure/Repositories/CategoryRepository.cs b/Webshop.Infrastructure/Repositories/CategoryRepository.cs index 31eadfc..0afc847 100644 --- a/Webshop.Infrastructure/Repositories/CategoryRepository.cs +++ b/Webshop.Infrastructure/Repositories/CategoryRepository.cs @@ -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> GetAllAsync() { return await _context.Set().ToListAsync(); } - // public async Task GetByIdAsync(Guid id) { return await _context.Set().FindAsync(id); } - // public async Task AddAsync(T entity) { _context.Set().Add(entity); await _context.SaveChangesAsync(); } - // public async Task UpdateAsync(T entity) { _context.Set().Update(entity); await _context.SaveChangesAsync(); } - // public async Task DeleteAsync(Guid id) { var entity = await _context.Set().FindAsync(id); if (entity != null) { _context.Set().Remove(entity); await _context.SaveChangesAsync(); } } + public async Task> GetAllAsync() + { + return await _context.Categories.ToListAsync(); + } + + public async Task GetByIdAsync(Guid id) + { + return await _context.Categories.FindAsync(id); + } + + public async Task 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(); + } + } } -} +} \ No newline at end of file