How do you map a relationship by a primary key in Restkit? - objective-c

Haven't been able to find an up to date example on how to map a relationship like the one below.
{
"seats": [
{
"number": "2A",
"user_id": 1
},
{
"number": "4E",
"user_id": 2
}
],
"users": [
{
"id": 1,
"name": "Foo"
},
{
"id": 2,
"name": "Bar"
}
]
}
using RestKit (0.9.3) to a model of
User
NSUInteger id
NSString* name
Seat
NSString* number
User* user;

I don't believe RestKit can do exactly what you want out of the box.
Your best bet might be to modify your data to be more RestKit friendly. You could do this via the objectLoader:willMapData: delegate method:
http://restkit.org/api/0.9.3/Protocols/RKObjectLoaderDelegate.html#//api/name/objectLoader:willMapData:

Related

how to POST array to Strapi

here is my API get request:
{
"data": [
{
"id": 2,
"attributes": {
"title": "something",
"game": "default",
"players": "static",
}
}
],
}
I would like to set "Players" to an array like below:
"attributes": {
"title": "23123",
"game": "1231",
"players": [
"1" : "static1",
"2" : "static2",
"3" : static3",
],
}
how can i make this array?
You need to make a component called "Player" from content-type builder and then add this component with repeatable to your main entity type.

Validating that a property value exists withing the keys of an object

Wise crowd,
I already have a working JSON Schema (v0.7) to validate my data. This is an example of valid JSON:
{
"people": [
{ "id": 1, "name": "bob" },
...
]
}
Now I need to a bunch of strings in it:
{
"people": [
{ "id": 1, "name": "bob", "appears_in": "long_string_id_1" },
{ "id": 2, "name": "ann", "appears_in": "long_string_id_1" }
...
],
"long_strings": {
"long_string_id_1": "blah blah blah.....",
...
}
}
What I need is:
a value for key appears_in MUST be a key of the long_strings object
(optional) a key of the long_strings object MUST be used as value in on of the appears_in key
Property dependencies are nice, but don't seem to address my needs.
Any idea?
And this question is not a duplicate, because I do not know the values in advance.
Sorry. You cannot do this in JSON schema. You cannot reference data in your schema.

How to define a related resource URI in JSON:API

In the json:api format relationships are defined with a type and a id.
Like in the example bellow. The article has a relationship with the type people and the id 9.
Now if i want to fetch the related resource i use the URI from "links.related"
// ...
{
"type": "articles",
"id": "1",
"attributes": {
"title": "Rails is Omakase"
},
"relationships": {
"author": {
"links": {
"self": "http://example.com/articles/1/relationships/author",
"related": "http://example.com/articles/1/author"
},
"data": { "type": "people", "id": "9" }
}
},
"links": {
"self": "http://example.com/articles/1"
}
}
// ...
But in my case the related resource (people) are in a separate API. There is no way to get the full people data from the articles API nor is it possible to include it. The only way to get the related data would be a call to:
http://example.com/v1-2/people/9/
Where can i define the relation between the URI and people:9
Or in other words: How would a client know where to fetch the related resource?

Backand (Back&) schema setup for user favorites

I am struggling with setting up the notion of user favorites in the Backand schema editor. An example for what I am trying to do is as follows:
Say I have a website selling cars. Users are authenticated and on any car they like they can "favorite" that car. When favorited, that specific car object or id should be added to the users favorites property so that the user can return to or call upon their favorites list at any time.
In Back&s schema editor I have added a collection property to my user object but I am completely confused because this just creates a 1 to many relationship.
The Back& documentation is pretty lacking at this point so I figured I'd bring it up here so others who run into this can see it as well. What is the best way to accomplish this common functionality in Back&? Any help would be greatly appreciated.
I think what you are actually trying to do needs a many-to-many relation which is well documents here and has a code pen example here.
i.e. each user can favorite many cars and each car can be a favorite of many users
so the json would look somthing like this
[
{
"name": "user_car",
"fields": {
"user": {
"object": "user"
},
"car": {
"object": "car"
}
}
},
{
"name": "user",
"fields": {
"user_car": {
"collection": "user_car",
"via": "car"
},
"email": {
"type": "string"
},
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
}
}
},
{
"name": "car",
"fields": {
"user_car": {
"collection": "user_car",
"via": "car"
},
"name": {
"type": "string"
},
"model": {
"type": "int"
}
}
}
}
]

RestKit Mapping concatenate

I have some json like this :
{
"items": [
{
"datas": [
{
"date": "2015-01-20T00:00+0100"
},
{
"date": "2015-01-21T00:00+0100"
}
],
"id": "100"
},
{
"datas": [
{
"date": "2015-01-20T00:00+0100"
},
{
"date": "2015-01-21T00:00+0100"
}
],
"id": "200"
}
],
"id": "itemset1"
}
I have an Object, data, with some info and a date.
I would like to set a property identifier to a value composed with the parent id and the date, like 100_2015-01-20T00:00+0100
I can get the parent id in my identifier with the following code :
[dataMapping addAttributeMappingsFromDictionary:#{"#parent.id":#"identifier",...}];
Is they a way do concatenate value with RestKit mapping?
You can't concatenate directly, no. Instead you would store both of the attributes and provide a public method which concatenates and returns the value. Or perhaps set the variables to transient and on willSave (or similar) convert them and persistently store the concatenated value.