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 // src/Webshop.Api/Controllers/Admin/AdminCategoriesController.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Webshop.Application.DTOs.Categorys;
using Webshop.Application.Services.Admin;
namespace Webshop.Api.Controllers.Admin namespace Webshop.Api.Controllers.Admin
{ {
[ApiController] [ApiController]
[Route("api/v1/admin/[controller]")] [Route("api/v1/admin/categories")]
[Authorize(Roles = "Admin")] [Authorize(Roles = "Admin")]
public class AdminCategoriesController : ControllerBase 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 // src/Webshop.Api/Controllers/Public/CategoriesController.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Webshop.Application.DTOs.Categorys;
using Webshop.Application.Services.Public;
namespace Webshop.Api.Controllers.Public namespace Webshop.Api.Controllers.Public
{ {
[ApiController] [ApiController]
[Route("api/v1/public/[controller]")] [Route("api/v1/public/categories")]
[AllowAnonymous] [AllowAnonymous]
public class CategoriesController : ControllerBase 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<ISupplierRepository, SupplierRepository>();
builder.Services.AddScoped<ICustomerRepository, CustomerRepository>(); builder.Services.AddScoped<ICustomerRepository, CustomerRepository>();
builder.Services.AddScoped<IPaymentMethodRepository, PaymentMethodRepository>(); builder.Services.AddScoped<IPaymentMethodRepository, PaymentMethodRepository>();
builder.Services.AddScoped<ICategoryRepository, CategoryRepository>();
// AUTH Services // AUTH Services
builder.Services.AddScoped<IAuthService, AuthService>(); builder.Services.AddScoped<IAuthService, AuthService>();
@@ -89,13 +90,14 @@ builder.Services.AddScoped<IAuthService, AuthService>();
builder.Services.AddScoped<IProductService, ProductService>(); builder.Services.AddScoped<IProductService, ProductService>();
builder.Services.AddScoped<IPaymentMethodService, PaymentMethodService>(); builder.Services.AddScoped<IPaymentMethodService, PaymentMethodService>();
builder.Services.AddScoped<ICategoryService, CategoryService>(); builder.Services.AddScoped<ICategoryService, CategoryService>();
builder.Services.AddScoped<ICategoryService, CategoryService>();
// ADMIN Services // ADMIN Services
builder.Services.AddScoped<IAdminUserService, AdminUserService>(); builder.Services.AddScoped<IAdminUserService, AdminUserService>();
builder.Services.AddScoped<IAdminProductService, AdminProductService>(); builder.Services.AddScoped<IAdminProductService, AdminProductService>();
builder.Services.AddScoped<IAdminSupplierService, AdminSupplierService>(); builder.Services.AddScoped<IAdminSupplierService, AdminSupplierService>();
builder.Services.AddScoped<IAdminPaymentMethodService, AdminPaymentMethodService>(); 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<IAdminDiscountService, AdminDiscountService>(); // Hinzugef<65>gt f<>r Konsistenz
//builder.Services.AddScoped<IAdminOrderService, AdminOrderService>(); // 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 //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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; 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 namespace Webshop.Application.Services.Admin
{ {
public class AdminCategoryService : IAdminCategoryService 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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Webshop.Application.DTOs; using Webshop.Application.DTOs.Categorys;
using Webshop.Application.DTOs.Auth;
using Webshop.Application.DTOs.Users;
namespace Webshop.Application.Services.Admin
namespace Webshop.Application.Services.Admin.Interfaces
{ {
public interface IAdminCategoryService 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 // src/Webshop.Application/Services/Public/CategoryService.cs
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Webshop.Application.Services.Public.Interfaces; using Webshop.Application.DTOs.Categorys;
using Webshop.Domain.Interfaces;
namespace Webshop.Application.Services.Public namespace Webshop.Application.Services.Public
{ {
public class CategoryService : ICategoryService 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 // src/Webshop.Application/Services/Public/ICategoryService.cs
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Webshop.Application.DTOs; using Webshop.Application.DTOs.Categorys;
using Webshop.Application.DTOs.Auth;
using Webshop.Application.DTOs.Users;
namespace Webshop.Application.Services.Public
namespace Webshop.Application.Services.Public.Interfaces
{ {
public interface ICategoryService public interface ICategoryService
{ {
// Fügen Sie hier Methodensignaturen hinzu Task<IEnumerable<CategoryDto>> GetAllActiveAsync();
Task<CategoryDto?> GetBySlugAsync(string slug);
} }
} }

View File

@@ -1,29 +1,29 @@
using System; // src/Webshop.Domain/Entities/Category.cs
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
namespace Webshop.Domain.Entities; namespace Webshop.Domain.Entities
{
/// <summary> /// <summary>
/// Zum Gruppieren und Organisieren von Produkten. /// Zum Gruppieren und Organisieren von Produkten.
/// </summary> /// </summary>
public class Category public class Category
{ {
[Key] [Key]
public Guid Id { get; set; } public Guid Id { get; set; } = Guid.NewGuid(); // Hinzufügen von Default-Wert
[Required] [Required]
[MaxLength(255)] [MaxLength(255)]
public string Name { get; set; } public string Name { get; set; } = string.Empty; // Hinzufügen von Default-Wert
[MaxLength(1000)] [MaxLength(1000)]
public string? Description { get; set; } public string? Description { get; set; }
// Unique-Constraint wird typischerweise via Fluent API konfiguriert
[Required] [Required]
[MaxLength(255)] [MaxLength(255)]
public string Slug { get; set; } public string Slug { get; set; } = string.Empty; // Hinzufügen von Default-Wert
[ForeignKey(nameof(ParentCategory))] [ForeignKey(nameof(ParentCategory))]
public Guid? ParentCategoryId { get; set; } public Guid? ParentCategoryId { get; set; }
@@ -37,9 +37,15 @@ public class Category
[Required] [Required]
public int DisplayOrder { get; set; } public int DisplayOrder { get; set; }
// << NEUE EIGENSCHAFTEN HINZUFÜGEN >>
public DateTimeOffset CreatedDate { get; set; } = DateTimeOffset.UtcNow;
public DateTimeOffset? LastModifiedDate { get; set; }
// << ENDE NEUE EIGENSCHAFTEN >>
// Navigation Properties // Navigation Properties
public virtual Category? ParentCategory { get; set; } public virtual Category? ParentCategory { get; set; }
public virtual ICollection<Category> SubCategories { get; set; } = new List<Category>(); public virtual ICollection<Category> SubCategories { get; set; } = new List<Category>();
public virtual ICollection<ProductCategory> ProductCategories { get; set; } = new List<ProductCategory>(); public virtual ICollection<ProductCategory> ProductCategories { get; set; } = new List<ProductCategory>();
public virtual ICollection<CategoryDiscount> CategoryDiscounts { get; set; } = new List<CategoryDiscount>(); 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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Webshop.Domain.Entities; using Webshop.Domain.Entities;
namespace Webshop.Domain.Interfaces namespace Webshop.Domain.Interfaces
{ {
public interface ICategoryRepository 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 namespace Webshop.Infrastructure.Migrations
{ {
[DbContext(typeof(ApplicationDbContext))] [DbContext(typeof(ApplicationDbContext))]
[Migration("20250729184055_InitialCreate")] [Migration("20250731131435_addCategory")]
partial class InitialCreate partial class addCategory
{ {
/// <inheritdoc /> /// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
@@ -225,6 +225,9 @@ namespace Webshop.Infrastructure.Migrations
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
.HasColumnType("uuid"); .HasColumnType("uuid");
b.Property<DateTimeOffset>("CreatedDate")
.HasColumnType("timestamp with time zone");
b.Property<string>("Description") b.Property<string>("Description")
.HasMaxLength(1000) .HasMaxLength(1000)
.HasColumnType("character varying(1000)"); .HasColumnType("character varying(1000)");
@@ -239,6 +242,9 @@ namespace Webshop.Infrastructure.Migrations
b.Property<bool>("IsActive") b.Property<bool>("IsActive")
.HasColumnType("boolean"); .HasColumnType("boolean");
b.Property<DateTimeOffset?>("LastModifiedDate")
.HasColumnType("timestamp with time zone");
b.Property<string>("Name") b.Property<string>("Name")
.IsRequired() .IsRequired()
.HasMaxLength(255) .HasMaxLength(255)

View File

@@ -7,7 +7,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace Webshop.Infrastructure.Migrations namespace Webshop.Infrastructure.Migrations
{ {
/// <inheritdoc /> /// <inheritdoc />
public partial class InitialCreate : Migration public partial class addCategory : Migration
{ {
/// <inheritdoc /> /// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder) protected override void Up(MigrationBuilder migrationBuilder)
@@ -23,7 +23,9 @@ namespace Webshop.Infrastructure.Migrations
ParentCategoryId = table.Column<Guid>(type: "uuid", nullable: true), ParentCategoryId = table.Column<Guid>(type: "uuid", nullable: true),
ImageUrl = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true), ImageUrl = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true),
IsActive = table.Column<bool>(type: "boolean", nullable: false), 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 => constraints: table =>
{ {

View File

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

View File

@@ -1,11 +1,11 @@
// Auto-generiert von CreateWebshopFiles.ps1 // src/Webshop.Infrastructure/Repositories/CategoryRepository.cs
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces;
using Webshop.Infrastructure.Data;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces;
using Webshop.Infrastructure.Data;
namespace Webshop.Infrastructure.Repositories namespace Webshop.Infrastructure.Repositories
{ {
@@ -18,12 +18,41 @@ namespace Webshop.Infrastructure.Repositories
_context = context; _context = context;
} }
// Fügen Sie hier Repository-Methoden hinzu public async Task<IEnumerable<Category>> GetAllAsync()
// Beispiel: {
// public async Task<IEnumerable<T>> GetAllAsync() { return await _context.Set<T>().ToListAsync(); } return await _context.Categories.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<Category?> GetByIdAsync(Guid id)
// 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(); } } {
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();
}
}
} }
} }