-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
54 lines (37 loc) · 1011 Bytes
/
Program.cs
File metadata and controls
54 lines (37 loc) · 1011 Bytes
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 FriendlyUrlHelper;
var builder = WebApplication.CreateBuilder(args);
var host = builder.Configuration.GetValue<string>("Host");
var app = builder.Build();
List<Post> posts = new();
app.MapGet("v1/posts", () => Results.Ok(posts));
app.MapGet("", () => "Nova api");
app.MapGet("v1/posts/{slug}", (string slug) => Results.Ok(posts.FirstOrDefault(x => x.Slug == slug)));
app.MapPost("v1/posts", (CreatePostModel model) =>
{
var slug = model.Title.ToUrl();
var post = new Post(
Id: posts.Count + 1,
model.Title,
slug,
model.Body,
Url: $"{host}/v1/posts/{slug}",
CreatedAt: DateTime.UtcNow,
UpdatedAt: DateTime.UtcNow
);
posts.Add(post);
return Results.Created(uri: "v1/posts", post);
});
app.Run();
public record CreatePostModel(
string Title,
string Body
);
public record Post(
int Id,
string Title,
string Slug,
string Body,
string Url,
DateTime CreatedAt,
DateTime UpdatedAt
);