diff options
author | crupest <crupest@outlook.com> | 2024-11-11 01:12:29 +0800 |
---|---|---|
committer | Yuqian Yang <crupest@crupest.life> | 2024-12-19 21:42:01 +0800 |
commit | a7fb0508867ee53934be18a6606aa8bc12c2645b (patch) | |
tree | 5994f0a62733b13f9f330e3515260ae20dc4a0bd /dropped/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs | |
parent | 7692763f83ad1be735b0b9e9ab0af8ce666d8de8 (diff) | |
download | crupest-a7fb0508867ee53934be18a6606aa8bc12c2645b.tar.gz crupest-a7fb0508867ee53934be18a6606aa8bc12c2645b.tar.bz2 crupest-a7fb0508867ee53934be18a6606aa8bc12c2645b.zip |
HALF WORK: 2024.12.19
Re-organize file structure.
Diffstat (limited to 'dropped/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs')
-rw-r--r-- | dropped/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/dropped/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs b/dropped/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs new file mode 100644 index 0000000..8942979 --- /dev/null +++ b/dropped/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs @@ -0,0 +1,101 @@ +namespace CrupestApi.Commons.Crud; + +public static class CrudWebApplicationExtensions +{ + public static WebApplication UseCrudCore(this WebApplication app) + { + app.Use(async (context, next) => + { + try + { + await next(); + } + catch (EntityNotExistException) + { + await context.ResponseMessageAsync("Requested entity does not exist.", StatusCodes.Status404NotFound); + } + catch (UserException e) + { + await context.ResponseMessageAsync(e.Message); + } + }); + + return app; + } + + public static WebApplication MapCrud<TEntity>(this WebApplication app, string path, string? permission) where TEntity : class + { + app.MapGet(path, async (context) => + { + if (!context.RequirePermission(permission)) return; + 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))); + }); + + app.MapGet(path + "/{key}", async (context) => + { + if (!context.RequirePermission(permission)) return; + var crudService = context.RequestServices.GetRequiredService<CrudService<TEntity>>(); + var entityJsonHelper = context.RequestServices.GetRequiredService<EntityJsonHelper<TEntity>>(); + var key = context.Request.RouteValues["key"]?.ToString(); + if (key == null) + { + await context.ResponseMessageAsync("Please specify a key in path."); + return; + } + + var entity = crudService.GetByKey(key); + await context.ResponseJsonAsync(entityJsonHelper.ConvertEntityToDictionary(entity)); + }); + + app.MapPost(path, async (context) => + { + if (!context.RequirePermission(permission)) return; + var crudService = context.RequestServices.GetRequiredService<CrudService<TEntity>>(); + var entityJsonHelper = context.RequestServices.GetRequiredService<EntityJsonHelper<TEntity>>(); + var jsonDocument = await context.Request.ReadJsonAsync(); + var key = crudService.Create(entityJsonHelper.ConvertJsonToEntityForInsert(jsonDocument.RootElement)); + await context.ResponseJsonAsync(entityJsonHelper.ConvertEntityToDictionary(crudService.GetByKey(key))); + }); + + app.MapPatch(path + "/{key}", async (context) => + { + if (!context.RequirePermission(permission)) return; + var key = context.Request.RouteValues["key"]?.ToString(); + var crudService = context.RequestServices.GetRequiredService<CrudService<TEntity>>(); + var entityJsonHelper = context.RequestServices.GetRequiredService<EntityJsonHelper<TEntity>>(); + if (key == null) + { + await context.ResponseMessageAsync("Please specify a key in path."); + return; + } + + var jsonDocument = await context.Request.ReadJsonAsync(); + var entity = entityJsonHelper.ConvertJsonToEntityForUpdate(jsonDocument.RootElement, out var updateBehavior); + var newKey = crudService.UpdateByKey(key, entity, updateBehavior); + await context.ResponseJsonAsync(entityJsonHelper.ConvertEntityToDictionary(crudService.GetByKey(newKey))); + }); + + app.MapDelete(path + "/{key}", async (context) => + { + if (!context.RequirePermission(permission)) return; + var crudService = context.RequestServices.GetRequiredService<CrudService<TEntity>>(); + var key = context.Request.RouteValues["key"]?.ToString(); + if (key == null) + { + await context.ResponseMessageAsync("Please specify a key in path."); + return; + } + + var deleted = crudService.DeleteByKey(key); + if (deleted) + await context.ResponseMessageAsync("Deleted.", StatusCodes.Status200OK); + else + await context.ResponseMessageAsync("Not exist.", StatusCodes.Status200OK); + }); + + return app; + } +} |