naming
This commit is contained in:
77
Webshop.Api/Controllers/Admin/AdminCategoryController.cs
Normal file
77
Webshop.Api/Controllers/Admin/AdminCategoryController.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
// src/Webshop.Api/Controllers/Admin/AdmincategorysController.cs
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.DTOs.Categorys;
|
||||
using Webshop.Application.Services.Admin;
|
||||
|
||||
namespace Webshop.Api.Controllers.Admin
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/v1/admin/categorys")]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public class AdminCategorysController : ControllerBase
|
||||
{
|
||||
private readonly IAdminCategoryService _adminCategoryService;
|
||||
|
||||
public AdminCategorysController(IAdminCategoryService adminCategoryService)
|
||||
{
|
||||
_adminCategoryService = adminCategoryService;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<IEnumerable<CategoryDto>>> GetAllcategorys()
|
||||
{
|
||||
var categorys = await _adminCategoryService.GetAllAsync();
|
||||
return Ok(categorys);
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult<CategoryDto>> GetCategoryById(Guid id)
|
||||
{
|
||||
var category = await _adminCategoryService.GetByIdAsync(id);
|
||||
if (category == null) return NotFound();
|
||||
return Ok(category);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<CategoryDto>> CreateCategory([FromBody] CreateCategoryDto categoryDto)
|
||||
{
|
||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||
|
||||
var (createdCategory, errorMessage) = await _adminCategoryService.CreateAsync(categoryDto);
|
||||
|
||||
if (createdCategory == null)
|
||||
{
|
||||
return BadRequest(new { Message = errorMessage });
|
||||
}
|
||||
|
||||
return CreatedAtAction(nameof(GetCategoryById), new { id = createdCategory.Id }, createdCategory);
|
||||
}
|
||||
|
||||
[HttpPut("{id}")]
|
||||
public async Task<IActionResult> UpdateCategory(Guid id, [FromBody] CreateCategoryDto categoryDto)
|
||||
{
|
||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||
|
||||
var (success, errorMessage) = await _adminCategoryService.UpdateAsync(id, categoryDto);
|
||||
|
||||
if (!success)
|
||||
{
|
||||
return BadRequest(new { Message = errorMessage });
|
||||
}
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<IActionResult> DeleteCategory(Guid id)
|
||||
{
|
||||
var success = await _adminCategoryService.DeleteAsync(id);
|
||||
if (!success) return NotFound();
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user