add: implementacao inicial

This commit is contained in:
2025-08-25 00:06:43 -03:00
parent 2378741455
commit cf9f2a1179
9 changed files with 203 additions and 28 deletions

View File

@@ -0,0 +1,40 @@
using CampusWorkshops.Api.Dtos;
using CampusWorkshops.Api.Models;
using CampusWorkshops.Api.Repositories;
using Microsoft.AspNetCore.Mvc;
namespace CampusWorkshops.Api.Controllers;
[ApiController]
[Route("api/[controller]")]
public class WorkshopsController : ControllerBase
{
private readonly IWorkshopRepository _repo;
public WorkshopsController(IWorkshopRepository repo) => _repo = repo;
/// <summary>Lista workshops com filtros opcionais.</summary>
[HttpGet]
public Task<IActionResult> GetAll([FromQuery] DateTimeOffset? from, [FromQuery] DateTimeOffset? to, [FromQuery] string? q, CancellationToken ct)
{
// TODO: implementar usando _repo.GetAllAsync e mapear para WorkshopResponse
return Task.FromResult<IActionResult>(Ok(Array.Empty<WorkshopResponse>()));
}
/// <summary>Obtém um workshop por Id.</summary>
[HttpGet("{id:guid}")]
public Task<IActionResult> GetById(Guid id, CancellationToken ct)
{
// TODO: implementar usando _repo.GetByIdAsync
return Task.FromResult<IActionResult>(NotFound());
}
/// <summary>Cria um novo workshop.</summary>
[HttpPost]
public Task<IActionResult> Create([FromBody] CreateWorkshopRequest body, CancellationToken ct)
{
// TODO: validar ModelState, regras de negócio e chamar _repo.AddAsync; retornar CreatedAtAction
return Task.FromResult<IActionResult>(BadRequest());
}
}

View File

@@ -0,0 +1,13 @@
namespace CampusWorkshops.Api.Dtos;
// DTO para criação de Workshop.
public record CreateWorkshopRequest(
string? Title,
string? Description,
DateTimeOffset StartAt,
DateTimeOffset EndAt,
string? Location,
int Capacity,
bool IsOnline
);

14
Dtos/WorkshopResponse.cs Normal file
View File

@@ -0,0 +1,14 @@
namespace CampusWorkshops.Api.Dtos;
// DTO de resposta para Workshop
public record WorkshopResponse(
Guid Id,
string? Title,
string? Description,
DateTimeOffset StartAt,
DateTimeOffset EndAt,
string? Location,
int Capacity,
bool IsOnline
);

29
Models/Workshop.cs Normal file
View File

@@ -0,0 +1,29 @@
namespace CampusWorkshops.Api.Models;
// Modelo mínimo para Workshop.Completar as validações
// e ajustar os tipos/atributos durante a atividade.
public class Workshop
{
// Identificador único
public Guid Id { get; set; } = Guid.NewGuid();
// TODO: adicionar [Required], [StringLength(120, MinimumLength = 3)]
public string? Title { get; set; }
// TODO: limitar tamanho (ex.: 2000)
public string? Description { get; set; }
// TODO: usar DateTimeOffset com formato ISO 8601
public DateTimeOffset StartAt { get; set; }
public DateTimeOffset EndAt { get; set; }
// Location deve ser obrigatório se IsOnline == false
public string? Location { get; set; }
// TODO: validar Capacity >= 1
public int Capacity { get; set; } = 1;
public bool IsOnline { get; set; }
}

View File

@@ -1,41 +1,56 @@
// Note: repository implementation removed for workshop exercise (TODOs in project files)
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
// Add services
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(o =>
{
o.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
{
Title = "CampusWorkshops API",
Version = "v1",
Description = "API para gestão de workshops do campus (MVP in-memory)."
});
});
// DI
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
// Exception handler that returns RFC7807 ProblemDetails for unhandled errors
app.UseExceptionHandler(errApp =>
{
app.MapOpenApi();
}
errApp.Run(async context =>
{
var feature = context.Features.Get<IExceptionHandlerFeature>();
var ex = feature?.Error;
var pd = new ProblemDetails
{
Title = "An unexpected error occurred.",
Status = StatusCodes.Status500InternalServerError,
Detail = app.Environment.IsDevelopment() ? ex?.Message : null
};
context.Response.StatusCode = pd.Status.Value;
context.Response.ContentType = "application/problem+json";
await context.Response.WriteAsJsonAsync(pd);
});
});
app.UseHttpsRedirection();
var summaries = new[]
app.UseSwagger();
app.UseSwaggerUI(c =>
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
c.SwaggerEndpoint("/swagger/v1/swagger.json", "CampusWorkshops API v1");
c.RoutePrefix = "swagger"; // serve at /swagger
});
app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast");
app.MapControllers();
app.Run();
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}

20
README.md Normal file
View File

@@ -0,0 +1,20 @@
# CampusWorkshops API — Starter for Workshop
Pré-requisitos:
* .NET 8 SDK instalado
Comandos básicos:
```bash
dotnet restore
dotnet run
```
Depois de rodar, abra: https://localhost:5001/swagger
Objetivos do encontro 1
* Entender o contrato REST (recursos, rotas, status codes).
* Implementar/ajustar `GET /api/workshops`, `GET /api/workshops/{id}` e codar o `POST /api/workshops` (com validação e `201 Created + Location`).
* Ver erros formatados como `application/problem+json`.

View File

@@ -0,0 +1,12 @@
using CampusWorkshops.Api.Models;
namespace CampusWorkshops.Api.Repositories;
// Define repository contract; implementations should be provided by students during the workshop.
public interface IWorkshopRepository
{
Task<IReadOnlyList<Workshop>> GetAllAsync(DateTimeOffset? from, DateTimeOffset? to, string? q, CancellationToken ct);
Task<Workshop?> GetByIdAsync(Guid id, CancellationToken ct);
Task<Workshop> AddAsync(Workshop workshop, CancellationToken ct);
}

View File

@@ -0,0 +1,31 @@
using CampusWorkshops.Api.Models;
namespace CampusWorkshops.Api.Repositories;
// In-memory repository stub
public class InMemoryWorkshopRepository : IWorkshopRepository
{
public InMemoryWorkshopRepository()
{
// TODO: Adicionar workshops iniciais
}
public Task<IReadOnlyList<Workshop>> GetAllAsync(DateTimeOffset? from, DateTimeOffset? to, string? q, CancellationToken ct)
{
// TODO: retornar uma lista filtrada e ordenada por StartAt
return Task.FromResult<IReadOnlyList<Workshop>>(Array.Empty<Workshop>());
}
public Task<Workshop?> GetByIdAsync(Guid id, CancellationToken ct)
{
// TODO: buscar por id
return Task.FromResult<Workshop?>(null);
}
public Task<Workshop> AddAsync(Workshop workshop, CancellationToken ct)
{
// TODO: adicionar à lista em memória e retornar criado
return Task.FromResult(workshop);
}
}

View File

@@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.3" />
</ItemGroup>
</Project>