From 106e7492d8e8d07ccf50f2d13b5685f5838a52d8 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 14 Dec 2022 16:23:31 +0800 Subject: Develop secret api. v40 --- .../Crud/CrudWebApplicationExtensions.cs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs') 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>(); - var entityJsonHelper = context.RequestServices.GetRequiredService>(); 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>(); + 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>(); - var entityJsonHelper = context.RequestServices.GetRequiredService>(); - // 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; -- cgit v1.2.3