1+ using System . Text . Json ;
2+ using System . Text . Json . Nodes ;
3+ using Microsoft . AspNetCore . Builder ;
4+ using Microsoft . AspNetCore . Http ;
5+
6+ namespace Mock ;
7+
8+ public static class JsonApiExtensions
9+ {
10+
11+ static JsonAPIsConfig _Config ;
12+
13+ public static WebApplication UseJsonRoutes ( this WebApplication app , Action < JsonAPIsConfigBuilder > options = null )
14+ {
15+
16+ var builder = new JsonAPIsConfigBuilder ( ) ;
17+ _Config = new JsonAPIsConfig ( ) ;
18+ if ( options != null )
19+ {
20+ options ( builder ) ;
21+ _Config = builder . Build ( ) ;
22+ }
23+
24+ var writableDoc = JsonNode . Parse ( File . ReadAllText ( _Config . JsonFilename ) ) ;
25+
26+ // print API
27+ foreach ( var elem in writableDoc ? . Root . AsObject ( ) . AsEnumerable ( ) )
28+ {
29+
30+ var thisEntity = _Config . Tables . FirstOrDefault ( t => t . Name . Equals ( elem . Key , StringComparison . InvariantCultureIgnoreCase ) ) ;
31+ if ( thisEntity == null ) continue ;
32+
33+ if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . Get ) == ApiMethodsToGenerate . Get )
34+ Console . WriteLine ( string . Format ( "GET /{0}" , elem . Key . ToLower ( ) ) ) ;
35+
36+ if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . GetById ) == ApiMethodsToGenerate . GetById )
37+ Console . WriteLine ( string . Format ( "GET /{0}" , elem . Key . ToLower ( ) ) + "/id" ) ;
38+
39+ if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . Insert ) == ApiMethodsToGenerate . Insert )
40+ Console . WriteLine ( string . Format ( "POST /{0}" , elem . Key . ToLower ( ) ) ) ;
41+
42+ if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . Delete ) == ApiMethodsToGenerate . Delete )
43+ Console . WriteLine ( string . Format ( "DELETE /{0}" , elem . Key . ToLower ( ) ) + "/id" ) ;
44+
45+ Console . WriteLine ( " " ) ;
46+ }
47+
48+ // setup routes
49+ foreach ( var elem in writableDoc ? . Root . AsObject ( ) . AsEnumerable ( ) )
50+ {
51+
52+ var thisEntity = _Config . Tables . FirstOrDefault ( t => t . Name . Equals ( elem . Key , StringComparison . InvariantCultureIgnoreCase ) ) ;
53+ if ( thisEntity == null ) continue ;
54+
55+ var arr = elem . Value . AsArray ( ) ;
56+
57+ if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . Get ) == ApiMethodsToGenerate . Get )
58+ app . MapGet ( string . Format ( "/{0}" , elem . Key ) , ( ) => elem . Value . ToString ( ) ) ;
59+
60+ if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . GetById ) == ApiMethodsToGenerate . GetById )
61+ app . MapGet ( string . Format ( "/{0}" , elem . Key ) + "/{id}" , ( int id ) =>
62+ {
63+ var matchedItem = arr . SingleOrDefault ( row => row
64+ . AsObject ( )
65+ . Any ( o => o . Key . ToLower ( ) == "id" && int . Parse ( o . Value . ToString ( ) ) == id )
66+ ) ;
67+ return matchedItem ;
68+ } ) ;
69+
70+ if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . Insert ) == ApiMethodsToGenerate . Insert )
71+ app . MapPost ( string . Format ( "/{0}" , elem . Key ) , async ( HttpRequest request ) =>
72+ {
73+ string content = string . Empty ;
74+ using ( StreamReader reader = new StreamReader ( request . Body ) )
75+ {
76+ content = await reader . ReadToEndAsync ( ) ;
77+ }
78+ var newNode = JsonNode . Parse ( content ) ;
79+ var array = elem . Value . AsArray ( ) ;
80+ newNode . AsObject ( ) . Add ( "Id" , array . Count ( ) + 1 ) ;
81+ array . Add ( newNode ) ;
82+
83+ File . WriteAllText ( _Config . JsonFilename , writableDoc . ToString ( ) ) ;
84+ return content ;
85+ } ) ;
86+
87+ if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . Update ) == ApiMethodsToGenerate . Update )
88+ app . MapPut ( string . Format ( "/{0}" , elem . Key ) , ( ) =>
89+ {
90+ return "TODO" ;
91+ } ) ;
92+
93+ if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . Delete ) == ApiMethodsToGenerate . Delete )
94+ app . MapDelete ( string . Format ( "/{0}" , elem . Key ) + "/{id}" , ( int id ) =>
95+ {
96+
97+ var matchedItem = arr
98+ . Select ( ( value , index ) => new { value , index } )
99+ . SingleOrDefault ( row => row . value
100+ . AsObject ( )
101+ . Any ( o => o . Key . ToLower ( ) == "id" && int . Parse ( o . Value . ToString ( ) ) == id )
102+ ) ;
103+ if ( matchedItem != null )
104+ {
105+ arr . RemoveAt ( matchedItem . index ) ;
106+ File . WriteAllText ( _Config . JsonFilename , writableDoc . ToString ( ) ) ;
107+ }
108+
109+ return "OK" ;
110+ } ) ;
111+
112+ } ;
113+
114+ return app ;
115+ }
116+ }
0 commit comments