-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiEndpoints.cs
161 lines (128 loc) · 5.1 KB
/
ApiEndpoints.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using ContactApp.Contacts;
using ContactApp.Pages.Contacts;
using ContactApp.Utils;
using Microsoft.AspNetCore.Components.Endpoints;
using Microsoft.AspNetCore.Mvc;
using MiniValidation;
namespace ContactApp;
public static class ApiEndpoints
{
public static WebApplication MapContactsApiEndpoints(this WebApplication app)
{
app.MapDelete("/Contacts/{id:int}", (HttpContext httpContext,
ContactRepository contactRepository, int id) =>
{
contactRepository.Delete(id);
if (httpContext.Request.Headers.TryGetValue("hx-trigger", out var trigger) && trigger == "delete-btn")
{
httpContext.Response.Headers.Append("Location", "/Contacts");
return Results.StatusCode(303);
}
return Results.Empty;
});
app.MapDelete("/Contacts", (HttpContext httpContext,
ContactRepository contactRepository, [FromForm] int[] selectedContactIds) =>
{
contactRepository.BulkDelete(selectedContactIds);
var contacts = contactRepository.All();
httpContext.Response.Headers.Append("Location", "/Contacts");
return Results.StatusCode(303);
});
app.MapGet("/Contacts/CheckEmail", (ContactRepository contactRepository, string email) =>
{
return contactRepository.EmailIsUnique(email)
? string.Empty
: "Email Must Be Unique";
});
app.MapGet("/Contacts/Count", (ContactRepository contactRepository) =>
{
var count = contactRepository.Count();
return $"({count} total Contacts)";
});
return app;
}
public static WebApplication MapArchiveApiEndpoints(this WebApplication app)
{
app.MapGet("/Contacts/Archive", (Archiver archiver) =>
{
return new RazorComponentResult<ArchiveUI>(new { Archiver = archiver });
});
app.MapPost("/Contacts/Archive", (Archiver archiver) =>
{
archiver.Run();
return new RazorComponentResult<ArchiveUI>(new { Archiver = archiver });
});
app.MapDelete("/Contacts/Archive", (Archiver archiver) =>
{
archiver.Reset();
return new RazorComponentResult<ArchiveUI>(new { Archiver = archiver });
});
app.MapGet("Contacts/Archive/File", (IWebHostEnvironment env, Archiver archiver) =>
{
var filePath = Path.Combine(env.WebRootPath, archiver.ArchiveFile());
return Results.File(filePath, "application/json", "archive.json");
});
return app;
}
public static WebApplication MapContactsDataApiEndpoints(this WebApplication app)
{
app.MapGet("/api/v1/contacts", (ContactRepository contactRepository) =>
{
var contactsSet = contactRepository.All();
return Results.Ok(new { Contacts = contactsSet });
});
app.MapPost("api/v1/contacts", (ContactRepository contactRepository, Contact contact) =>
{
var isValid = MiniValidator.TryValidate(contact, out var errors);
if (contact.Email != null && !contactRepository.EmailIsUnique(contact.Email))
{
errors["Email"] = new string[] { "Email Must Be Unique" };
isValid = false;
}
if (!isValid)
return Results.ValidationProblem(errors);
var c = new Contact
{
First = contact.First,
Last = contact.Last,
Phone = contact.Phone,
Email = contact.Email!
};
contactRepository.Add(c);
return Results.Ok(c);
});
app.MapGet("api/v1/contacts/{id}", (ContactRepository contactRepository, int id) =>
{
var contact = contactRepository.GetById(id);
return contact is null
? Results.NotFound()
: Results.Ok(contact);
});
app.MapPut("api/v1/contacts/{id}", (ContactRepository contactRepository, int id, Contact contact) =>
{
var isValid = MiniValidator.TryValidate(contact, out var errors);
if (contact.Email != null && !contactRepository.EmailIsUnique(contact.Email))
{
errors["Email"] = new string[] { "Email Must Be Unique" };
isValid = false;
}
if (!isValid)
return Results.ValidationProblem(errors);
var c = contactRepository.GetById(id);
if (c is null)
return Results.NotFound();
c.First = contact.First;
c.Last = contact.Last;
c.Phone = contact.Phone;
c.Email = contact.Email!;
contactRepository.Update(c);
return Results.Ok(c);
});
app.MapDelete("api/v1/contacts/{id}", (ContactRepository contactRepository, int id) =>
{
contactRepository.Delete(id);
return Results.Ok(new { Success = true });
});
return app;
}
}