aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs
diff options
context:
space:
mode:
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;