113 lines
3.8 KiB
C#
113 lines
3.8 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Project.Application.DTOs.Incoming;
|
|
using Project.Application.Interfaces;
|
|
|
|
namespace Project.Web.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class CategoryController : ControllerBase
|
|
{
|
|
|
|
// FIELDS FOR CTOR
|
|
private readonly ICategoryService _categoryService;
|
|
|
|
// CTOR
|
|
public CategoryController(ICategoryService categoryService)
|
|
{
|
|
_categoryService = categoryService;
|
|
}
|
|
|
|
// CREATE
|
|
[HttpPost]
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> CreateCategory([FromBody] CreatingCategoryDto creatingCategoryDto)
|
|
{
|
|
var result = await _categoryService.AddCategoryAsync(creatingCategoryDto);
|
|
|
|
if (result != null)
|
|
{
|
|
var id = result.Id;
|
|
var createdResource = new { Id = id };
|
|
var actionName = nameof(GetCategoryById);
|
|
var routeValue = new { id = createdResource.Id };
|
|
return CreatedAtAction(actionName, routeValue, createdResource);
|
|
}
|
|
else
|
|
{
|
|
return BadRequest("geht nix");
|
|
}
|
|
}
|
|
|
|
// READ ALL
|
|
[HttpGet]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
public async Task<IActionResult> GetCategories()
|
|
{
|
|
var categories = await _categoryService.GetAllAsync();
|
|
return Ok(categories);
|
|
}
|
|
|
|
// READ BY ID
|
|
[HttpGet("id/{id}", Name = "GetCategoryById")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
public async Task<IActionResult> GetCategoryById(int id)
|
|
{
|
|
if (id <= 0)
|
|
{
|
|
return BadRequest("Invalid Id");
|
|
}
|
|
var category = await _categoryService.GetByIdAsync(id);
|
|
if (category == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return Ok(category);
|
|
}
|
|
|
|
// READ BY NAME
|
|
[HttpGet("name/{name}", Name = "GetCategoryByName")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
public async Task<IActionResult> GetCategoryByName(string name)
|
|
{
|
|
if (string.IsNullOrEmpty(name))
|
|
{
|
|
return BadRequest("Name cannot be empty");
|
|
}
|
|
var category = await _categoryService.GetByNameAsync(name);
|
|
if (category == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return Ok(category);
|
|
}
|
|
|
|
// UPDATE
|
|
[HttpPut("id/{id}", Name = "UpdateCategory")]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
public async Task<IActionResult> UpdateCategory(int id, UpdatingCategoryDto updatingCategoryDto)
|
|
{
|
|
var updated = await _categoryService.UpdateCategoryAsync(updatingCategoryDto);
|
|
return Ok(updated);
|
|
}
|
|
|
|
// DELETE
|
|
[HttpDelete("id/{id}", Name = "DeleteCategory")]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
public async Task<IActionResult> DeleteCategory([FromRoute] int id)
|
|
{
|
|
await _categoryService.DeleteCategoryAsync(id);
|
|
return Ok();
|
|
}
|
|
}
|
|
}
|