fix ID reference

This commit is contained in:
davidwells 2018-06-11 17:10:19 -07:00
parent 4f616b21a1
commit d4c3868e20
5 changed files with 43 additions and 52 deletions

View file

@ -1,27 +1,25 @@
'use strict';
import faunadb from 'faunadb' import faunadb from 'faunadb'
// const faunadb = require('faunadb');
const q = faunadb.query; const q = faunadb.query
const client = new faunadb.Client({ const client = new faunadb.Client({
secret: process.env.FAUNADB_SECRET secret: process.env.FAUNADB_SECRET
}); })
exports.handler = (event, context, callback) => { exports.handler = (event, context, callback) => {
const data = JSON.parse(event.body); const data = JSON.parse(event.body)
console.log("create todo", data); console.log("create todo", data)
return client.query(q.Create(q.Ref("classes/todos"), {data})) return client.query(q.Create(q.Ref("classes/todos"), {data}))
.then((response) => { .then((response) => {
console.log("success", response); console.log("success", response)
return callback(null, { return callback(null, {
statusCode: 200, statusCode: 200,
body: JSON.stringify(response) body: JSON.stringify(response)
}) })
}).catch((error) => { }).catch((error) => {
console.log("error", error); console.log("error", error)
return callback(null, { return callback(null, {
statusCode: 400, statusCode: 400,
body: JSON.stringify(error) body: JSON.stringify(error)
}) })
}) })
}; }

View file

@ -1,26 +1,25 @@
'use strict';
import faunadb from 'faunadb' import faunadb from 'faunadb'
// const faunadb = require('faunadb');
const q = faunadb.query; const q = faunadb.query
const client = new faunadb.Client({ const client = new faunadb.Client({
secret: process.env.FAUNADB_SECRET secret: process.env.FAUNADB_SECRET
}); })
exports.handler = (event, context, callback) => { exports.handler = (event, context, callback) => {
console.log("delete todo"); const id = event.path.replace(/\/\.netlify\/functions\/todos-delete\//, "")
return client.query(q.Delete(q.Ref("classes/todos/"+event.pathParameters.id))) console.log("delete todo", id)
return client.query(q.Delete(q.Ref(`classes/todos/${id}`)))
.then((response) => { .then((response) => {
console.log("success", response); console.log("success", response)
return callback(null, { return callback(null, {
statusCode: 200, statusCode: 200,
body: JSON.stringify(response) body: JSON.stringify(response)
}) })
}).catch((error) => { }).catch((error) => {
console.log("error", error); console.log("error", error)
return callback(null, { return callback(null, {
statusCode: 400, statusCode: 400,
body: JSON.stringify(error) body: JSON.stringify(error)
}) })
}) })
}; }

View file

@ -1,26 +1,24 @@
'use strict';
import faunadb from 'faunadb' import faunadb from 'faunadb'
// const faunadb = require('faunadb');
const q = faunadb.query; const q = faunadb.query
const client = new faunadb.Client({ const client = new faunadb.Client({
secret: process.env.FAUNADB_SECRET secret: process.env.FAUNADB_SECRET
}); })
exports.handler = (event, context, callback) => { exports.handler = (event, context, callback) => {
console.log("readAll todo"); console.log("readAll todos")
return client.query(q.Paginate(q.Match(q.Ref("indexes/all_todos")))) return client.query(q.Paginate(q.Match(q.Ref("indexes/all_todos"))))
.then((response) => { .then((response) => {
console.log("success", response); console.log("success", response)
return callback(null, { return callback(null, {
statusCode: 200, statusCode: 200,
body: JSON.stringify(response) body: JSON.stringify(response)
}) })
}).catch((error) => { }).catch((error) => {
console.log("error", error); console.log("error", error)
return callback(null, { return callback(null, {
statusCode: 400, statusCode: 400,
body: JSON.stringify(error) body: JSON.stringify(error)
}) })
}) })
}; }

View file

@ -1,28 +1,25 @@
'use strict';
import faunadb from 'faunadb' import faunadb from 'faunadb'
// const faunadb = require('faunadb');
const q = faunadb.query; const q = faunadb.query
const client = new faunadb.Client({ const client = new faunadb.Client({
secret: process.env.FAUNADB_SECRET secret: process.env.FAUNADB_SECRET
}); })
exports.handler = (event, context, callback) => { exports.handler = (event, context, callback) => {
console.log('event', event) const id = event.path.replace(/\/\.netlify\/functions\/todos-read-one\//, "")
console.log('context', context) console.log(`readOne todo ${id}`)
console.log(`readOne todo ${event.pathParameters.id}`); return client.query(q.Get(q.Ref(`classes/todos/${id}`)))
return client.query(q.Get(q.Ref("classes/todos/"+event.pathParameters.id)))
.then((response) => { .then((response) => {
console.log("success", response); console.log("success", response)
return callback(null, { return callback(null, {
statusCode: 200, statusCode: 200,
body: JSON.stringify(response) body: JSON.stringify(response)
}) })
}).catch((error) => { }).catch((error) => {
console.log("error", error); console.log("error", error)
return callback(null, { return callback(null, {
statusCode: 400, statusCode: 400,
body: JSON.stringify(error) body: JSON.stringify(error)
}) })
}) })
}; }

View file

@ -1,27 +1,26 @@
'use strict';
import faunadb from 'faunadb' import faunadb from 'faunadb'
// const faunadb = require('faunadb');
const q = faunadb.query; const q = faunadb.query
const client = new faunadb.Client({ const client = new faunadb.Client({
secret: process.env.FAUNADB_SECRET secret: process.env.FAUNADB_SECRET
}); })
exports.handler = (event, context, callback) => { exports.handler = (event, context, callback) => {
const data = JSON.parse(event.body); const data = JSON.parse(event.body)
console.log("update todo"); const id = event.path.replace(/\/\.netlify\/functions\/todos-update\//, "")
return client.query(q.Update(q.Ref("classes/todos/"+event.pathParameters.id), {data})) console.log("update todo", id)
return client.query(q.Update(q.Ref(`classes/todos/${id}`), {data}))
.then((response) => { .then((response) => {
console.log("success", response); console.log("success", response)
return callback(null, { return callback(null, {
statusCode: 200, statusCode: 200,
body: JSON.stringify(response) body: JSON.stringify(response)
}) })
}).catch((error) => { }).catch((error) => {
console.log("error", error); console.log("error", error)
return callback(null, { return callback(null, {
statusCode: 400, statusCode: 400,
body: JSON.stringify(error) body: JSON.stringify(error)
}) })
}) })
}; }