88 lines
3.3 KiB
C#
88 lines
3.3 KiB
C#
// 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;
|
||
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<ActionResult<IEnumerable<CategorieDto>>> GetAllcategories()
|
||
{
|
||
var categories = await _adminCategorieService.GetAllAsync();
|
||
return Ok(categories);
|
||
}
|
||
|
||
[HttpGet("{id}")]
|
||
public async Task<ActionResult<CategorieDto>> GetcategorieById(Guid id)
|
||
{
|
||
var categorie = await _adminCategorieService.GetByIdAsync(id);
|
||
if (categorie == null) return NotFound();
|
||
return Ok(categorie);
|
||
}
|
||
|
||
[HttpPost]
|
||
[Consumes("multipart/form-data")]
|
||
public async Task<ActionResult<CategorieDto>> CreateCategorie([FromForm] CreatecategorieDto categorieDto)
|
||
{
|
||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||
|
||
var result = await _adminCategorieService.CreateAsync(categorieDto);
|
||
|
||
if (result.Type == ServiceResultType.Success)
|
||
{
|
||
return CreatedAtAction(nameof(GetcategorieById), new { id = result.Value!.Id }, result.Value);
|
||
}
|
||
|
||
return BadRequest(new { Message = result.ErrorMessage });
|
||
}
|
||
|
||
[HttpPut("{id}")]
|
||
[Consumes("multipart/form-data")]
|
||
public async Task<IActionResult> UpdateCategorie(Guid id, [FromForm] UpdatecategorieDto categorieDto)
|
||
{
|
||
if (id != categorieDto.Id) return BadRequest("ID in URL und Body stimmen nicht <20>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.InvalidInput => BadRequest(new { Message = result.ErrorMessage }),
|
||
_ => StatusCode(500, "Ein unerwarteter Fehler ist aufgetreten.")
|
||
};
|
||
}
|
||
|
||
[HttpDelete("{id}")]
|
||
public async Task<IActionResult> 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(500, "Ein unerwarteter Fehler ist aufgetreten.")
|
||
};
|
||
}
|
||
}
|
||
} |