-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
63 lines (48 loc) · 1.93 KB
/
Program.cs
File metadata and controls
63 lines (48 loc) · 1.93 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
55
56
57
58
59
60
61
62
63
using Microsoft.AspNetCore.Mvc;
using MinimalApis;
var builder = WebApplication.CreateBuilder();
builder.Services.AddSingleton<ArticleServices>();
var app = builder.Build();
app.MapGet("/get", () => "Hello GET !");
app.MapDelete("/delete", () => "Hello DELETE !");
app.MapPost("/post", () => "Hello POST !");
app.MapPut("/put", () => "Hello PUT !");
app.MapPatch("/patch", () => "Hello PATCH !");
// app.MapMethods("/methods", new[] {"GET", "POST"}, ()=> "Hello vous !");
app.MapGet("/articles", ([FromServices] ArticleServices service) => service.GetAll());
app.MapGet("/article/{id:int}", (int id, [FromServices] ArticleServices service) =>
{
var article = service.GetAll().Find(a => a.Id == id);
if (article is not null){
return Results.Ok(article);
}
return Results.NotFound("Not found");
});
app.MapPost("/addArticle/", (Article article, [FromServices] ArticleServices service)=>
{
Article result = service.Add(article.Title);
return Results.Ok(result);
});
app.MapGet("/personne/{nom}", (
[FromRoute(Name = "nom")] string nomPersonne,
[FromQuery] string? prenom) => Results.Ok($"{nomPersonne} {prenom}"));
// app.MapGet("/personne/identite", (Personne p)=> Results.Ok(p));
app.Use(async (context, next) =>
{
using var reader = new StreamReader(context.Request.Body);
var body = await reader.ReadToEndAsync();
Console.WriteLine($"Corps brut reçu : {body}");
context.Request.Body = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(body));
await next();
});
app.MapPost("/personne/identite", (Personne? p) =>
{
if (p == null)
{
Console.WriteLine("Erreur : Désérialisation échouée. Le corps JSON est peut-être mal formé.");
return Results.BadRequest("Données non valides ou mal formées.");
}
Console.WriteLine($"Nom : {p.Nom}, Prénom : {p.Prenom}");
return Results.Ok(p);
});
app.Run();