-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
66 lines (54 loc) · 1.82 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
using EvilInc.Models;
using Microsoft.EntityFrameworkCore;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Http.Json;
using EvilInc.Models.DTOs;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddNpgsql<EvilIncDbContext>(builder.Configuration["EvilIncDbConnectionString"]);
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.MapGet("/minions", (EvilIncDbContext db) => {
return db.Minions.Select( m =>
new MinionDTO{
Name = m.Name,
PowerLevel = m.PowerLevel
}
).ToList();
});
app.MapGet("/minions/{id}", (EvilIncDbContext db, int id) =>
{
var minion = db.Minions.Include(m => m.Plots).FirstOrDefault(m => m.Id == id);
if (minion == null) return Results.NotFound();
return Results.Ok(new MinionDetailsDTO
{
Name = minion.Name,
Description = minion.Description,
PowerLevel = minion.PowerLevel,
Plots = minion.Plots.Select(p => p.Name).ToList()
});
});
app.MapDelete("/plots/{id}", async (EvilIncDbContext db, int id) =>
{
var plot = await db.Plots.FindAsync(id);
if (plot == null) return Results.NotFound();
db.Plots.Remove(plot);
await db.SaveChangesAsync();
return Results.NoContent();
});
app.MapPost("/plots/{plotId}/assign/{minionId}", async (EvilIncDbContext db, int plotId, int minionId) =>
{
var plot = await db.Plots.Include(p => p.Minions).FirstOrDefaultAsync(p => p.Id == plotId);
var minion = await db.Minions.FindAsync(minionId);
if (plot == null || minion == null) return Results.NotFound();
plot.Minions.Add(minion);
await db.SaveChangesAsync();
return Results.NoContent();
});
app.Run();