blob: 575dc4fac2851b2cb92802866abf0468ad883f58 (
plain)
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
|
using System;
using System.Text.Json;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace CrupestApi.Todos;
public static class TodosWebApplicationExtensions
{
public static WebApplication MapTodos(this WebApplication app, string path)
{
if (app is null)
{
throw new ArgumentNullException(nameof(app));
}
app.MapGet(path, async ([FromServices] TodosService todosService) =>
{
var jsonOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
try
{
var todos = await todosService.GetTodosAsync();
return Results.Json(todos, jsonOptions, statusCode: 200);
}
catch (Exception e)
{
return Results.Json(new
{
e.Message
}, jsonOptions, statusCode: StatusCodes.Status503ServiceUnavailable);
}
});
return app;
}
}
|