@@ -23,9 +23,9 @@ public Task<IEnumerable<JsonObject>> Get(HttpRequest request, JsonContext contex
2323 public Task < JsonObject ? > GetById ( HttpRequest request , JsonContext context , string name , int id , CancellationToken cancellationToken )
2424 {
2525 var array = context . LoadTable ( name ) ;
26- var matchedItem = array . SingleOrDefault ( row => row
26+ var matchedItem = array . SingleOrDefault ( row => row != null && row
2727 . AsObject ( )
28- . Any ( o => o . Key . ToLower ( ) == "id" && o . Value . ToString ( ) == id . ToString ( ) )
28+ . Any ( o => o . Key . ToLower ( ) == "id" && o . Value ? . GetValue < int > ( ) == id )
2929 ) ? . AsObject ( ) ;
3030 return Task . FromResult ( matchedItem ) ;
3131 }
@@ -35,8 +35,13 @@ public Task<int> Insert(HttpRequest request, JsonContext context, string name, J
3535 {
3636
3737 var array = context . LoadTable ( name ) ;
38- var key = array . Count + 1 ;
39- newObj . AsObject ( ) . Add ( "Id" , key . ToString ( ) ) ;
38+ var lastKey = array
39+ . Select ( row => row ? . AsObject ( ) . FirstOrDefault ( o => o . Key . ToLower ( ) == "id" ) . Value ? . GetValue < int > ( ) )
40+ . Select ( x => x . GetValueOrDefault ( ) )
41+ . Max ( ) ;
42+
43+ var key = lastKey + 1 ;
44+ newObj . AsObject ( ) . Add ( "id" , key ) ;
4045 array . Add ( newObj ) ;
4146 context . SaveChanges ( ) ;
4247
@@ -45,17 +50,38 @@ public Task<int> Insert(HttpRequest request, JsonContext context, string name, J
4550
4651 public Task Update ( HttpRequest request , JsonContext context , string name , int id , JsonObject newObj , CancellationToken cancellationToken )
4752 {
48- throw new NotImplementedException ( ) ;
53+ var array = context . LoadTable ( name ) ;
54+ var matchedItem = array . SingleOrDefault ( row => row != null
55+ && row . AsObject ( ) . Any ( o => o . Key . ToLower ( ) == "id" && o . Value ? . GetValue < int > ( ) == id )
56+ ) ? . AsObject ( ) ;
57+ if ( matchedItem != null )
58+ {
59+ var updates = newObj
60+ . GroupJoin ( matchedItem , o => o . Key , i => i . Key , ( o , i ) => new { NewValue = o , OldValue = i . FirstOrDefault ( ) } )
61+ . Where ( x => x . NewValue . Key . ToLower ( ) != "id" )
62+ . ToList ( ) ;
63+ foreach ( var newField in updates )
64+ {
65+ if ( newField . OldValue . Value != null )
66+ {
67+ matchedItem . Remove ( newField . OldValue . Key ) ;
68+ }
69+ matchedItem . Add ( newField . NewValue . Key , JsonValue . Create ( newField . NewValue . Value ? . GetValue < string > ( ) ) ) ;
70+ }
71+ context . SaveChanges ( ) ;
72+ }
73+
74+ return Task . CompletedTask ;
4975 }
5076
5177 public Task < bool > Delete ( HttpRequest request , JsonContext context , string name , int id , CancellationToken cancellationToken )
5278 {
5379 var array = context . LoadTable ( name ) ;
5480 var matchedItem = array
5581 . Select ( ( value , index ) => new { value , index } )
56- . SingleOrDefault ( row => row . value
57- . AsObject ( )
58- . Any ( o => o . Key . ToLower ( ) == "id" && o . Value . ToString ( ) == id . ToString ( ) ) ) ;
82+ . SingleOrDefault ( row => row . value == null
83+ ? false
84+ : row . value . AsObject ( ) . Any ( o => o . Key . ToLower ( ) == "id" && o . Value ? . GetValue < int > ( ) == id ) ) ;
5985 if ( matchedItem != null )
6086 {
6187 array . RemoveAt ( matchedItem . index ) ;
0 commit comments