-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathProgram.cs
More file actions
54 lines (48 loc) · 1.48 KB
/
Copy pathProgram.cs
File metadata and controls
54 lines (48 loc) · 1.48 KB
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
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Linq;
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder
.ConfigureServices(services => services.AddControllers())
.Configure(app =>
{
app.UseRouting();
app.UseEndpoints(endpoints => endpoints.MapControllers());
});
})
.Build()
.Run();
[Route("api/[controller]")]
[ApiController]
public class HeroesController : ControllerBase
{
public record HeroDto(int ID, string Name, string Universe, bool CanFly);
private static List<HeroDto> Heroes { get; } = new()
{
new(1, "Homelander", "DC", true),
new(2, "Groot", "Marvel", false),
};
[HttpGet]
[Route("")]
public IActionResult GetAllHeroes() => Ok(Heroes);
[HttpGet("{id}", Name = nameof(GetHeroById))]
public IActionResult GetHeroById(int id) =>
Heroes.FirstOrDefault(h => h.ID == id) switch
{
null => NotFound(),
var h => Ok(h)
};
[HttpPost]
public IActionResult AddHero([FromBody] HeroDto hero)
{
Heroes.Add(hero);
return CreatedAtRoute(nameof(GetHeroById), new { id = hero.ID }, hero);
}
}