aboutsummaryrefslogtreecommitdiff
path: root/dropped/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs
blob: 8942979fda06e9e3372f38a24b0414dc27044218 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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;
    }
}