Skip to content

Commit d2ec13d

Browse files
committed
PATCH resource attributes
1 parent 3914e52 commit d2ec13d

File tree

4 files changed

+68
-9
lines changed

4 files changed

+68
-9
lines changed

JsonApiDotNetCore/Abstractions/ModelAccessor.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public static object SetValuesOnModelInstance(object model, Dictionary<string, o
5555
}
5656
}
5757

58-
5958
return model;
6059
}
6160
}

JsonApiDotNetCore/Controllers/JsonApiController.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using JsonApiDotNetCore.Abstractions;
22
using JsonApiDotNetCore.Data;
33
using Microsoft.AspNetCore.Mvc;
4+
using System.Reflection;
5+
using System.Collections.Generic;
46

57
namespace JsonApiDotNetCore.Controllers
68
{
@@ -40,14 +42,32 @@ public ObjectResult Post(object entity)
4042
return new CreatedResult(JsonApiContext.HttpContext.Request.Path, entity);
4143
}
4244

43-
public ObjectResult Put(string id, object entity)
45+
public ObjectResult Patch(string id, Dictionary<PropertyInfo, object> entityPatch)
4446
{
47+
var entity = _resourceRepository.Get(id);
48+
if(entity == null) {
49+
return new NotFoundObjectResult(null);
50+
}
51+
52+
entity = PatchEntity(entity, entityPatch);
53+
_resourceRepository.SaveChanges();
54+
4555
return new OkObjectResult(entity);
4656
}
4757

4858
public ObjectResult Delete(string id)
4959
{
5060
return new OkObjectResult(null);
5161
}
62+
63+
protected object PatchEntity(object entity, Dictionary<PropertyInfo, object> entityPatch)
64+
{
65+
foreach(var attrPatch in entityPatch)
66+
{
67+
attrPatch.Key.SetValue(entity, attrPatch.Value);
68+
}
69+
70+
return entity;
71+
}
5272
}
5373
}

JsonApiDotNetCore/Routing/Router.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ private ObjectResult ActivateControllerMethod(JsonApiController controller)
5757
return string.IsNullOrEmpty(route.ResourceId) ? controller.Get() : controller.Get(route.ResourceId);
5858
case "POST":
5959
return controller.Post(new JsonApiDeserializer(_jsonApiContext).GetEntityFromRequest());
60-
case "PUT":
61-
return controller.Put(route.ResourceId, null);
60+
case "PATCH":
61+
return controller.Patch(route.ResourceId, new JsonApiDeserializer(_jsonApiContext).GetEntityPatch());
6262
case "DELETE":
6363
return controller.Delete(route.ResourceId);
6464
default:

JsonApiDotNetCore/Services/JsonApiDeserializer.cs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
4+
using System.Reflection;
35
using JsonApiDotNetCore.Abstractions;
46
using JsonApiDotNetCore.Configuration;
7+
using JsonApiDotNetCore.Extensions;
58
using JsonApiDotNetCore.JsonApi;
69
using Newtonsoft.Json;
710
using Newtonsoft.Json.Linq;
11+
using System.Collections.Generic;
812

913
namespace JsonApiDotNetCore.Services
1014
{
@@ -18,21 +22,57 @@ public JsonApiDeserializer(JsonApiContext jsonApiContext)
1822

1923
public object GetEntityFromRequest()
2024
{
21-
var body = GetRequestBody(_context.HttpContext.Request.Body);
22-
var document = JsonConvert.DeserializeObject<JsonApiDocument>(body);
2325
var entity = Activator.CreateInstance(_context.GetEntityType());
24-
var datum = ((JsonApiDatum) ((JObject) document.Data).ToObject(typeof(JsonApiDatum)));
26+
var datum = GetSignularJsonApiDatum();
2527
var attributes = datum.Attributes;
2628
var relationships = datum.Relationships;
2729
return ModelAccessor.SetValuesOnModelInstance(entity, attributes, relationships);
2830
}
2931

30-
private static string GetRequestBody(Stream body)
32+
private JsonApiDatum GetSignularJsonApiDatum()
3133
{
32-
using (var reader = new StreamReader(body))
34+
var document = GetJsonApiDocument();
35+
return ((JsonApiDatum) ((JObject) document.Data).ToObject(typeof(JsonApiDatum)));
36+
}
37+
38+
private JsonApiDocument GetJsonApiDocument()
39+
{
40+
var body = GetRequestBody();
41+
return JsonConvert.DeserializeObject<JsonApiDocument>(body);
42+
}
43+
44+
private string GetRequestBody()
45+
{
46+
using (var reader = new StreamReader(_context.HttpContext.Request.Body))
3347
{
3448
return reader.ReadToEnd();
3549
}
3650
}
51+
52+
public Dictionary<PropertyInfo, object> GetEntityPatch()
53+
{
54+
var datum = GetSignularJsonApiDatum();
55+
var attributes = datum.Attributes;
56+
// var relationships = datum.Relationships;
57+
58+
var patchDefinitions = new Dictionary<PropertyInfo, object>();
59+
60+
var modelProperties = _context.GetEntityType().GetProperties().ToList();
61+
62+
foreach (var attribute in attributes)
63+
{
64+
modelProperties.ForEach(pI =>
65+
{
66+
if (pI.Name.ToProperCase() == attribute.Key.ToProperCase())
67+
{
68+
var convertedValue = Convert.ChangeType(attribute.Value, pI.PropertyType);
69+
patchDefinitions.Add(pI, convertedValue);
70+
}
71+
});
72+
}
73+
74+
return patchDefinitions;
75+
}
76+
3777
}
3878
}

0 commit comments

Comments
 (0)