@@ -7,123 +7,131 @@ namespace InstantAPIs;
77public static class JsonApiExtensions
88{
99
10- static JsonAPIsConfig _Config ;
10+ static JsonAPIsConfig _Config = new JsonAPIsConfig ( ) ;
1111
12- public static WebApplication UseJsonRoutes ( this WebApplication app , Action < JsonAPIsConfigBuilder > options = null )
13- {
12+ public static WebApplication UseJsonRoutes ( this WebApplication app , Action < JsonAPIsConfigBuilder > ? options = null )
13+ {
1414
15- var builder = new JsonAPIsConfigBuilder ( ) ;
16- _Config = new JsonAPIsConfig ( ) ;
17- if ( options != null )
15+ var builder = new JsonAPIsConfigBuilder ( ) ;
16+ if ( options != null )
1817 {
19- options ( builder ) ;
20- _Config = builder . Build ( ) ;
21- }
22-
23- var writableDoc = JsonNode . Parse ( File . ReadAllText ( _Config . JsonFilename ) ) ;
24-
25- // print API
26- foreach ( var elem in writableDoc ? . Root . AsObject ( ) . AsEnumerable ( ) )
27- {
28-
29- var thisEntity = _Config . Tables . FirstOrDefault ( t => t . Name . Equals ( elem . Key , StringComparison . InvariantCultureIgnoreCase ) ) ;
30- if ( thisEntity == null ) continue ;
31-
32- if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . Get ) == ApiMethodsToGenerate . Get )
33- Console . WriteLine ( string . Format ( "GET /{0}" , elem . Key . ToLower ( ) ) ) ;
34-
35- if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . GetById ) == ApiMethodsToGenerate . GetById )
36- Console . WriteLine ( string . Format ( "GET /{0}" , elem . Key . ToLower ( ) ) + "/id" ) ;
37-
38- if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . Insert ) == ApiMethodsToGenerate . Insert )
39- Console . WriteLine ( string . Format ( "POST /{0}" , elem . Key . ToLower ( ) ) ) ;
40-
41- if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . Update ) == ApiMethodsToGenerate . Update )
42- Console . WriteLine ( string . Format ( "PUT /{0}" , elem . Key . ToLower ( ) ) ) ;
43-
44- if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . Delete ) == ApiMethodsToGenerate . Delete )
45- Console . WriteLine ( string . Format ( "DELETE /{0}" , elem . Key . ToLower ( ) ) + "/id" ) ;
46-
47- Console . WriteLine ( " " ) ;
48- }
49-
50- // setup routes
51- foreach ( var elem in writableDoc ? . Root . AsObject ( ) . AsEnumerable ( ) )
52- {
53-
54- var thisEntity = _Config . Tables . FirstOrDefault ( t => t . Name . Equals ( elem . Key , StringComparison . InvariantCultureIgnoreCase ) ) ;
55- if ( thisEntity == null ) continue ;
56-
57- var arr = elem . Value . AsArray ( ) ;
58-
59- if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . Get ) == ApiMethodsToGenerate . Get )
60- app . MapGet ( string . Format ( "/{0}" , elem . Key ) , ( ) => elem . Value . ToString ( ) ) ;
61-
62- if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . GetById ) == ApiMethodsToGenerate . GetById )
63- app . MapGet ( string . Format ( "/{0}" , elem . Key ) + "/{id}" , ( int id ) =>
64- {
65- var matchedItem = arr . SingleOrDefault ( row => row
66- . AsObject ( )
67- . Any ( o => o . Key . ToLower ( ) == "id" && int . Parse ( o . Value . ToString ( ) ) == id )
68- ) ;
69- return matchedItem ;
70- } ) ;
71-
72- if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . Insert ) == ApiMethodsToGenerate . Insert )
73- app . MapPost ( string . Format ( "/{0}" , elem . Key ) , async ( HttpRequest request ) =>
74- {
75- string content = string . Empty ;
76- using ( StreamReader reader = new StreamReader ( request . Body ) )
77- {
78- content = await reader . ReadToEndAsync ( ) ;
79- }
80- var newNode = JsonNode . Parse ( content ) ;
81- var array = elem . Value . AsArray ( ) ;
82- newNode . AsObject ( ) . Add ( "Id" , array . Count ( ) + 1 ) ;
83- array . Add ( newNode ) ;
84-
85- File . WriteAllText ( _Config . JsonFilename , writableDoc . ToString ( ) ) ;
86- return content ;
87- } ) ;
88-
89- if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . Update ) == ApiMethodsToGenerate . Update )
90- app . MapPut ( string . Format ( "/{0}" , elem . Key ) , async ( HttpRequest request ) =>
18+ options ( builder ) ;
19+ _Config = builder . Build ( ) ;
20+ }
21+
22+ var writableDoc = JsonNode . Parse ( File . ReadAllText ( _Config . JsonFilename ) )
23+ ?? throw new Exception ( "Missing json file" ) ;
24+ var sets = writableDoc . Root ? . AsObject ( ) ? . AsEnumerable ( ) ;
25+ if ( sets == null || ! sets . Any ( ) ) return app ;
26+
27+ // print API
28+ foreach ( var elem in sets )
29+ {
30+
31+ var thisEntity = _Config . Tables . FirstOrDefault ( t => elem . Key . Equals ( t . Name , StringComparison . InvariantCultureIgnoreCase ) ) ;
32+ if ( thisEntity == null ) continue ;
33+
34+ if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . Get ) == ApiMethodsToGenerate . Get )
35+ Console . WriteLine ( string . Format ( "GET /{0}" , elem . Key . ToLower ( ) ) ) ;
36+
37+ if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . GetById ) == ApiMethodsToGenerate . GetById )
38+ Console . WriteLine ( string . Format ( "GET /{0}" , elem . Key . ToLower ( ) ) + "/id" ) ;
39+
40+ if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . Insert ) == ApiMethodsToGenerate . Insert )
41+ Console . WriteLine ( string . Format ( "POST /{0}" , elem . Key . ToLower ( ) ) ) ;
42+
43+ if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . Update ) == ApiMethodsToGenerate . Update )
44+ Console . WriteLine ( string . Format ( "PUT /{0}" , elem . Key . ToLower ( ) ) ) ;
45+
46+ if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . Delete ) == ApiMethodsToGenerate . Delete )
47+ Console . WriteLine ( string . Format ( "DELETE /{0}" , elem . Key . ToLower ( ) ) + "/id" ) ;
48+
49+ Console . WriteLine ( " " ) ;
50+ }
51+
52+ // setup routes
53+ foreach ( var elem in sets )
9154 {
92- string content = string . Empty ;
93- using ( StreamReader reader = new StreamReader ( request . Body ) )
94- {
95- content = await reader . ReadToEndAsync ( ) ;
96- }
97- var newNode = JsonNode . Parse ( content ) ;
98- var array = elem . Value . AsArray ( ) ;
99- array . Add ( newNode ) ;
100-
101- File . WriteAllText ( _Config . JsonFilename , writableDoc . ToString ( ) ) ;
102-
103- return "OK" ;
104- } ) ;
105-
106- if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . Delete ) == ApiMethodsToGenerate . Delete )
107- app . MapDelete ( string . Format ( "/{0}" , elem . Key ) + "/{id}" , ( int id ) =>
108- {
109-
110- var matchedItem = arr
111- . Select ( ( value , index ) => new { value , index } )
112- . SingleOrDefault ( row => row . value
113- . AsObject ( )
114- . Any ( o => o . Key . ToLower ( ) == "id" && int . Parse ( o . Value . ToString ( ) ) == id )
115- ) ;
116- if ( matchedItem != null )
117- {
118- arr . RemoveAt ( matchedItem . index ) ;
119- File . WriteAllText ( _Config . JsonFilename , writableDoc . ToString ( ) ) ;
120- }
121-
122- return "OK" ;
123- } ) ;
124-
125- } ;
126-
127- return app ;
128- }
55+
56+ var thisEntity = _Config . Tables . FirstOrDefault ( t => elem . Key . Equals ( t . Name , StringComparison . InvariantCultureIgnoreCase ) ) ;
57+ if ( thisEntity == null ) continue ;
58+
59+ var arr = elem . Value ? . AsArray ( ) ?? new JsonArray ( ) ;
60+
61+ if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . Get ) == ApiMethodsToGenerate . Get )
62+ app . MapGet ( string . Format ( "/{0}" , elem . Key ) , ( ) => elem . Value ? . ToString ( ) ) ;
63+
64+ if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . GetById ) == ApiMethodsToGenerate . GetById )
65+ app . MapGet ( string . Format ( "/{0}" , elem . Key ) + "/{id}" , ( int id ) =>
66+ {
67+ var matchedItem = arr == null ? null :
68+ arr . SingleOrDefault ( row => row != null && row
69+ . AsObject ( )
70+ . Any ( o => o . Key . ToLower ( ) == "id" && o . Value != null && int . Parse ( o . Value . ToString ( ) ) == id )
71+ ) ;
72+ return matchedItem == null ? Results . NotFound ( ) : Results . Ok ( matchedItem ) ;
73+ } ) ;
74+
75+ if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . Insert ) == ApiMethodsToGenerate . Insert )
76+ app . MapPost ( string . Format ( "/{0}" , elem . Key ) , async ( HttpRequest request ) =>
77+ {
78+ string content = string . Empty ;
79+ using ( StreamReader reader = new StreamReader ( request . Body ) )
80+ {
81+ content = await reader . ReadToEndAsync ( ) ;
82+ }
83+ var newNode = JsonNode . Parse ( content ) ;
84+ var array = elem . Value ? . AsArray ( ) ;
85+ if ( newNode == null || array == null ) return Results . NotFound ( ) ;
86+ newNode . AsObject ( ) . Add ( "Id" , array . Count ( ) + 1 ) ;
87+ array . Add ( newNode ) ;
88+
89+ File . WriteAllText ( _Config . JsonFilename , writableDoc . ToString ( ) ) ;
90+ return Results . Ok ( content ) ;
91+ } ) ;
92+
93+ if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . Update ) == ApiMethodsToGenerate . Update )
94+ app . MapPut ( string . Format ( "/{0}" , elem . Key ) , async ( HttpRequest request ) =>
95+ {
96+ string content = string . Empty ;
97+ using ( StreamReader reader = new StreamReader ( request . Body ) )
98+ {
99+ content = await reader . ReadToEndAsync ( ) ;
100+ }
101+ var newNode = JsonNode . Parse ( content ) ;
102+ var array = elem . Value ? . AsArray ( ) ;
103+ if ( array != null )
104+ {
105+ array . Add ( newNode ) ;
106+
107+ File . WriteAllText ( _Config . JsonFilename , writableDoc . ToString ( ) ) ;
108+ }
109+
110+ return "OK" ;
111+ } ) ;
112+
113+ if ( ( thisEntity . ApiMethodsToGenerate & ApiMethodsToGenerate . Delete ) == ApiMethodsToGenerate . Delete )
114+ app . MapDelete ( string . Format ( "/{0}" , elem . Key ) + "/{id}" , ( int id ) =>
115+ {
116+
117+ var matchedItem = arr
118+ . Select ( ( value , index ) => new { value , index } )
119+ . SingleOrDefault ( row => row . value
120+ ? . AsObject ( )
121+ ? . Any ( o => o . Key . ToLower ( ) == "id" && o . Value != null && int . Parse ( o . Value . ToString ( ) ) == id )
122+ ?? false
123+ ) ;
124+ if ( matchedItem != null )
125+ {
126+ arr . RemoveAt ( matchedItem . index ) ;
127+ File . WriteAllText ( _Config . JsonFilename , writableDoc . ToString ( ) ) ;
128+ }
129+
130+ return "OK" ;
131+ } ) ;
132+
133+ } ;
134+
135+ return app ;
136+ }
129137}
0 commit comments