AdminCategorie
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
// src/Webshop.Api/Controllers/Admin/AdmincategoriesController.cs
|
||||
// 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;
|
||||
@@ -23,42 +24,70 @@ namespace Webshop.Api.Controllers.Admin
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<IEnumerable<CategorieDto>>> GetAllcategories()
|
||||
[ProducesResponseType(typeof(IEnumerable<CategorieDto>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetAllcategories()
|
||||
{
|
||||
var categories = await _adminCategorieService.GetAllAsync();
|
||||
return Ok(categories);
|
||||
var result = await _adminCategorieService.GetAllAsync();
|
||||
// Ein 'GetAll' schl<68>gt im Normalfall nicht fehl (gibt leere Liste zur<75>ck),
|
||||
// daher wird hier nur der Erfolgsfall mit dem Wert zur<75>ckgegeben.
|
||||
return Ok(result.Value);
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult<CategorieDto>> GetcategorieById(Guid id)
|
||||
[ProducesResponseType(typeof(CategorieDto), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetcategorieById(Guid id)
|
||||
{
|
||||
var categorie = await _adminCategorieService.GetByIdAsync(id);
|
||||
if (categorie == null) return NotFound();
|
||||
return Ok(categorie);
|
||||
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")]
|
||||
public async Task<ActionResult<CategorieDto>> CreateCategorie([FromForm] CreatecategorieDto categorieDto)
|
||||
[ProducesResponseType(typeof(CategorieDto), StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status409Conflict)]
|
||||
public async Task<IActionResult> CreateCategorie([FromForm] CreatecategorieDto categorieDto)
|
||||
{
|
||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
var result = await _adminCategorieService.CreateAsync(categorieDto);
|
||||
|
||||
if (result.Type == ServiceResultType.Success)
|
||||
return result.Type switch
|
||||
{
|
||||
return CreatedAtAction(nameof(GetcategorieById), new { id = result.Value!.Id }, result.Value);
|
||||
}
|
||||
|
||||
return BadRequest(new { Message = result.ErrorMessage });
|
||||
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<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);
|
||||
if (id != categorieDto.Id)
|
||||
{
|
||||
return BadRequest(new { Message = "ID in der URL und im Body stimmen nicht <20>berein." });
|
||||
}
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
var result = await _adminCategorieService.UpdateAsync(categorieDto);
|
||||
|
||||
@@ -66,12 +95,16 @@ namespace Webshop.Api.Controllers.Admin
|
||||
{
|
||||
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(500, "Ein unerwarteter Fehler ist aufgetreten.")
|
||||
_ => 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<IActionResult> DeleteCategorie(Guid id)
|
||||
{
|
||||
var result = await _adminCategorieService.DeleteAsync(id);
|
||||
@@ -81,7 +114,7 @@ namespace Webshop.Api.Controllers.Admin
|
||||
ServiceResultType.Success => NoContent(),
|
||||
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
|
||||
ServiceResultType.Conflict => Conflict(new { Message = result.ErrorMessage }),
|
||||
_ => StatusCode(500, "Ein unerwarteter Fehler ist aufgetreten.")
|
||||
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user