Chris Bitoy
11/30/2021, 4:42 PMVictor Iris
11/30/2021, 4:43 PMVictor Iris
11/30/2021, 4:44 PMChris Bitoy
11/30/2021, 4:45 PMChris Bitoy
11/30/2021, 4:46 PMVictor Iris
11/30/2021, 4:48 PMChris Bitoy
11/30/2021, 4:50 PMmodel Template {
id Int @id @default(autoincrement())
title String
email String? @unique
// Relation fields
questions Question[]
answers Answer[]
}
model Question {
id Int @id @default(autoincrement())
owner_id String? @default(cuid())
campaign_id String? @default(cuid())
freestyle_order Int? @unique
checked Boolean @default(false)
input_type String @default("radio")
ui_section Int? @default(3)
heading String?
child_questions String @db.VarChar(500)
answer_type Int?
correct_answer String?
point_value Int?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relation fields
answers Answer[]
Template Template? @relation(fields: [templateId], references: [id])
templateId Int?
}
model Answer {
id Int @id @default(autoincrement())
options String
question Question? @relation(fields: [questionId], references: [id])
questionId Int?
template Template? @relation(fields: [templateId], references: [id])
templateId Int?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
My routes look like this:
// Template endpoint
//Get all
router.get("/templates", async (req, res) => {
try {
const templates = await prisma.template.findMany({
include: {
questions: {
include: {
answers: true,
},
},
},
});
res.status(200).send({ data: templates, error: "", status: 200 });
} catch (error) {
console.log(error);
res.status(500).send({ data: {}, error: error, status: 500 });
}
});
//Create a template
<http://router.post|router.post>("/templates", async (req, res) => {
try {
const { title, email, id } = req.body;
const template = await prisma.template.create({
data: {
title,
email,
},
answers: {
connect: {
id: id,
},
},
});
res.status(200).send({ data: template, error: "", status: 200 });
} catch (error) {
console.log(error);
res.status(500).send({ data: {}, error: error, status: 500 });
}
});
//Questions endpoint
// Get all questions
router.get("/questions", async (req, res) => {
try {
const questions = await prisma.question.findMany({
include: {
Template: true,
answers: true,
},
});
res.status(200).send({ data: questions, error: "", status: 200 });
} catch (error) {
console.log(error);
res.status(500).send({ data: {}, error: error, status: 500 });
}
});
//Create a question
<http://router.post|router.post>("/questions", async (req, res) => {
const { child_questions, checked, input_type, answerID } = req.body;
try {
const question = await prisma.question.create({
data: {
child_questions,
checked,
input_type,
answers: {
connect: {
id: id,
},
},
},
});
res.status(200).send({ data: question, error: "", status: 200 });
} catch (error) {
console.log(error);
res.status(500).send({ data: {}, error: error, status: 500 });
}
});
//Answers endpoint
// Get all answers
router.get("/answers", async (req, res) => {
try {
const answers = await prisma.answer.findMany({
include: {
question: true,
},
});
res.status(200).send({ data: answers, error: "", status: 200 });
} catch (error) {
console.log(error);
res.status(500).send({ data: {}, error: error, status: 500 });
}
});
//Create a answer
<http://router.post|router.post>("/answers", async (req, res) => {
const { option } = req.body;
try {
const answers = await prisma.answer.create({
data: {
option,
},
question: {
connect: {
id: id,
},
},
template: {
connect: {
id: id,
},
},
});
res.status(200).send({ data: answers, error: "", status: 200 });
} catch (error) {
console.log(error);
res.status(500).send({ data: {}, error: error, status: 500 });
}
});
// Current state of Data Structure : http://3.215.90.93:5000/api/templates
// Desired/expected data structure
const questions = [
{
id: 1,
title: "Top 5 Questions",
email: "<mailto:me@myemail.com|me@myemail.com>",
questions: [
{
question:
"What features are most important to you when considering a new snack brand?",
checked: false,
input_type: "radio",
answers: [
"Heard of it - never tried it before",
"Tried it once before",
"Buy it regularly",
"Curious to learn more",
],
},
{
question:
"In what time frame are you considering buying a new snack brand?",
input_type: "radio",
checked: false,
answers: [
"Never heard of it",
"Heard of it - never tried it before",
"Tried it once before",
"Buy it regularly",
"Curious to learn more",
],
},
{
question: "How familiar are you with Zoey & Eva's Cookies?",
input_type: "radio",
checked: false,
answers: [
"Never heard of it",
"Heard of it - never tried it before",
"Tried it once before",
"Buy it regularly",
"Curious to learn more",
],
},
{
question:
"How likely are you to recommend or discuss Zoey & Eva's Cookies with a friend?",
input_type: "radio",
checked: false,
answers: [
"Never heard of it",
"Heard of it - never tried it before",
"Tried it once before",
"Buy it regularly",
"Curious to learn more",
],
},
],
},
{
id: 2,
title: "Brand Questions",
questions: [
{
question: "How familiar are you with Zoey & Eva's Cookies?",
input_type: "radio",
checked: false,
answers: [
"Never heard of it",
"Heard of it - never tried it before",
"Tried it once before",
"Buy it regularly",
"Curious to learn more",
],
},
{
question:
"How likely are you to recommend or talk with friends about Zoey & Eva's Cookies in the next 30 days?",
input_type: "radio",
checked: false,
answers: [
"Never heard of it",
"Heard of it - never tried it before",
"Tried it once before",
"Buy it regularly",
"Curious to learn more",
],
},
{
question:
"Which of the following words would you associate with Zoey & Eva's Cookies?",
input_type: "radio",
checked: false,
answers: [
"Never heard of it",
"Heard of it - never tried it before",
"Tried it once before",
"Buy it regularly",
"Curious to learn more",
],
},
],
},
]
Chris Bitoy
11/30/2021, 4:51 PMVictor Iris
11/30/2021, 4:56 PMChris Bitoy
11/30/2021, 5:16 PMChris Bitoy
11/30/2021, 5:17 PMChris Bitoy
11/30/2021, 5:19 PMChris Bitoy
11/30/2021, 5:19 PM