1
1
using Microsoft . AspNetCore . Http ;
2
- using System . Net . Http . Json ;
3
2
using System . Text . Json . Nodes ;
4
3
5
4
namespace InstantAPIs . Repositories . Json ;
6
5
7
- internal class JsonRepositoryHelper :
6
+ internal class RepositoryHelper :
8
7
IRepositoryHelper < Context , JsonArray , JsonObject , int >
9
8
{
10
9
private readonly Func < Context , JsonArray > _setSelector ;
11
- private readonly InstantAPIsOptions . TableOptions < JsonObject , int > _config ;
12
10
13
- public JsonRepositoryHelper ( Func < Context , JsonArray > setSelector , InstantAPIsOptions . TableOptions < JsonObject , int > config )
11
+ public RepositoryHelper ( Func < Context , JsonArray > setSelector , InstantAPIsOptions . TableOptions < JsonObject , int > config )
14
12
{
15
13
_setSelector = setSelector ;
16
- _config = config ;
17
14
}
18
15
19
16
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
24
21
public Task < JsonObject ? > GetById ( HttpRequest request , Context context , string name , int id , CancellationToken cancellationToken )
25
22
{
26
23
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
28
25
. 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 )
30
27
) ? . AsObject ( ) ;
31
28
return Task . FromResult ( matchedItem ) ;
32
29
}
@@ -36,8 +33,13 @@ public Task<int> Insert(HttpRequest request, Context context, string name, JsonO
36
33
{
37
34
38
35
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 ) ;
41
43
array . Add ( newObj ) ;
42
44
context . SaveChanges ( ) ;
43
45
@@ -47,8 +49,25 @@ public Task<int> Insert(HttpRequest request, Context context, string name, JsonO
47
49
public Task Update ( HttpRequest request , Context context , string name , int id , JsonObject newObj , CancellationToken cancellationToken )
48
50
{
49
51
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
+ }
52
71
53
72
return Task . CompletedTask ;
54
73
}
@@ -58,9 +77,9 @@ public Task<bool> Delete(HttpRequest request, Context context, string name, int
58
77
var array = context . LoadTable ( name ) ;
59
78
var matchedItem = array
60
79
. 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 ) ) ;
64
83
if ( matchedItem != null )
65
84
{
66
85
array . RemoveAt ( matchedItem . index ) ;
@@ -69,4 +88,5 @@ public Task<bool> Delete(HttpRequest request, Context context, string name, int
69
88
70
89
return Task . FromResult ( true ) ;
71
90
}
91
+
72
92
}
0 commit comments