From 4c5df72057fe02257e243de37930a47425a84722 Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Wed, 19 Feb 2025 02:05:39 +0800 Subject: chore(docker): remove crupest-api, forgejo and move dropped. --- .../Crud/CrudWebApplicationExtensions.cs | 101 --------------------- 1 file changed, 101 deletions(-) delete mode 100644 dropped/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs (limited to 'dropped/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs') diff --git a/dropped/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs b/dropped/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs deleted file mode 100644 index 8942979..0000000 --- a/dropped/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs +++ /dev/null @@ -1,101 +0,0 @@ -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(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>(); - var entityJsonHelper = context.RequestServices.GetRequiredService>(); - 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>(); - var entityJsonHelper = context.RequestServices.GetRequiredService>(); - 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>(); - var entityJsonHelper = context.RequestServices.GetRequiredService>(); - 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>(); - var entityJsonHelper = context.RequestServices.GetRequiredService>(); - 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>(); - 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; - } -} -- cgit v1.2.3