How to create dynamic content/Pages using JSON files in Gatsby? - json

I wish to create a complex routing in Gatsby using multiple JSON files:
First JSON file is based on routing:
File Location: /src/data/route.json
[
{
type: "Article",
title: "Post One",
slug: "/post-one",
data: "post-one.json"
},
{
type: "Course",
title: "Course One",
slug: "/course-one",
data: "course-one.json"
},
...
]
It is the Json file which contains route data.
Now in post-one.json file contains all the required data about the post that is required to render the post.
File Location: /src/data/articles/post-one.json
Routes: /post-one
{
title: "Post One",
head: {
title: "Some title here",
image: "/path/to/image.jpg",
description: "Some description here..."
},
body: {
// Body data here
...
}
}
But the main problem arises now as the courses is the collection of the different chapters.
Now for the main course page:
File location : /src/data/courses/course-name.json
Route is like: /course-name
{
title: "Course One",
head: {
title: "Some title here",
image: "/path/to/image.jpg",
description: "Some description here..."
},
info: {
// Some info about the course...
},
chapter: [
{
title: "chapter one",
slug: "/:courseId/chapter-one",
description: "Something about the chapter"
},
{
title: "chapter two",
slug: "/:courseId/chapter-two",
description: "Something about the chapter"
},
...
//Multiple chapters in the same course
]
}
Now the chapters will have the data like
File location : /src/data/chapters/chapter-name.json
Route: /courseId/chapter-name
{
title: "Chapter one",
description: "Some description here",
body: {
...
//Content for the body that can be rendered easily
}
}

Related

mongodb $lookup has_many association from embedded document

I have a boards collection, a lists collection, and a cards collection. An array of lists is embedded in a board document. I am trying to get an output that looks like this:
{
_id: 1,
title: "a board",
lists: [
{
_id: 1,
title: "a list",
cards: [ { _id: 1, title: "a card", list_id: 1 }, { _id: 2, title: "another card", list_id: 1 } ]
},
...
]
}
I want to nest the cards in the list it belongs to. The card document has a list_id field. I tried this:
db.boards.aggregate([
{ '$match' => { _id: 1 } },
{ '$lookup' => {
from: "cards",
localField: "lists._id",
foreignField: "list_id",
as: "cards"
} },
])
But that resulted in:
{
_id: 1,
title: "a board",
lists: [ { _id: 1, title: "a list" } ],
cards: [ { _id: 1, title: "a card", list_id: 1 }, { _id: 2, title: "another card", list_id: 1 } ]
}
I know that I have to use $unwind to get the result I want, but I can't get it to work
You need one additional aggregation pipeline step to "merge" these two lists and you can achieve it by running $map with embedded $filter. It simply works as a "join" operation on two arrays:
{
$project: {
_id: 1,
title: 1,
lists: {
$map: {
input: "$lists",
as: "list",
in: {
$mergeObjects: [
"$$list",
{
cards: {
$filter: {
input: "$cards",
cond: { $eq: [ "$$this.list_id", "$$list._id" ] }
}
}
}
]
}
}
}
}
}
Mongo Playground

Attach multiple files to a Google Classroom assignment?

I'm trying to create a Classroom assignment with more than one file attached, here is my code with one file:
Classroom.Courses.CourseWork.create({
courseId: id,
title : title,
description : desc,
workType : 'ASSIGNMENT',
materials: [
{
driveFile:{
driveFile: {
id: fileId,
title: fileName
},
shareMode: "STUDENT_COPY"
},
}
],
state : "PUBLISHED"
}, id)
How do I add another file? I have tried to duplicate the 'driveFile' with no luck:
materials: [
{
driveFile:{
driveFile: {
id: fileId,
title: fileName
},
shareMode: "STUDENT_COPY"
},
driveFile:{
driveFile: {
id: fileId,
title: fileName
},
shareMode: "STUDENT_COPY"
},
}
],
Can't find any documentation or other questions on this.
Thank you!
Try this code:
{
"workType": "ASSIGNMENT",
"materials": [
{
"driveFile": {
"driveFile": {
"id": "FILE_ID",
"title": "TestingFile"
},
"shareMode": "STUDENT_COPY"
}
},
{
"driveFile": {
"driveFile": {
"id": "FILE_ID",
"title": "Sample Docs"
},
"shareMode": "STUDENT_COPY"
}
}
],
"description": "Assignment 1 &2",
"title": "Assignment"
}
Here is the result:
I tried it using the Method: courses.courseWork.create Try this API to help me construct the request correctly.
Hope this helps.

Grails Creating JSON format output

I'm writing a Grails web app to take the input data below and reformat it into the output format below that then can be used to draw fancyTree view. I created a controller class with the code shown in the except that follows.
How can I iterate through to make the format like the output format shown below? I looked at how Groovy does each syntax but how can I use it to add ingredients?
code excerpt:
def list(){
def results = recipies.list()
def recipiesContents = [:] //Test List
recipiesContents=[]
for (record in results){
test.add([folder:true, title: results.Name, key: results.key
])
}
//render response types
withFormat {
html test
json {render test as JSON}
xml {render test as XML}
}
}
Input data:
{
recipies :[
{key: "1",
category:"Tuna Sandwich"
data:[{some data}],
ingredients: [
{item_name:"mayo",
brand: "My Own Brand"
},
{
item_name: "Tuna".
brand: "Bumble Bee"
}
]},
{key: "2",
category:"Chicken Noodle Soup"
data:[{some data}],
ingredients: [
{item_name:"condensed chicken soup",
brand: "Campbell"
},
{
item_name: "Noodles".
brand: "Top Ramen"
}
]}
]
}
output data:
[
{
title: "Tuna Sandwich"
ingredients: [
{title: "Tuna"},
{title: "mayo"}
]
},
{
title: "Chicken Noodle Soup",
ingredients: [
{title: "condensed chicken soup"},
{title: "Noodles"}
]
}
]
Have you tried:
for (record in results){
def ingredients = [:]
for (ingredient in record.ingredients) {
ingredients.add([title: ingredient.item_name)
}
test.add([title: results.category, ingredients: ingredients
])
}

Show specific attachment using json api for Wordpress

When I go to:
example.com/api/get_tag_posts/?dev=2&slug=ME3022&include=attachments
I see:
{
status: "ok",
count: 1,
pages: 1,
tag: {
id: 17,
slug: "me3022",
title: "ME3022",
description: "",
post_count: 1
},
posts: [
{
id: 181,
attachments: [
{
id: 397,
url: "http://example.com/files/2012/10/WebInstruction_TCH.pdf",
slug: "webinstruction_tch-3",
title: "Traditional Chinese",
description: "",
caption: "Traditional Chinese",
parent: 181,
mime_type: "application/pdf"
},
{
id: 398,
url: "http://example.com/files/2012/10/WebInstruction_SCH.pdf",
slug: "webinstruction_sch-3",
title: "Simplified Chinese",
description: "",
caption: "Simplified Chinese",
parent: 181,
mime_type: "application/pdf"
},
What I want to show is a specific attachment instead of all of the attachments. How can I do that?
To get the a specific attachment &include=attachments,id=51

how to display child and parent JSON data in each row of the list in Sencha using model and store

My server returning the following nested json. I want to display the fullname and text comes as a each row of the list. like,
apple Message2
apple Message1
Instead of this, I got one row contains all the data as the following:
apple Message2 Message1.
If json may be like this:
"packet": {
"fullname": "apple",
"wants": {
"text": "Message 2",
}
}
we can mention the field in model:
fields: [{
name: 'fullname',
type: 'string'
}, {
name: 'text',
mapping: 'wants.text'
}]
But I have array of wants, how to access the wants array through mapping?
json String:
"packet": {
"fullname": "apple",
"wants": [{
"text": "Message 2",
}, {
"text": "Message 1",
}]
}
Model:
Ext.define('myApp.model.Packet', {
extend: 'Ext.data.Model',
config: {
fields: [{
name: 'fullname',
type: 'string'
}, {
name: 'text',
mapping: 'wants[0].text'
} //Is this right? If right mean how to get next wants.how to iterate?
]
}
});
Store:
Ext.define('myApp.store.WantsStore', {
extend: 'Ext.data.Store',
config: {
model: 'myApp.model.Packet',
proxy: {
type: 'ajax',
url: 'server path',
reader: {
type: 'json',
rootProperty: 'packet'
}
}
}
});
List:
Ext.define('myappApp.view.WantsList', {
extend: 'Ext.List',
xtype: 'wantslist',
config: {
itemCls: 'my-dataview-item',
loadingText: 'Loading items...',
emptyText: '<div class="notes-list-empty-text">No Wants found.</div>',
itemTpl: '<div><p class="list-item-title">{fullname} <span id="time" class="list-item-title">{text}</span></p>'
}
});
Thanks in Advance!