diff options
author | crupest <crupest@outlook.com> | 2022-12-14 16:23:31 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-12-20 20:32:53 +0800 |
commit | 106e7492d8e8d07ccf50f2d13b5685f5838a52d8 (patch) | |
tree | 65639071646120768668149d40cff208bfee39b2 /docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs | |
parent | 3af36448bed4f825331dc359615f8c4acf825f58 (diff) | |
download | crupest-106e7492d8e8d07ccf50f2d13b5685f5838a52d8.tar.gz crupest-106e7492d8e8d07ccf50f2d13b5685f5838a52d8.tar.bz2 crupest-106e7492d8e8d07ccf50f2d13b5685f5838a52d8.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.cs | 22 |
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; |