using Microsoft.AspNetCore.Mvc;
using TaskLedger.Api.Domain.Entities;
using TaskLedger.Api.Services;
namespace TaskLedger.Api.Controllers;
///
/// Controller for task operations.
///
[ApiController]
[Route("api/[controller]")]
public class TasksController : ControllerBase
{
private readonly ITaskService _taskService;
///
/// Initializes a new instance of the class.
///
/// The task service.
public TasksController(ITaskService taskService)
{
_taskService = taskService;
}
///
/// Creates a new task.
///
/// The task creation request.
/// The created task.
[HttpPost]
[ProducesResponseType(typeof(Domain.Entities.Task), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task> CreateTask([FromBody] CreateTaskRequest request)
{
try
{
var task = await _taskService.CreateTaskAsync(request.Title, request.Description);
return CreatedAtAction(nameof(GetAllTasks), new { id = task.Id }, task);
}
catch (ArgumentException ex)
{
return BadRequest(new { error = ex.Message });
}
}
///
/// Gets all tasks.
///
/// A list of all tasks.
[HttpGet]
[ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)]
public async Task>> GetAllTasks()
{
var tasks = await _taskService.GetAllTasksAsync();
return Ok(tasks);
}
}
///
/// Request model for creating a task.
///
public record CreateTaskRequest
{
///
/// Gets or sets the title of the task.
///
public string Title { get; init; } = string.Empty;
///
/// Gets or sets the description of the task.
///
public string? Description { get; init; }
}