-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecipes.js
95 lines (79 loc) · 2.9 KB
/
recipes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import * as dynamoDB from './lib/dynamodb-lib';
import { success, failure } from './lib/response-lib';
import uuid from 'uuid';
import _kebabCase from 'lodash/kebabCase'
import _forEach from 'lodash/forEach'
export async function get(event, context, callback) {
const params = {
TableName: "LissnerRecipes"
}
try {
const result = await dynamoDB.call('scan', params);
callback(null, success(result.Items));
} catch (err) {
console.error(err);
callback(null, failure({ status: false }));
}
}
export async function create(event, context, callback) {
const data = JSON.parse(event.body);
_forEach(data.ingredients, (ingredient) => ingredient.title = ingredient.title ? ingredient.title : null)
_forEach(data.directions, (direction) => direction.title = direction.title ? direction.title : null)
const Item = {
Id: uuid.v4(),
recipeUrl: `/${_kebabCase(data.title)}`,
author: data.author || null,
description: data.description || null,
note: data.note || null,
title: data.title || null,
cookTime: data.cookTime || null,
serves: data.serves || null,
ingredients: data.ingredients || null,
directions: data.directions || null,
tags: data.tags || null,
createdBy: event.requestContext.identity.cognitoIdentityId,
};
const params = {
TableName: "LissnerRecipes",
Item,
}
try {
const result = await dynamoDB.call('put', params);
callback(null, success({ success: true, data: Item }));
} catch (err) {
console.error(err);
callback(null, failure({ status: false }));
}
}
export async function update(event, context, callback) {
const data = JSON.parse(event.body);
_forEach(data.ingredients, (ingredient) => ingredient.title = ingredient.title ? ingredient.title : null)
_forEach(data.directions, (direction) => direction.title = direction.title ? direction.title : null)
const params = {
TableName: "LissnerRecipes",
Key: {
Id: data.Id,
},
UpdateExpression: "SET author = :author, description = :description, note = :note, title = :title, cookTime = :cookTime, serves = :serves, ingredients = :ingredients, directions = :directions, tags = :tags, recipeUrl = :recipeUrl, comments = :comments",
ExpressionAttributeValues: {
":author": data.author || null,
":description": data.description || null,
":note": data.note || null,
":title": data.title || null,
":cookTime": data.cookTime || null,
":serves": data.serves || null,
":ingredients": data.ingredients || null,
":directions": data.directions || null,
":tags": data.tags || null,
":recipeUrl": `/${_kebabCase(data.title)}` || null,
":comments": data.comments || null,
}
}
try {
const result = await dynamoDB.call('update', params);
callback(null, success({ success: true, data }));
} catch (err) {
console.error(err);
callback(null, failure({ status: false }));
}
}