Initial commit
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
using ExpenseTracker.Api.Data;
|
||||
using ExpenseTracker.Api.DTOs;
|
||||
using ExpenseTracker.Api.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ExpenseTracker.Api.Controllers;
|
||||
|
||||
/// <summary>Endpoints for managing expense categories.</summary>
|
||||
[ApiController]
|
||||
[Route("api/categories")]
|
||||
[Produces("application/json")]
|
||||
public class CategoriesController : ControllerBase
|
||||
{
|
||||
private readonly AppDbContext _db;
|
||||
private readonly ILogger<CategoriesController> _logger;
|
||||
|
||||
public CategoriesController(AppDbContext db, ILogger<CategoriesController> logger)
|
||||
{
|
||||
_db = db;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
// ── GET /api/categories ───────────────────────────────────────────────────
|
||||
|
||||
/// <summary>Returns all categories sorted by name ascending.</summary>
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(List<CategoryDto>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetAll()
|
||||
{
|
||||
var categories = await _db.Categories
|
||||
.OrderBy(c => c.Name)
|
||||
.Select(c => new CategoryDto(c.Id, c.Name, c.Color))
|
||||
.ToListAsync();
|
||||
|
||||
return Ok(categories);
|
||||
}
|
||||
|
||||
// ── POST /api/categories ──────────────────────────────────────────────────
|
||||
|
||||
/// <summary>Creates a new category.</summary>
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(CategoryDto), StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> Create([FromBody] CreateCategoryDto dto)
|
||||
{
|
||||
// FluentValidation auto-validation handles 400 before we get here.
|
||||
|
||||
var category = new Category
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = dto.Name,
|
||||
Color = dto.Color
|
||||
};
|
||||
|
||||
_db.Categories.Add(category);
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
_logger.LogInformation("Category '{Name}' created with id {Id}.", category.Name, category.Id);
|
||||
|
||||
var result = new CategoryDto(category.Id, category.Name, category.Color);
|
||||
return CreatedAtAction(nameof(GetAll), null, result);
|
||||
}
|
||||
|
||||
// ── DELETE /api/categories/{id} ───────────────────────────────────────────
|
||||
|
||||
/// <summary>Deletes a category. Returns 409 Conflict if entries reference it.</summary>
|
||||
[HttpDelete("{id:guid}")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
[ProducesResponseType(StatusCodes.Status409Conflict)]
|
||||
public async Task<IActionResult> Delete(Guid id)
|
||||
{
|
||||
var category = await _db.Categories.FindAsync(id);
|
||||
|
||||
if (category is null)
|
||||
{
|
||||
_logger.LogWarning("Delete category: id {Id} not found.", id);
|
||||
return NotFound(new { error = "Category not found." });
|
||||
}
|
||||
|
||||
bool hasEntries = await _db.Entries.AnyAsync(e => e.CategoryId == id);
|
||||
|
||||
if (hasEntries)
|
||||
{
|
||||
_logger.LogWarning("Delete category {Id} refused: entries still reference it.", id);
|
||||
return Conflict(new { error = "This category is referenced by one or more entries and cannot be deleted." });
|
||||
}
|
||||
|
||||
_db.Categories.Remove(category);
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
_logger.LogInformation("Category {Id} deleted.", id);
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user