Skip to content

Commit

Permalink
Json Repository Helper fixes
Browse files Browse the repository at this point in the history
- implemented an update
- insert should take max of the key + 1, instead of just counting
- remove unused constructor prarameter
  • Loading branch information
Dries Verbeke committed Jul 22, 2022
1 parent c614f7a commit 625abfc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
48 changes: 34 additions & 14 deletions InstantAPIs/Repositories/Json/RepositoryHelper.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
using Microsoft.AspNetCore.Http;
using System.Net.Http.Json;
using System.Text.Json.Nodes;

namespace InstantAPIs.Repositories.Json;

internal class JsonRepositoryHelper :
internal class RepositoryHelper :
IRepositoryHelper<Context, JsonArray, JsonObject, int>
{
private readonly Func<Context, JsonArray> _setSelector;
private readonly InstantAPIsOptions.TableOptions<JsonObject, int> _config;

public JsonRepositoryHelper(Func<Context, JsonArray> setSelector, InstantAPIsOptions.TableOptions<JsonObject, int> config)
public RepositoryHelper(Func<Context, JsonArray> setSelector, InstantAPIsOptions.TableOptions<JsonObject, int> config)
{
_setSelector = setSelector;
_config = config;
}

public Task<IEnumerable<JsonObject>> Get(HttpRequest request, Context context, string name, CancellationToken cancellationToken)
Expand All @@ -24,9 +21,9 @@ public Task<IEnumerable<JsonObject>> Get(HttpRequest request, Context context, s
public Task<JsonObject?> GetById(HttpRequest request, Context context, string name, int id, CancellationToken cancellationToken)
{
var array = context.LoadTable(name);
var matchedItem = array.SingleOrDefault(row => (row ?? throw new Exception("No row found"))
var matchedItem = array.SingleOrDefault(row => row != null && row
.AsObject()
.Any(o => o.Key.ToLower() == "id" && o.Value?.ToString() == id.ToString())
.Any(o => o.Key.ToLower() == "id" && o.Value?.GetValue<int>() == id)
)?.AsObject();
return Task.FromResult(matchedItem);
}
Expand All @@ -36,8 +33,13 @@ public Task<int> Insert(HttpRequest request, Context context, string name, JsonO
{

var array = context.LoadTable(name);
var key = array.Count + 1;
newObj.AsObject().Add("Id", key.ToString());
var lastKey = array
.Select(row => row?.AsObject().FirstOrDefault(o => o.Key.ToLower() == "id").Value?.GetValue<int>())
.Select(x => x.GetValueOrDefault())
.Max();

var key = lastKey + 1;
newObj.AsObject().Add("id", key);
array.Add(newObj);
context.SaveChanges();

Expand All @@ -47,8 +49,25 @@ public Task<int> Insert(HttpRequest request, Context context, string name, JsonO
public Task Update(HttpRequest request, Context context, string name, int id, JsonObject newObj, CancellationToken cancellationToken)
{
var array = context.LoadTable(name);
array.Add(newObj);
context.SaveChanges();
var matchedItem = array.SingleOrDefault(row => row != null
&& row.AsObject().Any(o => o.Key.ToLower() == "id" && o.Value?.GetValue<int>() == id)
)?.AsObject();
if (matchedItem != null)
{
var updates = newObj
.GroupJoin(matchedItem, o => o.Key, i => i.Key, (o, i) => new { NewValue = o, OldValue = i.FirstOrDefault() })
.Where(x => x.NewValue.Key.ToLower() != "id")
.ToList();
foreach (var newField in updates)
{
if (newField.OldValue.Value != null)
{
matchedItem.Remove(newField.OldValue.Key);
}
matchedItem.Add(newField.NewValue.Key, JsonValue.Create(newField.NewValue.Value?.GetValue<string>()));
}
context.SaveChanges();
}

return Task.CompletedTask;
}
Expand All @@ -58,9 +77,9 @@ public Task<bool> Delete(HttpRequest request, Context context, string name, int
var array = context.LoadTable(name);
var matchedItem = array
.Select((value, index) => new { value, index })
.SingleOrDefault(row => (row.value ?? throw new Exception("No json value found"))
.AsObject()
.Any(o => o.Key.ToLower() == "id" && o.Value?.ToString() == id.ToString()));
.SingleOrDefault(row => row.value == null
? false
: row.value.AsObject().Any(o => o.Key.ToLower() == "id" && o.Value?.GetValue<int>() == id));
if (matchedItem != null)
{
array.RemoveAt(matchedItem.index);
Expand All @@ -69,4 +88,5 @@ public Task<bool> Delete(HttpRequest request, Context context, string name, int

return Task.FromResult(true);
}

}
7 changes: 3 additions & 4 deletions InstantAPIs/Repositories/Json/RepositoryHelperFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Net.Http.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Nodes;

namespace InstantAPIs.Repositories.Json;

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

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

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

0 comments on commit 625abfc

Please sign in to comment.