Last active
August 29, 2022 07:57
-
-
Save edwardsmoses/382519dc7d0628d77d33f26801265765 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2017, Google, Inc. | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
'use strict'; | |
const config = require("../config"); | |
// Load the @google-cloud/datastore module | |
const { Datastore } = require("@google-cloud/datastore"); | |
const ds = new Datastore({ | |
projectId: config.get('GCLOUD_PROJECT') | |
}) | |
const kind = "Question" | |
function create({ quiz, author, title, answer1, answer2, answer3, answer4, correctAnswer }) { | |
const key = ds.key(kind); | |
const entity = { | |
key, | |
data: [ | |
{ name: 'quiz', value: quiz }, | |
{ name: 'author', value: author }, | |
{ name: 'title', value: title }, | |
{ name: 'answer1', value: answer1 }, | |
{ name: 'answer2', value: answer2 }, | |
{ name: 'answer3', value: answer3 }, | |
{ name: 'answer4', value: answer4 }, | |
{ name: 'correctAnswer', value: correctAnswer }, | |
] | |
} | |
return ds.save(entity); | |
} | |
// Lists all questions in a Quiz (defaults to 'gcp'). | |
// Returns a promise | |
// [START list] | |
function list(quiz = 'gcp') { | |
const query = ds.createQuery(kind); | |
const queryPromise = ds.runQuery(query); | |
return Promise.resolve( | |
queryPromise.then((response) => { | |
const [data, { moreResults }] = response; | |
const questions = data.map((value) => { | |
return { | |
id: value[ds.KEY].id, | |
title: value.title, | |
answer1: value.answer1, | |
answer2: value.answer2, | |
answer3: value.answer3, | |
answer4: value.answer4, | |
} | |
}); | |
return { | |
questions: questions, | |
nextPageToken: moreResults, | |
} | |
}) | |
); | |
} | |
// [END list] | |
// [START exports] | |
module.exports = { | |
create, | |
list | |
}; | |
// [END exports] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment