using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Purrse.Core.DTOs; using Purrse.Core.Interfaces.Services; using System.Security.Claims; namespace Purrse.Api.Controllers; [Authorize] [ApiController] [Route("api/[controller]")] public class CategoriesController : ControllerBase { private readonly ICategoryService _categoryService; private Guid UserId => Guid.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!); public CategoriesController(ICategoryService categoryService) => _categoryService = categoryService; [HttpGet] public async Task>> GetAll() => Ok(await _categoryService.GetAllAsync(UserId)); [HttpGet("tree")] public async Task>> GetTree() => Ok(await _categoryService.GetTreeAsync(UserId)); [HttpGet("{id}")] public async Task> GetById(Guid id) { var cat = await _categoryService.GetByIdAsync(UserId, id); return cat == null ? NotFound() : Ok(cat); } [HttpPost] public async Task> Create(CreateCategoryRequest request) { var cat = await _categoryService.CreateAsync(UserId, request); return CreatedAtAction(nameof(GetById), new { id = cat.Id }, cat); } [HttpPut("{id}")] public async Task> Update(Guid id, UpdateCategoryRequest request) => Ok(await _categoryService.UpdateAsync(UserId, id, request)); [HttpDelete("{id}")] public async Task Delete(Guid id) { await _categoryService.DeleteAsync(UserId, id); return NoContent(); } }