I'm using json-server to create a test API. I'd like to have make the API endpoint: http://localhost:3000/posts/1/comments
/posts and /posts/1 works fine but I'm not sure why /posts/1/comments doesn't. Am I missing something?
Here is the json
{
"posts": [
{
"id": 1,
"title": "Post 1",
"comments": [
{
"id": 1,
"body": "This is comment 1"
}
]
},
{
"id": 2,
"title": "Post 2",
"comments": []
}
]
}
The comments are a separate object referenced by postId, as follows:
{
"posts": [
{
"id": 1,
"title": "json-server",
"author": "typicode"
}
],
"comments": [
{
"id": 1,
"body": "some comment",
"postId": 1
},
{
"id": 2,
"body": "this is another",
"postId": 1
}
],
"profile": {
"name": "typicode"
}
}
So you can do this :
localhost/posts/1
{
id: 1,
title: "json-server",
author: "typicode"
}
localhost/posts/1/comments
[
{
id: 1,
body: "some comment",
postId: 1
},
{
id: 2,
body: "this is another",
postId: 1
}
]
http://jsonplaceholder.typicode.com/comments
Related
I cant figure out how to make this conversion iterating a json.
I have this pojo in my backend:
class Part{
Long id;
String name;
Set<Part> parts = new HashSet<>();
}
Every part can have parts and this part more parts and so on.
I get this parts from httpclient in angular and get this json:
[{
"id": 1,
"name": "Parts A and B",
"parts": [{
"id": 2,
"name": "A",
"parts": [{
"id": 4,
"name": "A1",
"parts": []
}]
},
{
"id": 3,
"name": "B",
"parts": []
}
]
},
{
"id": 2,
"name": "A",
"parts": []
},
{
"id": 3,
"name": "B",
"parts": []
},
{
"id": 4,
"name": "A1",
"parts": []
}
]
And need to convert to this to populate a PrimeNG TreeTable:
{
"data": [{
"data": {
"name": "Parts A and B",
"id": "1"
},
"children": [{
"data": {
"name": "Part A",
"id": "2"
},
"children": [{
"data": {
"name": "A1",
"id": "4"
}
}]
},
{
"data": {
"name": "Part B",
"id": "3"
},
"children": []
}
]
},
{
"data": {
"name": "Part A",
"id": "2"
},
"children": []
},
{
"data": {
"name": "Part B",
"id": "3"
},
"children": []
},
{
"data": {
"name": "A1",
"id": "4"
},
"children": []
}
]
}
How can I do that?
In angular I get this in an array parts: Part[] and need partsTree: TreeNode[]
Thanks!!!
Its just a simple conversion by a map;
interface PartAPI{
id: number;
name: string;
parts : PartAPI[];
}
interface Data {
id: number;
name: string;
}
interface Part {
data : Data;
children : Part[];
}
console.log('a')
let convert = (inputArr: PartAPI[] = []) : Part[] => {
return inputArr.map(partApi => ({ data : { id : partApi.id , name : partApi.name }, children: convert(partApi.parts) }) as Part)
}
let data : PartAPI[] = [{
"id": 1,
"name": "Parts A and B",
"parts": [{
"id": 2,
"name": "A",
"parts": [{
"id": 4,
"name": "A1",
"parts": []
}]
},
{
"id": 3,
"name": "B",
"parts": []
}
]
},
{
"id": 2,
"name": "A",
"parts": []
},
{
"id": 3,
"name": "B",
"parts": []
},
{
"id": 4,
"name": "A1",
"parts": []
}
]
console.log(convert(data));
In ExtJS 6.02, I would like to join 2 or more models into a single store.
1 to 1 relationship:
Given users that can only be associated with a single user detail row in DB:
"users": [
{
"id": 1,
"name": "Philip J. Fry",
"userDetail": 1
},
{
"id": 2,
"name": "Hubert Farnsworth",
"userDetail": 2
},
{
"id": 3,
"name": "Turanga Leela",
"userDetail": 3
},
{
"id": 4,
"name": "Amy Wong",
"userDetail": 4
}
]
"usersDetails": [
{
"id": 1,
"email": "jfry#a.com",
"sex": "m"
},
{
"id": 2,
"email": "hubert#a.com",
"sex": "m"
},
{
"id": 3,
"email": "leela#a.com",
"sex": "f"
},
{
"id": 4,
"email": "amy#a.com",
"sex": "m"
}
]
I would like to have this on the store:
"users": [
{
"id": 1,
"name": "Philip J. Fry",
"email": "jfry#a.com",
"sex": "m"
},
{
"id": 2,
"name": "Hubert Farnsworth",
"email": "hubert#a.com",
"sex": "m"
},
{
"id": 3,
"name": "Turanga Leela",
"email": "leela#a.com",
"sex": "f"
},
{
"id": 4,
"name": "Amy Wong",
"email": "amy#a.com",
"sex": "m"
}
]
1 to many relationship:
Given users that can have multiple posts in BD:
"users": [
{
"id": 1,
"name": "Philip J. Fry",
"userDetail": 1
},
{
"id": 2,
"name": "Hubert Farnsworth",
"userDetail": 2
},
{
"id": 3,
"name": "Turanga Leela",
"userDetail": 3
},
{
"id": 4,
"name": "Amy Wong",
"userDetail": 4
}
]
"posts": [
{
"id": 1,
"user": 1,
"title": "Post 1 title",
"body": "Post 1 body"
},
{
"id": 2,
"user": 1,
"title": "Post 2 title",
"body": "Post 2 body"
},
{
"id": 3,
"user": 2,
"title": "Post 3 title",
"body": "Post 3 body"
},
{
"id": 4,
"user": 3,
"title": "Post 4 title",
"body": "Post 4 body"
}
]
I would like to have a store containing:
"items": [
{
"id": 1,
"name": "Philip J. Fry",
"posts": [
{
"id": 1,
"title": "Post 1 title",
"body": "Post 1 body"
},
{
"id": 2,
"title": "Post 2 title",
"body": "Post 2 body"
}
]
},
{
"id": 2,
"name": "Hubert Farnsworth",
"posts": [
{
"id": 3,
"user": 2,
"title": "Post 3 title",
"body": "Post 3 body"
}
]
},
{
"id": 3,
"name": "Turanga Leela",
"userDetail": 3,
"posts": [
{
"id": 4,
"user": 3,
"title": "Post 4 title",
"body": "Post 4 body"
}
]
},
{
"id": 4,
"name": "Amy Wong",
"posts": []
}
]
Many to 1 relationship:
This is actually my actual issue, I have a DB table with multiple items that each can only match a single item in another table.
Should be same as 1 to many.
Many to many relationship:
Basicly same as 1 to many I guess?
I have seem this solution: https://fiddle.sencha.com/#fiddle/1hs9&view/editor it doesn't cover everything and maybe there's a better option.
According to Sencha support:
The concept of joining records doesn't really exist within ExtJS but
you can, by making use of renderers, access and print record
association data.
I have a simple fiddle of your example above showing you how you may
achieve this. Please note that dynamic loading of association (which
you have in your example) makes using renderers a bit tricky, but not
impossible, as the loading of the association info is asynchronous in
nature and the renderer cannot wait for the response. The work around
is to refresh the entire grid when all association stores have
finished loading.
https://fiddle.sencha.com/#fiddle/3d1v&view/editor
I have the below response in my postman Body .
{
"count": 3,
"next": null,
"previous": null,
"results": [
{
"id": "c2004ef952894279920417ba8283d26a",
"name": "attrib_inv_bu",
"modified_date": "2020-07-06T02:15:06.839577",
"display_group": {
"id": "attr_dis_267a405426184764816e504b4f0a565b",
"name": "Custom"
},
"api_url": "https://abc.xyz.com/api/v1/attributes/attr_c2004ef952894279920417ba8283d26a/",
"url": "https://abc.xyz.com/manage/matter_attributes/2707/"
},
{
"id": "c2004ef95289422324232a8283d26a",
"name": "attrib_idsdw_sfs",
"modified_date": "2020-07-06T02:11:06.839577",
"display_group": {
"id": "attr_dis_267a4054261dfsf64816e504b4f0a565b",
"name": "Custom"
},
"api_url": "https://abc.xyz.com/api/v1/attributes/attr_c2004ef952894279920417ba8ssd3d26a/",
"url": "https://abc.xyz.com/manage/matter_attributes/2709/"
},
{
"id": "c2004ef9we22e2e8283d26a",
"name": "attrib_iasa_ac",
"modified_date": "2020-07-06T02:17:06.839577",
"display_group": {
"id": "attr_dis_267a405426184764816e5dsds4f0a565b",
"name": "Custom"
},
"api_url": "https://abc.xyz.com/api/v1/attributes/attr_c2004ef952894279920417ba8283sds26a/",
"url": "https://abc.xyz.com/manage/matter_attributes/2708/"
}
]
}
I want to assert whether I have 3 different ids match with the count 3 that is displayed in the body.
res = pm.response.json()
res.count === res.results.length
you can use in expect as :
pm.expect(res.count).to.be.equal(res.results.length)
If you want to verify they are indeed unique:
let resultidlits = []
result.forEach((a) => {
resultidlits.includes(a.id)? null : resultidlits.push(a.id)
})
pm.expect(res.count).to.be.equal(resultidlits.length)
$categoryData = $this->compCatObj->find()->select(['CompanyCategory.id', 'CompanyCategory.name','CompanyCategory.restricted'])->contain(['CompanyItems'=>['fields'=>['CompanyItems.id','CompanyItems.company_category_id','CompanyItems.name']]])->toArray();
Response
{
"status": "success",
"message": "List of company categories",
"data": [
{
"id": 1,
"name": "Breakfast gdfgedfgdf",
"restricted": "no",
"company_items": []
},
{
"id": 2,
"name": "Breakfast",
"restricted": "yes",
"company_items": []
}
]
}
i need category_id instead of id. Is there any way to do this without using forloop.
yes you can do this by a small change in your code,
$categoryData = $this->compCatObj->find()->select(['category_id' => 'CompanyCategory.id', 'CompanyCategory.name','CompanyCategory.restricted'])->contain(['CompanyItems'=>['fields'=>['CompanyItems.id','CompanyItems.company_category_id','CompanyItems.name']]])->toArray();
Response
{
"status": "success",
"message": "List of company categories",
"data": [
{
"category_id": 1,
"name": "Breakfast gdfgedfgdf",
"restricted": "no",
"company_items": []
},
{
"category_id": 2,
"name": "Breakfast",
"restricted": "yes",
"company_items": []
}
]
}
I have json stored in a column (oid) with the following structure:
{
"fullName": "test test",
"personDetails": {
"address": "Advisor",
"phoneNumber": "clare.railton#heptonstalls.co.uk"
},
"id": "6765788-yt67",
"submittedDocument": {
"answers": [
{
"questionId": "2",
"responses": [
{
"value": "123456"
}
]
},
{
"questionId": "2.1",
"responses": [
{
"IdA": 1,
"IdB": 1,
"value": "false"
},
{
"IdA": 1,
"IdB": 2,
"value": "false"
},
{
"IdA": 1,
"IdB": 3,
"value": "false"
},
{
"IdA": 1,
"IdB": 4,
"value": "true"
}
]
}
]
},
"date": "2018-11-22",
"PeriodId": 123456
}
How would i get the value of the response to all question numbers? I have managed to get the json structure from the oid column using the lo_get function but i am struggling to capture the values i need.
Many thanks
Are you sure you want a large object to store the json data?
Postgres can handle json columntypes in tables:
CREATE TABLE test_json (id INTEGER PRIMARY KEY, json json);
INSERT INTO test_json VALUES(1,'{
"fullName": "test test",
"personDetails": {
"address": "Advisor",
"phoneNumber": "clare.railton#heptonstalls.co.uk"
},
"id": "6765788-yt67",
"submittedDocument": {
"answers": [
{
"questionId": "2",
"responses": [
{
"value": "123456"
}
]
},
{
"questionId": "2.1",
"responses": [
{
"IdA": 1,
"IdB": 1,
"value": "false"
},
{
"IdA": 1,
"IdB": 2,
"value": "false"
},
{
"IdA": 1,
"IdB": 3,
"value": "false"
},
{
"IdA": 1,
"IdB": 4,
"value": "true"
}
]
}
]
},
"date": "2018-11-22",
"PeriodId": 123456
}');
SELECT json_extract_path(
json_array_elements(
json_extract_path(
json_array_elements(
json_extract_path((json),'submittedDocument','answers')
),'responses')
),'value'
)FROM test_json;
See:
https://www.postgresql.org/docs/current/functions-json.html