-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
101 lines (70 loc) · 2.39 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using Microsoft.EntityFrameworkCore;
using World.Cup.Simulator;
using World.Cup.Simulator.Models;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("ServerConnection");
string? connectionStringUrl = Environment.GetEnvironmentVariable("DATABASE_URL");
connectionString = string.IsNullOrEmpty(connectionStringUrl) ? connectionString : connectionStringUrl;
// Add services to the container.
builder.Services.AddDbContext<WorldCupContext>(options =>
{
options.UseMySql(connectionString,ServerVersion.AutoDetect(connectionString));
});
//Enable CORS
builder.Services.AddCors(c =>
{
c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
//Enable CORS
app.UseCors(options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
app.UseSwagger();
app.UseSwaggerUI();
app.UseHttpsRedirection();
app.MapGet("/api/teams/groups", async (WorldCupContext context) =>
{
var teams = await context.teams.ToListAsync();
var groups = teams.GroupBy(p => p.Group)
.OrderBy(p => p.Key)
.Select(p => p.Select(p => p));
return Results.Ok(groups);
});
app.MapGet("/teams/{id}",async (WorldCupContext context, int id) =>
{
var team = await context.teams.FindAsync(id);
return Results.Ok(team);
});
app.MapGet("/teams", async (WorldCupContext context) =>
{
var teams = await context.teams.ToListAsync();
return Results.Ok(teams);
});
app.MapPost("/teams", async(WorldCupContext context, team team) =>
{
await context.teams.AddAsync(team);
await context.SaveChangesAsync();
return Results.Ok(team);
});
app.MapPut("/teams/{id}", async (WorldCupContext context, team team) =>
{
var dbTeam = await context.teams.FindAsync(team.Id);
if (dbTeam == null)
Results.NotFound();
dbTeam.Name = team.Name;
dbTeam.Img = team.Img;
context.teams.Update(dbTeam);
await context.SaveChangesAsync();
return Results.Ok(dbTeam);
});
app.MapDelete("/teams/{id}", async (WorldCupContext context, int id) =>
{
team dbTeam = await context.teams.FindAsync(id);
if (dbTeam == null)
return Results.NotFound();
context.teams.Remove(dbTeam);
await context.SaveChangesAsync();
return Results.NoContent();
});
app.Run();