@@ -7,69 +7,107 @@ namespace Mock;
77
88public static class JsonApiExtensions
99{
10- public static WebApplication UseJsonRoutes ( this WebApplication app )
10+
11+ static JsonAPIsConfig _Config ;
12+
13+ public static WebApplication UseJsonRoutes ( this WebApplication app , Action < JsonAPIsConfigBuilder > options = null )
1114 {
12- var writableDoc = JsonNode . Parse ( File . ReadAllText ( "mock.json" ) ) ;
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 ) ) ;
1325
1426 // print API
1527 foreach ( var elem in writableDoc ? . Root . AsObject ( ) . AsEnumerable ( ) )
1628 {
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 )
1734 Console . WriteLine ( string . Format ( "GET /{0}" , elem . Key . ToLower ( ) ) ) ;
18- Console . WriteLine ( string . Format ( "GET /{0}" , elem . Key . ToLower ( ) ) + "/id" ) ;
19- Console . WriteLine ( string . Format ( "POST /{0}" , elem . Key . ToLower ( ) ) ) ;
20- Console . WriteLine ( string . Format ( "DELETE /{0}" , elem . Key . ToLower ( ) ) + "/id" ) ;
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+
2145 Console . WriteLine ( " " ) ;
2246 }
2347
2448 // setup routes
2549 foreach ( var elem in writableDoc ? . Root . AsObject ( ) . AsEnumerable ( ) )
2650 {
51+
52+ var thisEntity = _Config . Tables . FirstOrDefault ( t => t . Name . Equals ( elem . Key , StringComparison . InvariantCultureIgnoreCase ) ) ;
53+ if ( thisEntity == null ) continue ;
54+
2755 var arr = elem . Value . AsArray ( ) ;
28- app . MapGet ( string . Format ( "/{0}" , elem . Key ) , ( ) => elem . Value . ToString ( ) ) ;
29- app . MapGet ( string . Format ( "/{0}" , elem . Key ) + "/{id}" , ( int id ) =>
30- {
31- var matchedItem = arr . SingleOrDefault ( row => row
32- . AsObject ( )
33- . Any ( o => o . Key . ToLower ( ) == "id" && int . Parse ( o . Value . ToString ( ) ) == id )
34- ) ;
35- return matchedItem ;
36- } ) ;
37- app . MapPost ( string . Format ( "/{0}" , elem . Key ) , async ( HttpRequest request ) =>
38- {
39- string content = string . Empty ;
40- using ( StreamReader reader = new StreamReader ( request . Body ) )
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 ) =>
4172 {
42- content = await reader . ReadToEndAsync ( ) ;
43- }
44- var newNode = JsonNode . Parse ( content ) ;
45- var array = elem . Value . AsArray ( ) ;
46- newNode . AsObject ( ) . Add ( "Id" , array . Count ( ) + 1 ) ;
47- array . Add ( newNode ) ;
48-
49- File . WriteAllText ( "mock.json" , writableDoc . ToString ( ) ) ;
50- return content ;
51- } ) ;
52- app . MapPut ( string . Format ( "/{0}" , elem . Key ) , ( ) =>
53- {
54- return "TODO" ;
55- } ) ;
56- app . MapDelete ( string . Format ( "/{0}" , elem . Key ) + "/{id}" , ( int id ) =>
57- {
58-
59- var matchedItem = arr
60- . Select ( ( value , index ) => new { value , index } )
61- . SingleOrDefault ( row => row . value
62- . AsObject ( )
63- . Any ( o => o . Key . ToLower ( ) == "id" && int . Parse ( o . Value . ToString ( ) ) == id )
64- ) ;
65- if ( matchedItem != null )
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 ) , ( ) =>
6689 {
67- arr . RemoveAt ( matchedItem . index ) ;
68- File . WriteAllText ( "mock.json" , writableDoc . ToString ( ) ) ;
69- }
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+ }
70108
71- return "OK" ;
72- } ) ;
109+ return "OK" ;
110+ } ) ;
73111
74112 } ;
75113
0 commit comments