aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-12-14 16:23:31 +0800
committercrupest <crupest@outlook.com>2022-12-20 20:32:53 +0800
commit4595650f6e36ff413bcb65f5419daf8f9bfee9de (patch)
tree65639071646120768668149d40cff208bfee39b2 /docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs
parent70662b4da01a2daa2ece586e5ba6e78bb7a7eb4d (diff)
downloadcrupest-4595650f6e36ff413bcb65f5419daf8f9bfee9de.tar.gz
crupest-4595650f6e36ff413bcb65f5419daf8f9bfee9de.tar.bz2
crupest-4595650f6e36ff413bcb65f5419daf8f9bfee9de.zip
Develop secret api. v40
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs')
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs22
1 files changed, 18 insertions, 4 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs
index 9f70f35..8df444c 100644
--- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs
@@ -7,16 +7,30 @@ public static class CrudWebApplicationExtensions
app.MapGet(path, async (context) =>
{
var crudService = context.RequestServices.GetRequiredService<CrudService<TEntity>>();
- var entityJsonHelper = context.RequestServices.GetRequiredService<EntityJsonHelper<TEntity>>();
var allEntities = crudService.GetAll();
- await context.ResponseJsonAsync(allEntities.Select(e => entityJsonHelper.ConvertEntityToDictionary(e)));
+ await context.ResponseJsonAsync(allEntities.Select(e => crudService.JsonHelper.ConvertEntityToDictionary(e)));
+ });
+
+ app.MapGet(path + "/{key}", async (context) =>
+ {
+ var crudService = context.RequestServices.GetRequiredService<CrudService<TEntity>>();
+ var key = context.Request.RouteValues["key"]?.ToString();
+ if (key == null)
+ {
+ await context.ResponseMessageAsync("Please specify a key.");
+ return;
+ }
+
+ var entity = crudService.GetByKey(key);
+ await context.ResponseJsonAsync(crudService.JsonHelper.ConvertEntityToDictionary(entity));
});
app.MapPost(path, async (context) =>
{
var crudService = context.RequestServices.GetRequiredService<CrudService<TEntity>>();
- var entityJsonHelper = context.RequestServices.GetRequiredService<EntityJsonHelper<TEntity>>();
- // TODO: Continue here.
+ var jsonDocument = await context.Request.ReadJsonAsync();
+ var key = crudService.Create(jsonDocument.RootElement);
+ await context.ResponseJsonAsync(crudService.JsonHelper.ConvertEntityToDictionary(crudService.GetByKey(key)));
});
return app;