// src/Webshop.Api/Controllers/Admin/AdminCategoriesController.cs using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Threading.Tasks; using Webshop.Application; 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] [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] public async Task GetAllcategories() { var result = await _adminCategorieService.GetAllAsync(); // Ein 'GetAll' schlägt im Normalfall nicht fehl (gibt leere Liste zurück), // daher wird hier nur der Erfolgsfall mit dem Wert zurückgegeben. return Ok(result.Value); } [HttpGet("{id}")] [ProducesResponseType(typeof(CategorieDto), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] public async Task GetcategorieById(Guid id) { var result = await _adminCategorieService.GetByIdAsync(id); return result.Type switch { ServiceResultType.Success => Ok(result.Value), ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }), _ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." }) }; } [HttpPost] [Consumes("multipart/form-data")] [ProducesResponseType(typeof(CategorieDto), StatusCodes.Status201Created)] [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status409Conflict)] public async Task CreateCategorie([FromForm] CreatecategorieDto categorieDto) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var result = await _adminCategorieService.CreateAsync(categorieDto); return result.Type switch { ServiceResultType.Success => CreatedAtAction(nameof(GetcategorieById), new { id = result.Value!.Id }, result.Value), ServiceResultType.Conflict => Conflict(new { Message = result.ErrorMessage }), ServiceResultType.InvalidInput => BadRequest(new { Message = result.ErrorMessage }), _ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." }) }; } [HttpPut("{id}")] [Consumes("multipart/form-data")] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status409Conflict)] public async Task UpdateCategorie(Guid id, [FromForm] UpdatecategorieDto categorieDto) { if (id != categorieDto.Id) { return BadRequest(new { Message = "ID in der URL und im Body stimmen nicht überein." }); } if (!ModelState.IsValid) { return BadRequest(ModelState); } var result = await _adminCategorieService.UpdateAsync(categorieDto); return result.Type switch { ServiceResultType.Success => NoContent(), ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }), ServiceResultType.Conflict => Conflict(new { Message = result.ErrorMessage }), ServiceResultType.InvalidInput => BadRequest(new { Message = result.ErrorMessage }), _ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." }) }; } [HttpDelete("{id}")] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status409Conflict)] public async Task DeleteCategorie(Guid id) { var result = await _adminCategorieService.DeleteAsync(id); return result.Type switch { ServiceResultType.Success => NoContent(), ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }), ServiceResultType.Conflict => Conflict(new { Message = result.ErrorMessage }), _ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." }) }; } } }