11using Microsoft . AspNetCore . Http ;
2- using System . Net . Http . Json ;
32using System . Text . Json . Nodes ;
43
54namespace InstantAPIs . Repositories . Json ;
65
7- internal class JsonRepositoryHelper :
6+ internal class RepositoryHelper :
87 IRepositoryHelper < Context , JsonArray , JsonObject , int >
98{
109 private readonly Func < Context , JsonArray > _setSelector ;
11- private readonly InstantAPIsOptions . TableOptions < JsonObject , int > _config ;
1210
13- public JsonRepositoryHelper ( Func < Context , JsonArray > setSelector , InstantAPIsOptions . TableOptions < JsonObject , int > config )
11+ public RepositoryHelper ( Func < Context , JsonArray > setSelector , InstantAPIsOptions . TableOptions < JsonObject , int > config )
1412 {
1513 _setSelector = setSelector ;
16- _config = config ;
1714 }
1815
1916 public Task < IEnumerable < JsonObject > > Get ( HttpRequest request , Context context , string name , CancellationToken cancellationToken )
@@ -24,9 +21,9 @@ public Task<IEnumerable<JsonObject>> Get(HttpRequest request, Context context, s
2421 public Task < JsonObject ? > GetById ( HttpRequest request , Context context , string name , int id , CancellationToken cancellationToken )
2522 {
2623 var array = context . LoadTable ( name ) ;
27- var matchedItem = array . SingleOrDefault ( row => ( row ?? throw new Exception ( "No row found" ) )
24+ var matchedItem = array . SingleOrDefault ( row => row != null && row
2825 . AsObject ( )
29- . Any ( o => o . Key . ToLower ( ) == "id" && o . Value ? . ToString ( ) == id . ToString ( ) )
26+ . Any ( o => o . Key . ToLower ( ) == "id" && o . Value ? . GetValue < int > ( ) == id )
3027 ) ? . AsObject ( ) ;
3128 return Task . FromResult ( matchedItem ) ;
3229 }
@@ -36,8 +33,13 @@ public Task<int> Insert(HttpRequest request, Context context, string name, JsonO
3633 {
3734
3835 var array = context . LoadTable ( name ) ;
39- var key = array . Count + 1 ;
40- newObj . AsObject ( ) . Add ( "Id" , key . ToString ( ) ) ;
36+ var lastKey = array
37+ . Select ( row => row ? . AsObject ( ) . FirstOrDefault ( o => o . Key . ToLower ( ) == "id" ) . Value ? . GetValue < int > ( ) )
38+ . Select ( x => x . GetValueOrDefault ( ) )
39+ . Max ( ) ;
40+
41+ var key = lastKey + 1 ;
42+ newObj . AsObject ( ) . Add ( "id" , key ) ;
4143 array . Add ( newObj ) ;
4244 context . SaveChanges ( ) ;
4345
@@ -47,8 +49,25 @@ public Task<int> Insert(HttpRequest request, Context context, string name, JsonO
4749 public Task Update ( HttpRequest request , Context context , string name , int id , JsonObject newObj , CancellationToken cancellationToken )
4850 {
4951 var array = context . LoadTable ( name ) ;
50- array . Add ( newObj ) ;
51- context . SaveChanges ( ) ;
52+ var matchedItem = array . SingleOrDefault ( row => row != null
53+ && row . AsObject ( ) . Any ( o => o . Key . ToLower ( ) == "id" && o . Value ? . GetValue < int > ( ) == id )
54+ ) ? . AsObject ( ) ;
55+ if ( matchedItem != null )
56+ {
57+ var updates = newObj
58+ . GroupJoin ( matchedItem , o => o . Key , i => i . Key , ( o , i ) => new { NewValue = o , OldValue = i . FirstOrDefault ( ) } )
59+ . Where ( x => x . NewValue . Key . ToLower ( ) != "id" )
60+ . ToList ( ) ;
61+ foreach ( var newField in updates )
62+ {
63+ if ( newField . OldValue . Value != null )
64+ {
65+ matchedItem . Remove ( newField . OldValue . Key ) ;
66+ }
67+ matchedItem . Add ( newField . NewValue . Key , JsonValue . Create ( newField . NewValue . Value ? . GetValue < string > ( ) ) ) ;
68+ }
69+ context . SaveChanges ( ) ;
70+ }
5271
5372 return Task . CompletedTask ;
5473 }
@@ -58,9 +77,9 @@ public Task<bool> Delete(HttpRequest request, Context context, string name, int
5877 var array = context . LoadTable ( name ) ;
5978 var matchedItem = array
6079 . Select ( ( value , index ) => new { value , index } )
61- . SingleOrDefault ( row => ( row . value ?? throw new Exception ( "No json value found" ) )
62- . AsObject ( )
63- . Any ( o => o . Key . ToLower ( ) == "id" && o . Value ? . ToString ( ) == id . ToString ( ) ) ) ;
80+ . SingleOrDefault ( row => row . value == null
81+ ? false
82+ : row . value . AsObject ( ) . Any ( o => o . Key . ToLower ( ) == "id" && o . Value ? . GetValue < int > ( ) == id ) ) ;
6483 if ( matchedItem != null )
6584 {
6685 array . RemoveAt ( matchedItem . index ) ;
@@ -69,4 +88,5 @@ public Task<bool> Delete(HttpRequest request, Context context, string name, int
6988
7089 return Task . FromResult ( true ) ;
7190 }
91+
7292}
0 commit comments