From 625abfc4731a509e97ebcdb728302e40bf473dae Mon Sep 17 00:00:00 2001 From: Dries Verbeke Date: Fri, 22 Jul 2022 01:31:16 +0200 Subject: [PATCH] Json Repository Helper fixes - implemented an update - insert should take max of the key + 1, instead of just counting - remove unused constructor prarameter --- .../Repositories/Json/RepositoryHelper.cs | 48 +++++++++++++------ .../Json/RepositoryHelperFactory.cs | 7 ++- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/InstantAPIs/Repositories/Json/RepositoryHelper.cs b/InstantAPIs/Repositories/Json/RepositoryHelper.cs index 79f2432..db6c305 100644 --- a/InstantAPIs/Repositories/Json/RepositoryHelper.cs +++ b/InstantAPIs/Repositories/Json/RepositoryHelper.cs @@ -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 { private readonly Func _setSelector; - private readonly InstantAPIsOptions.TableOptions _config; - public JsonRepositoryHelper(Func setSelector, InstantAPIsOptions.TableOptions config) + public RepositoryHelper(Func setSelector, InstantAPIsOptions.TableOptions config) { _setSelector = setSelector; - _config = config; } public Task> Get(HttpRequest request, Context context, string name, CancellationToken cancellationToken) @@ -24,9 +21,9 @@ public Task> Get(HttpRequest request, Context context, s public Task 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() == id) )?.AsObject(); return Task.FromResult(matchedItem); } @@ -36,8 +33,13 @@ public Task 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()) + .Select(x => x.GetValueOrDefault()) + .Max(); + + var key = lastKey + 1; + newObj.AsObject().Add("id", key); array.Add(newObj); context.SaveChanges(); @@ -47,8 +49,25 @@ public Task 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() == 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())); + } + context.SaveChanges(); + } return Task.CompletedTask; } @@ -58,9 +77,9 @@ public Task 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() == id)); if (matchedItem != null) { array.RemoveAt(matchedItem.index); @@ -69,4 +88,5 @@ public Task Delete(HttpRequest request, Context context, string name, int return Task.FromResult(true); } + } diff --git a/InstantAPIs/Repositories/Json/RepositoryHelperFactory.cs b/InstantAPIs/Repositories/Json/RepositoryHelperFactory.cs index 58209bc..01a45b5 100644 --- a/InstantAPIs/Repositories/Json/RepositoryHelperFactory.cs +++ b/InstantAPIs/Repositories/Json/RepositoryHelperFactory.cs @@ -1,5 +1,4 @@ -using System.Net.Http.Json; -using System.Text.Json.Nodes; +using System.Text.Json.Nodes; namespace InstantAPIs.Repositories.Json; @@ -13,8 +12,8 @@ public IRepositoryHelper Create)returnValue;