Skip to content

Commit 6dbb35b

Browse files
committed
first pass at persisting POST'd data
1 parent 3c23d0f commit 6dbb35b

File tree

6 files changed

+18
-7
lines changed

6 files changed

+18
-7
lines changed

JsonApiDotNetCore/Controllers/JsonApiController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public ObjectResult Get(string id)
3535

3636
public ObjectResult Post(object entity)
3737
{
38+
_resourceRepository.Add(entity);
39+
_resourceRepository.SaveChanges();
3840
return new CreatedResult(JsonApiContext.HttpContext.Request.Path, entity);
3941
}
4042

JsonApiDotNetCore/Data/ResourceRepository.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,10 @@ private object GetRelated(string id, RelationalRoute relationalRoute)
4040
return relationalRoute.BaseModelType.GetProperties().FirstOrDefault(pi => pi.Name.ToCamelCase() == relationalRoute.RelationshipName.ToCamelCase()).GetValue(entity);
4141
}
4242

43-
4443
private IQueryable GetDbSetFromContext(string propName)
4544
{
4645
var dbContext = _context.DbContext;
47-
return (IQueryable)dbContext.GetType().GetProperties().FirstOrDefault(pI => pI.Name.ToCamelCase() == propName)?.GetValue(dbContext, null);
46+
return (IQueryable)dbContext.GetType().GetProperties().FirstOrDefault(pI => pI.Name.ToProperCase() == propName.ToProperCase())?.GetValue(dbContext, null);
4847
}
4948

5049
private object GetEntityById(Type modelType, string id, string includedRelationship)
@@ -66,6 +65,15 @@ private object GetEntityById(Type modelType, string id, string includedRelations
6665
return (dbSet as IEnumerable<dynamic>).SingleOrDefault(x => x.Id.ToString() == id);
6766
}
6867

68+
public void Add(object entity)
69+
{
70+
((DbSet<object>)GetDbSetFromContext(_context.Route.BaseRouteDefinition.ContextPropertyName)).Add(entity);
71+
}
72+
73+
public int SaveChanges()
74+
{
75+
return ((DbContext)_context.DbContext).SaveChanges();
76+
}
6977

7078
}
7179
}

JsonApiDotNetCore/JsonApi/DocumentBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ private IJsonApiResource EntityToJsonApiResource(object entity)
5050
var resource = entity as IJsonApiResource;
5151
if (resource != null) return resource;
5252

53-
var attributes = TypeDescriptor.GetAttributes(_resource);
53+
var attributes = TypeDescriptor.GetAttributes(entity);
5454
var type = ((JsonApiResourceAttribute)attributes[typeof(JsonApiResourceAttribute)]).JsonApiResourceType;
55-
return (IJsonApiResource)_context.Configuration.ResourceMapper.Map(_resource, _resource.GetType(), type);
55+
return (IJsonApiResource)_context.Configuration.ResourceMapper.Map(entity, entity.GetType(), type);
5656
}
5757

5858
private JsonApiDatum ResourceToJsonApiDatum(IJsonApiResource resource)

JsonApiDotNetCore/Routing/RouteBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public Route BuildFromRequest()
4848

4949
private bool PathStringIsEmpty(PathString pathString)
5050
{
51-
return pathString.HasValue ? string.IsNullOrEmpty(pathString.ToString().TrimStart('/')) : false;
51+
return pathString.HasValue ? string.IsNullOrEmpty(pathString.ToString().TrimStart('/')) : true;
5252
}
5353

5454
private PathString SetBaseRouteDefinition()

JsonApiDotNetCore/Routing/Router.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private ObjectResult ActivateControllerMethod(JsonApiController controller)
5656
case "GET":
5757
return string.IsNullOrEmpty(route.ResourceId) ? controller.Get() : controller.Get(route.ResourceId);
5858
case "POST":
59-
return controller.Post(new JsonApiDeserializer(_jsonApiContext).GetEntity());
59+
return controller.Post(new JsonApiDeserializer(_jsonApiContext).GetEntityFromRequest());
6060
case "PUT":
6161
return controller.Put(route.ResourceId, null);
6262
case "DELETE":

JsonApiDotNetCore/Services/JsonApiDeserializer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using JsonApiDotNetCore.Configuration;
55
using JsonApiDotNetCore.JsonApi;
66
using Newtonsoft.Json;
7+
using Newtonsoft.Json.Linq;
78

89
namespace JsonApiDotNetCore.Services
910
{
@@ -20,7 +21,7 @@ public object GetEntityFromRequest()
2021
var body = GetRequestBody(_context.HttpContext.Request.Body);
2122
var document = JsonConvert.DeserializeObject<JsonApiDocument>(body);
2223
var entity = Activator.CreateInstance(_context.GetEntityType());
23-
return ModelAccessor.SetValuesOnModelInstance(entity, ((JsonApiDatum) document.Data).Attributes);
24+
return ModelAccessor.SetValuesOnModelInstance(entity, ((JsonApiDatum)((JObject)document.Data).ToObject(typeof(JsonApiDatum))).Attributes);
2425
}
2526

2627
private static string GetRequestBody(Stream body)

0 commit comments

Comments
 (0)