-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
62 lines (52 loc) · 1.76 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using ActivityStudents.Repositories;
using ActivityStudents.Entities;
using ActivityStudents.Models;
var builder = WebApplication.CreateBuilder(args);
var connection = builder.Configuration.GetConnectionString("db");
builder.Services.AddSingleton(new Database(connection!));
builder.Services.AddScoped<ActivityRepository>();
builder.Services.AddSwaggerGen();
// Add Swagger services
builder.Services.AddEndpointsApiExplorer();
var app = builder.Build();
//?classId=A&name=Arthur%20Teodoro
//?name=Arthur%20Teodoro
//?classId=A
app.MapGet("/", (ActivityRepository repo, string? name, string? classId) =>
{
try
{
IEnumerable<ActivityEntity> activities;
// Se ambos os parâmetros forem fornecidos, filtre por ambos
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(classId))
{
activities = repo.GetByNameAndClass(name, classId);
}
// Se apenas o nome for fornecido, filtre por nome
else if (!string.IsNullOrEmpty(name))
{
activities = repo.GetByName(name);
}
// Se apenas o classId for fornecido, filtre por classId
else if (!string.IsNullOrEmpty(classId))
{
activities = repo.GetByClass(classId);
}
// Se nenhum filtro for fornecido, retorne todas as atividades
else
{
activities = repo.GetAll();
}
var grouped = activities.GroupBy(g => g.ClassIdentify, (key, g) => new
{
@class = key,
students = g.Select(s => new Student(s)).ToList()
});
return grouped.Any() ? Results.Ok(grouped) : Results.NotFound("Nenhuma atividade encontrada.");
}
catch (Exception e)
{
return Results.Problem(e.Message);
}
});
app.Run();