@@ -25,7 +25,7 @@ public Task<IEnumerable<JsonObject>> Get(HttpRequest request, JsonContext contex
2525 var array = context . LoadTable ( name ) ;
2626 var matchedItem = array . SingleOrDefault ( row => 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,7 +35,14 @@ 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 ;
38+ var lastKey = array
39+ . Select ( row => row
40+ . AsObject ( )
41+ . FirstOrDefault ( o => o . Key . ToLower ( ) == "id" ) . Value ? . GetValue < int > ( ) )
42+ . Select ( x => x . GetValueOrDefault ( ) )
43+ . Max ( ) ;
44+
45+ var key = lastKey + 1 ;
3946 newObj . AsObject ( ) . Add ( "Id" , key . ToString ( ) ) ;
4047 array . Add ( newObj ) ;
4148 context . SaveChanges ( ) ;
@@ -45,7 +52,29 @@ public Task<int> Insert(HttpRequest request, JsonContext context, string name, J
4552
4653 public Task Update ( HttpRequest request , JsonContext context , string name , int id , JsonObject newObj , CancellationToken cancellationToken )
4754 {
48- throw new NotImplementedException ( ) ;
55+ var array = context . LoadTable ( name ) ;
56+ var matchedItem = array . SingleOrDefault ( row => row
57+ . AsObject ( )
58+ . Any ( o => o . Key . ToLower ( ) == "id" && o . Value . GetValue < int > ( ) == id )
59+ ) ? . AsObject ( ) ;
60+ if ( matchedItem != null )
61+ {
62+ var updates = newObj
63+ . GroupJoin ( matchedItem , o => o . Key , i => i . Key , ( o , i ) => new { NewValue = o , OldValue = i . FirstOrDefault ( ) } )
64+ . Where ( x => x . NewValue . Key . ToLower ( ) != "id" )
65+ . ToList ( ) ;
66+ foreach ( var newField in updates )
67+ {
68+ if ( newField . OldValue . Value != null )
69+ {
70+ matchedItem . Remove ( newField . OldValue . Key ) ;
71+ }
72+ matchedItem . Add ( newField . NewValue . Key , JsonValue . Create ( newField . NewValue . Value . GetValue < string > ( ) ) ) ;
73+ }
74+ context . SaveChanges ( ) ;
75+ }
76+
77+ return Task . CompletedTask ;
4978 }
5079
5180 public Task < bool > Delete ( HttpRequest request , JsonContext context , string name , int id , CancellationToken cancellationToken )
@@ -55,7 +84,7 @@ public Task<bool> Delete(HttpRequest request, JsonContext context, string name,
5584 . Select ( ( value , index ) => new { value , index } )
5685 . SingleOrDefault ( row => row . value
5786 . AsObject ( )
58- . Any ( o => o . Key . ToLower ( ) == "id" && o . Value . ToString ( ) == id . ToString ( ) ) ) ;
87+ . Any ( o => o . Key . ToLower ( ) == "id" && o . Value ? . GetValue < int > ( ) == id ) ) ;
5988 if ( matchedItem != null )
6089 {
6190 array . RemoveAt ( matchedItem . index ) ;
0 commit comments