Skip to content

Commit 625abfc

Browse files
author
Dries Verbeke
committed
Json Repository Helper fixes
- implemented an update - insert should take max of the key + 1, instead of just counting - remove unused constructor prarameter
1 parent c614f7a commit 625abfc

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed
Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
using Microsoft.AspNetCore.Http;
2-
using System.Net.Http.Json;
32
using System.Text.Json.Nodes;
43

54
namespace 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
}

InstantAPIs/Repositories/Json/RepositoryHelperFactory.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Net.Http.Json;
2-
using System.Text.Json.Nodes;
1+
using System.Text.Json.Nodes;
32

43
namespace InstantAPIs.Repositories.Json;
54

@@ -13,8 +12,8 @@ public IRepositoryHelper<TContext, TSet, TEntity, TKey> Create<TContext, TSet, T
1312
{
1413
if (!typeof(TContext).IsAssignableTo(typeof(Context))) throw new ArgumentException("Context needs to derive from JsonContext");
1514

16-
var newRepositoryType = typeof(JsonRepositoryHelper);
17-
var returnValue = Activator.CreateInstance(newRepositoryType, setSelector, config)
15+
var newRepositoryType = typeof(RepositoryHelper);
16+
var returnValue = Activator.CreateInstance(newRepositoryType, setSelector)
1817
?? throw new Exception("Could not create an instance of the JsonRepository implementation");
1918

2019
return (IRepositoryHelper<TContext, TSet, TEntity, TKey>)returnValue;

0 commit comments

Comments
 (0)