// 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.Categorie; using Webshop.Application.Services.Admin; namespace Webshop.Api.Controllers.Admin { [ApiController] [Route("api/v1/admin/[controller]")] [Authorize(Roles = "Admin")] public class AdminCategoriesController : ControllerBase { private readonly IAdminCategorieService _adminCategorieService; public AdminCategoriesController(IAdminCategorieService admincategorieservice) { _adminCategorieService = admincategorieservice; } [HttpGet] public async Task>> GetAllcategories() { var categories = await _adminCategorieService.GetAllAsync(); return Ok(categories); } [HttpGet("{id}")] public async Task> GetcategorieById(Guid id) { var categorie = await _adminCategorieService.GetByIdAsync(id); if (categorie == null) return NotFound(); return Ok(categorie); } [HttpPost] public async Task> Createcategorie([FromBody] CreatecategorieDto categorieDto) { if (!ModelState.IsValid) return BadRequest(ModelState); var (createdcategorie, errorMessage) = await _adminCategorieService.CreateAsync(categorieDto); if (createdcategorie == null) { return BadRequest(new { Message = errorMessage }); } return CreatedAtAction(nameof(GetcategorieById), new { id = createdcategorie.Id }, createdcategorie); } [HttpPut("{id}")] public async Task Updatecategorie(Guid id, [FromBody] CreatecategorieDto categorieDto) { if (!ModelState.IsValid) return BadRequest(ModelState); var (success, errorMessage) = await _adminCategorieService.UpdateAsync(id, categorieDto); if (!success) { return BadRequest(new { Message = errorMessage }); } return NoContent(); } [HttpDelete("{id}")] public async Task Deletecategorie(Guid id) { var success = await _adminCategorieService.DeleteAsync(id); if (!success) return NotFound(); return NoContent(); } } }