add: funcionalidades REST

This commit is contained in:
2025-08-27 18:15:12 -03:00
parent cf9f2a1179
commit 3c2513a567
7 changed files with 434 additions and 24 deletions

View File

@@ -16,25 +16,198 @@ public class WorkshopsController : ControllerBase
/// <summary>Lista workshops com filtros opcionais.</summary> /// <summary>Lista workshops com filtros opcionais.</summary>
[HttpGet] [HttpGet]
public Task<IActionResult> GetAll([FromQuery] DateTimeOffset? from, [FromQuery] DateTimeOffset? to, [FromQuery] string? q, CancellationToken ct) public async Task<IActionResult> GetAll([FromQuery] DateTimeOffset? from, [FromQuery] DateTimeOffset? to, [FromQuery] string? q, CancellationToken ct)
{ {
// TODO: implementar usando _repo.GetAllAsync e mapear para WorkshopResponse var workshops = await _repo.GetAllAsync(from, to, q, ct);
return Task.FromResult<IActionResult>(Ok(Array.Empty<WorkshopResponse>()));
var response = workshops.Select(w => new WorkshopResponse(
w.Id,
w.Title,
w.Description,
w.StartAt,
w.EndAt,
w.Location,
w.Capacity,
w.IsOnline
)).ToList();
return Ok(response);
} }
/// <summary>Obtém um workshop por Id.</summary> /// <summary>Obtém um workshop por Id.</summary>
[HttpGet("{id:guid}")] [HttpGet("{id:guid}")]
public Task<IActionResult> GetById(Guid id, CancellationToken ct) public async Task<IActionResult> GetById(Guid id, CancellationToken ct)
{ {
// TODO: implementar usando _repo.GetByIdAsync var workshop = await _repo.GetByIdAsync(id, ct);
return Task.FromResult<IActionResult>(NotFound());
if (workshop == null)
{
return NotFound();
}
var response = new WorkshopResponse(
workshop.Id,
workshop.Title,
workshop.Description,
workshop.StartAt,
workshop.EndAt,
workshop.Location,
workshop.Capacity,
workshop.IsOnline
);
return Ok(response);
} }
/// <summary>Cria um novo workshop.</summary> /// <summary>Cria um novo workshop.</summary>
[HttpPost] [HttpPost]
public Task<IActionResult> Create([FromBody] CreateWorkshopRequest body, CancellationToken ct) public async Task<IActionResult> Create([FromBody] CreateWorkshopRequest body, CancellationToken ct)
{ {
// TODO: validar ModelState, regras de negócio e chamar _repo.AddAsync; retornar CreatedAtAction if (!ModelState.IsValid)
return Task.FromResult<IActionResult>(BadRequest()); {
return BadRequest(ModelState);
}
// Validações de negócio
if (body.StartAt >= body.EndAt)
{
return BadRequest("Data de início deve ser anterior à data de fim.");
}
if (body.StartAt < DateTimeOffset.UtcNow)
{
return BadRequest("Data de início deve ser no futuro.");
}
if (!body.IsOnline && string.IsNullOrWhiteSpace(body.Location))
{
return BadRequest("Location é obrigatório para workshops presenciais.");
}
if (body.Capacity < 1)
{
return BadRequest("Capacidade deve ser pelo menos 1.");
}
var workshop = new Workshop
{
Id = Guid.NewGuid(),
Title = body.Title,
Description = body.Description,
StartAt = body.StartAt,
EndAt = body.EndAt,
Location = body.Location,
Capacity = body.Capacity,
IsOnline = body.IsOnline
};
var createdWorkshop = await _repo.AddAsync(workshop, ct);
var response = new WorkshopResponse(
createdWorkshop.Id,
createdWorkshop.Title,
createdWorkshop.Description,
createdWorkshop.StartAt,
createdWorkshop.EndAt,
createdWorkshop.Location,
createdWorkshop.Capacity,
createdWorkshop.IsOnline
);
return CreatedAtAction(nameof(GetById), new { id = createdWorkshop.Id }, response);
}
/// <summary>Atualiza parcialmente um workshop existente.</summary>
[HttpPatch("{id:guid}")]
public async Task<IActionResult> Patch(Guid id, [FromBody] PatchWorkshopRequest body, CancellationToken ct)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var existingWorkshop = await _repo.GetByIdAsync(id, ct);
if (existingWorkshop == null)
{
return NotFound();
}
// Aplicar apenas os campos fornecidos
if (body.Title != null)
existingWorkshop.Title = body.Title;
if (body.Description != null)
existingWorkshop.Description = body.Description;
if (body.StartAt.HasValue)
{
if (body.StartAt.Value < DateTimeOffset.UtcNow)
{
return BadRequest("Data de início deve ser no futuro.");
}
existingWorkshop.StartAt = body.StartAt.Value;
}
if (body.EndAt.HasValue)
existingWorkshop.EndAt = body.EndAt.Value;
if (body.Location != null)
existingWorkshop.Location = body.Location;
if (body.Capacity.HasValue)
{
if (body.Capacity.Value < 1)
{
return BadRequest("Capacidade deve ser pelo menos 1.");
}
existingWorkshop.Capacity = body.Capacity.Value;
}
if (body.IsOnline.HasValue)
existingWorkshop.IsOnline = body.IsOnline.Value;
// Validações de negócio após aplicar mudanças
if (existingWorkshop.StartAt >= existingWorkshop.EndAt)
{
return BadRequest("Data de início deve ser anterior à data de fim.");
}
if (!existingWorkshop.IsOnline && string.IsNullOrWhiteSpace(existingWorkshop.Location))
{
return BadRequest("Location é obrigatório para workshops presenciais.");
}
var updatedWorkshop = await _repo.UpdateAsync(existingWorkshop, ct);
if (updatedWorkshop == null)
{
return NotFound();
}
var response = new WorkshopResponse(
updatedWorkshop.Id,
updatedWorkshop.Title,
updatedWorkshop.Description,
updatedWorkshop.StartAt,
updatedWorkshop.EndAt,
updatedWorkshop.Location,
updatedWorkshop.Capacity,
updatedWorkshop.IsOnline
);
return Ok(response);
}
/// <summary>Remove um workshop.</summary>
[HttpDelete("{id:guid}")]
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
{
var workshop = await _repo.GetByIdAsync(id, ct);
if (workshop == null)
{
return NotFound();
}
await _repo.DeleteAsync(id, ct);
return NoContent();
} }
} }

View File

@@ -1,13 +1,28 @@
using System.ComponentModel.DataAnnotations;
namespace CampusWorkshops.Api.Dtos; namespace CampusWorkshops.Api.Dtos;
// DTO para criação de Workshop. // DTO para criação de Workshop com validações
public record CreateWorkshopRequest( public record CreateWorkshopRequest(
string? Title, [Required(ErrorMessage = "Título é obrigatório")]
[StringLength(120, MinimumLength = 3, ErrorMessage = "Título deve ter entre 3 e 120 caracteres")]
string Title,
[StringLength(2000, ErrorMessage = "Descrição não pode exceder 2000 caracteres")]
string? Description, string? Description,
[Required(ErrorMessage = "Data de início é obrigatória")]
DateTimeOffset StartAt, DateTimeOffset StartAt,
[Required(ErrorMessage = "Data de fim é obrigatória")]
DateTimeOffset EndAt, DateTimeOffset EndAt,
[StringLength(200, ErrorMessage = "Localização não pode exceder 200 caracteres")]
string? Location, string? Location,
[Range(1, 1000, ErrorMessage = "Capacidade deve estar entre 1 e 1000")]
int Capacity, int Capacity,
bool IsOnline bool IsOnline
); );

View File

@@ -0,0 +1,23 @@
using System.ComponentModel.DataAnnotations;
namespace CampusWorkshops.Api.Dtos;
// DTO para atualização parcial de Workshop.
public record PatchWorkshopRequest(
[StringLength(120, MinimumLength = 3, ErrorMessage = "Título deve ter entre 3 e 120 caracteres")]
string? Title,
[StringLength(2000, ErrorMessage = "Descrição não pode exceder 2000 caracteres")]
string? Description,
DateTimeOffset? StartAt,
DateTimeOffset? EndAt,
[StringLength(200, ErrorMessage = "Localização não pode exceder 200 caracteres")]
string? Location,
[Range(1, 1000, ErrorMessage = "Capacidade deve estar entre 1 e 1000")]
int? Capacity,
bool? IsOnline
);

View File

@@ -1,28 +1,32 @@
using System.ComponentModel.DataAnnotations;
namespace CampusWorkshops.Api.Models; namespace CampusWorkshops.Api.Models;
// Modelo mínimo para Workshop.Completar as validações // Modelo mínimo para Workshop com validações
// e ajustar os tipos/atributos durante a atividade.
public class Workshop public class Workshop
{ {
// Identificador único // Identificador único
public Guid Id { get; set; } = Guid.NewGuid(); public Guid Id { get; set; } = Guid.NewGuid();
// TODO: adicionar [Required], [StringLength(120, MinimumLength = 3)] [Required(ErrorMessage = "Título é obrigatório")]
[StringLength(120, MinimumLength = 3, ErrorMessage = "Título deve ter entre 3 e 120 caracteres")]
public string? Title { get; set; } public string? Title { get; set; }
// TODO: limitar tamanho (ex.: 2000) [StringLength(2000, ErrorMessage = "Descrição não pode exceder 2000 caracteres")]
public string? Description { get; set; } public string? Description { get; set; }
// TODO: usar DateTimeOffset com formato ISO 8601 [Required(ErrorMessage = "Data de início é obrigatória")]
public DateTimeOffset StartAt { get; set; } public DateTimeOffset StartAt { get; set; }
[Required(ErrorMessage = "Data de fim é obrigatória")]
public DateTimeOffset EndAt { get; set; } public DateTimeOffset EndAt { get; set; }
// Location deve ser obrigatório se IsOnline == false // Location deve ser obrigatório se IsOnline == false (validado no controller)
[StringLength(200, ErrorMessage = "Localização não pode exceder 200 caracteres")]
public string? Location { get; set; } public string? Location { get; set; }
// TODO: validar Capacity >= 1 [Range(1, 1000, ErrorMessage = "Capacidade deve estar entre 1 e 1000")]
public int Capacity { get; set; } = 1; public int Capacity { get; set; } = 1;
public bool IsOnline { get; set; } public bool IsOnline { get; set; }

View File

@@ -1,10 +1,12 @@
// Note: repository implementation removed for workshop exercise (TODOs in project files) // Note: repository implementation removed for workshop exercise (TODOs in project files)
using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using CampusWorkshops.Api.Repositories;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// Add services // Add services
builder.Services.AddSingleton<IWorkshopRepository, InMemoryWorkshopRepository>();
builder.Services.AddControllers(); builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(o => builder.Services.AddSwaggerGen(o =>

View File

@@ -9,4 +9,8 @@ public interface IWorkshopRepository
Task<IReadOnlyList<Workshop>> GetAllAsync(DateTimeOffset? from, DateTimeOffset? to, string? q, CancellationToken ct); Task<IReadOnlyList<Workshop>> GetAllAsync(DateTimeOffset? from, DateTimeOffset? to, string? q, CancellationToken ct);
Task<Workshop?> GetByIdAsync(Guid id, CancellationToken ct); Task<Workshop?> GetByIdAsync(Guid id, CancellationToken ct);
Task<Workshop> AddAsync(Workshop workshop, CancellationToken ct); Task<Workshop> AddAsync(Workshop workshop, CancellationToken ct);
Task<Workshop?> UpdateAsync(Workshop workshop, CancellationToken ct);
Task<bool> DeleteAsync(Guid id, CancellationToken ct);
Task<bool> ExistsAsync(Guid id, CancellationToken ct);
Task<Workshop?> UpdatePartialAsync(Guid id, Action<Workshop> updateAction, CancellationToken ct);
} }

View File

@@ -6,26 +6,215 @@ namespace CampusWorkshops.Api.Repositories;
// In-memory repository stub // In-memory repository stub
public class InMemoryWorkshopRepository : IWorkshopRepository public class InMemoryWorkshopRepository : IWorkshopRepository
{ {
List<Workshop> _workshops = new();
private void Seed()
{
// Base de datas: sempre a partir da próxima semana, para não “envelhecer” o seed
var baseDate = DateTimeOffset.UtcNow.Date.AddDays(7);
var w1 = new Workshop
{
Id = Guid.Parse("8a0b9f1b-6c9b-4a2a-9d5e-1c2f3a4b5c6d"),
Title = "APIs RESTful — visão geral",
Description = "Modelagem de recursos, status codes e boas práticas.",
StartAt = baseDate.AddHours(12), // daqui a 7 dias, 12:00 UTC
EndAt = baseDate.AddHours(15), // 15:00 UTC
Location = "Lab 101",
Capacity = 25,
IsOnline = false
};
var w2 = new Workshop
{
Id = Guid.Parse("1c3d5e7f-9a2b-4c6d-8e0f-1a2b3c4d5e6f"),
Title = "Entity Framework — mapeamentos e LINQ",
Description = "Relações, consultas eficientes e boas práticas.",
StartAt = baseDate.AddDays(2).AddHours(12), // +2 dias, 12:00 UTC
EndAt = baseDate.AddDays(2).AddHours(14), // 14:00 UTC
Location = null, // online
Capacity = 40,
IsOnline = true
};
var w3 = new Workshop
{
Id = Guid.Parse("f0e1d2c3-b4a5-6789-9a8b-7c6d5e4f3a2b"),
Title = "Construindo contratos com Swagger/OpenAPI",
Description = "Documentação viva, exemplos e testes via Swagger UI.",
StartAt = baseDate.AddDays(5).AddHours(13), // +5 dias, 13:00 UTC
EndAt = baseDate.AddDays(5).AddHours(16), // 16:00 UTC
Location = "Auditório Central",
Capacity = 80,
IsOnline = false
};
_workshops.Add(w1);
_workshops.Add(w2);
_workshops.Add(w3);
}
public InMemoryWorkshopRepository() public InMemoryWorkshopRepository()
{ {
// TODO: Adicionar workshops iniciais Seed();
} }
public Task<IReadOnlyList<Workshop>> GetAllAsync(DateTimeOffset? from, DateTimeOffset? to, string? q, CancellationToken ct) public Task<IReadOnlyList<Workshop>> GetAllAsync(DateTimeOffset? from, DateTimeOffset? to, string? q, CancellationToken ct)
{ {
// TODO: retornar uma lista filtrada e ordenada por StartAt var query = _workshops.AsQueryable();
return Task.FromResult<IReadOnlyList<Workshop>>(Array.Empty<Workshop>());
// Filtro por data início (from)
if (from.HasValue)
{
query = query.Where(w => w.StartAt >= from.Value);
}
// Filtro por data fim (to)
if (to.HasValue)
{
query = query.Where(w => w.StartAt <= to.Value);
}
// Filtro por texto (q) - busca no título e descrição
if (!string.IsNullOrWhiteSpace(q))
{
var searchTerm = q.Trim().ToLowerInvariant();
query = query.Where(w =>
(w.Title != null && w.Title.ToLowerInvariant().Contains(searchTerm)) ||
(w.Description != null && w.Description.ToLowerInvariant().Contains(searchTerm))
);
}
// Ordenar por data de início
var result = query.OrderBy(w => w.StartAt).ToList();
return Task.FromResult<IReadOnlyList<Workshop>>(result);
} }
public Task<Workshop?> GetByIdAsync(Guid id, CancellationToken ct) public Task<Workshop?> GetByIdAsync(Guid id, CancellationToken ct)
{ {
// TODO: buscar por id var workshop = _workshops.FirstOrDefault(w => w.Id == id);
return Task.FromResult<Workshop?>(null); return Task.FromResult(workshop);
} }
public Task<Workshop> AddAsync(Workshop workshop, CancellationToken ct) public Task<Workshop> AddAsync(Workshop workshop, CancellationToken ct)
{ {
// TODO: adicionar à lista em memória e retornar criado // Gerar novo ID se não foi fornecido ou se já existe
if (workshop.Id == Guid.Empty || _workshops.Any(w => w.Id == workshop.Id))
{
workshop.Id = Guid.NewGuid();
}
// Validações de negócio que serão úteis quando migrar para banco
ValidateWorkshop(workshop);
_workshops.Add(workshop);
return Task.FromResult(workshop); return Task.FromResult(workshop);
} }
public Task<Workshop?> UpdateAsync(Workshop workshop, CancellationToken ct)
{
var existingIndex = _workshops.FindIndex(w => w.Id == workshop.Id);
if (existingIndex < 0)
{
return Task.FromResult<Workshop?>(null);
}
// Validações de negócio
ValidateWorkshop(workshop);
_workshops[existingIndex] = workshop;
return Task.FromResult<Workshop?>(workshop);
}
public Task<bool> DeleteAsync(Guid id, CancellationToken ct)
{
var workshop = _workshops.FirstOrDefault(w => w.Id == id);
if (workshop == null)
{
return Task.FromResult(false);
}
_workshops.Remove(workshop);
return Task.FromResult(true);
}
public Task<bool> ExistsAsync(Guid id, CancellationToken ct)
{
var exists = _workshops.Any(w => w.Id == id);
return Task.FromResult(exists);
}
public Task<Workshop?> UpdatePartialAsync(Guid id, Action<Workshop> updateAction, CancellationToken ct)
{
var workshop = _workshops.FirstOrDefault(w => w.Id == id);
if (workshop == null)
{
return Task.FromResult<Workshop?>(null);
}
// Aplicar as modificações
updateAction(workshop);
// Validar após modificações
ValidateWorkshop(workshop);
return Task.FromResult<Workshop?>(workshop);
}
private void ValidateWorkshop(Workshop workshop)
{
var errors = new List<string>();
// Validação de título
if (string.IsNullOrWhiteSpace(workshop.Title))
{
errors.Add("Title is required");
}
else if (workshop.Title.Length < 3 || workshop.Title.Length > 120)
{
errors.Add("Title must be between 3 and 120 characters");
}
// Validação de descrição
if (!string.IsNullOrWhiteSpace(workshop.Description) && workshop.Description.Length > 2000)
{
errors.Add("Description cannot exceed 2000 characters");
}
// Validação de datas
if (workshop.EndAt <= workshop.StartAt)
{
errors.Add("End date must be after start date");
}
if (workshop.StartAt <= DateTimeOffset.UtcNow)
{
errors.Add("Start date must be in the future");
}
// Validação de capacidade
if (workshop.Capacity < 1)
{
errors.Add("Capacity must be at least 1");
}
// Validação de localização para workshops presenciais
if (!workshop.IsOnline && string.IsNullOrWhiteSpace(workshop.Location))
{
errors.Add("Location is required for in-person workshops");
}
// Validação de localização para workshops online
if (workshop.IsOnline && !string.IsNullOrWhiteSpace(workshop.Location))
{
errors.Add("Location should not be specified for online workshops");
}
if (errors.Any())
{
throw new ArgumentException($"Workshop validation failed: {string.Join(", ", errors)}");
}
}
} }