@@ -23,9 +23,9 @@ public Task<IEnumerable<JsonObject>> Get(HttpRequest request, JsonContext contex
23
23
public Task < JsonObject ? > GetById ( HttpRequest request , JsonContext context , string name , int id , CancellationToken cancellationToken )
24
24
{
25
25
var array = context . LoadTable ( name ) ;
26
- var matchedItem = array . SingleOrDefault ( row => row
26
+ var matchedItem = array . SingleOrDefault ( row => row != null && row
27
27
. 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 )
29
29
) ? . AsObject ( ) ;
30
30
return Task . FromResult ( matchedItem ) ;
31
31
}
@@ -35,8 +35,13 @@ public Task<int> Insert(HttpRequest request, JsonContext context, string name, J
35
35
{
36
36
37
37
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 ) ;
40
45
array . Add ( newObj ) ;
41
46
context . SaveChanges ( ) ;
42
47
@@ -45,17 +50,38 @@ public Task<int> Insert(HttpRequest request, JsonContext context, string name, J
45
50
46
51
public Task Update ( HttpRequest request , JsonContext context , string name , int id , JsonObject newObj , CancellationToken cancellationToken )
47
52
{
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 ;
49
75
}
50
76
51
77
public Task < bool > Delete ( HttpRequest request , JsonContext context , string name , int id , CancellationToken cancellationToken )
52
78
{
53
79
var array = context . LoadTable ( name ) ;
54
80
var matchedItem = array
55
81
. 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 ) ) ;
59
85
if ( matchedItem != null )
60
86
{
61
87
array . RemoveAt ( matchedItem . index ) ;
0 commit comments