77 lines
2.5 KiB
C#
77 lines
2.5 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.DTOs.categories;
|
|
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]
|
|
public async Task<ActionResult<categorieDto>> 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<IActionResult> 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<IActionResult> Deletecategorie(Guid id)
|
|
{
|
|
var success = await _admincategorieservice.DeleteAsync(id);
|
|
if (!success) return NotFound();
|
|
return NoContent();
|
|
}
|
|
}
|
|
} |