File tree Expand file tree Collapse file tree 9 files changed +282
-0
lines changed
2_crud_api/18_securing_routes Expand file tree Collapse file tree 9 files changed +282
-0
lines changed Original file line number Diff line number Diff line change 1+ # package directories
2+ node_modules
3+ jspm_packages
4+
5+ # Serverless directories
6+ .serverless
Original file line number Diff line number Diff line change 1+ const Sequelize = require ( "sequelize" ) ;
2+
3+ // elephantsql endpoint
4+ const sequelize = new Sequelize (
5+ "postgres://pxdowepn:[email protected] :5432/pxdowepn" 6+ ) ;
7+
8+ const todo = require ( "./models/todo" ) ( sequelize , Sequelize ) ;
9+
10+ const db = {
11+ Sequelize,
12+ sequelize,
13+ todo
14+ } ;
15+
16+ db . sequelize . sync ( /*{ force: true }*/ ) ;
17+
18+ module . exports = db ;
Original file line number Diff line number Diff line change 1+ const db = require ( "../db.js" ) ;
2+
3+ module . exports . createTodo = ( event , context , callback ) => {
4+ const body = JSON . parse ( event . body ) ;
5+ const { task } = body ;
6+ if ( ! task ) {
7+ return callback ( null , {
8+ statusCode : 500 ,
9+ body : JSON . stringify ( {
10+ error : 'The property "task" is required.'
11+ } )
12+ } ) ;
13+ }
14+ db . todo
15+ . create ( {
16+ task : body . task
17+ } )
18+ . then ( todo => {
19+ return callback ( null , {
20+ statusCode : 200 ,
21+ body : JSON . stringify ( {
22+ todo : todo
23+ } )
24+ } ) ;
25+ } )
26+ . catch ( error => {
27+ return callback ( null , {
28+ statusCode : 500 ,
29+ body : JSON . stringify ( {
30+ error : "There was an error creating your todo."
31+ } )
32+ } ) ;
33+ } ) ;
34+ } ;
Original file line number Diff line number Diff line change 1+ const db = require ( "../db.js" ) ;
2+
3+ module . exports . deleteTodo = ( event , context , callback ) => {
4+ const todo_id = event . pathParameters . id ;
5+ db . todo
6+ . destroy ( { where : { id : todo_id } } )
7+ . then ( num_deleted => {
8+ return callback ( null , {
9+ statusCode : 200 ,
10+ body : JSON . stringify ( {
11+ num_deleted : num_deleted
12+ } )
13+ } ) ;
14+ } )
15+ . catch ( error => {
16+ callback ( null , {
17+ statusCode : 500 ,
18+ body : JSON . stringify ( {
19+ error : `There was an error deleting your todo with id: ${ todo_id } .`
20+ } )
21+ } ) ;
22+ } ) ;
23+ } ;
Original file line number Diff line number Diff line change 1+ "use strict" ;
2+
3+ const db = require ( "../db.js" ) ;
4+
5+ module . exports . getTodo = ( event , context , callback ) => {
6+ const todo_id = event . pathParameters . id ;
7+ db . todo
8+ . findOne ( {
9+ where : { id : todo_id } ,
10+ attributes : [ "id" , "task" , "completed" ]
11+ } )
12+ . then ( todo => {
13+ const response = {
14+ statusCode : 200 ,
15+ body : JSON . stringify ( {
16+ todo : todo
17+ } )
18+ } ;
19+
20+ callback ( null , response ) ;
21+ } )
22+ . catch ( error => {
23+ callback ( null , {
24+ statusCode : 500 ,
25+ body : JSON . stringify ( {
26+ error : `There was an error fetching your todo with id: ${ todo_id } .`
27+ } )
28+ } ) ;
29+ } ) ;
30+ } ;
31+
32+ module . exports . listTodos = ( event , context , callback ) => {
33+ db . todo
34+ . findAll ( {
35+ attributes : [ "id" , "task" , "completed" ]
36+ } )
37+ . then ( todos => {
38+ const response = {
39+ statusCode : 200 ,
40+ body : JSON . stringify ( {
41+ todos : todos
42+ } )
43+ } ;
44+
45+ callback ( null , response ) ;
46+ } )
47+ . catch ( error => {
48+ callback ( null , {
49+ statusCode : 500 ,
50+ body : JSON . stringify ( {
51+ error : `There was an error fetching your todos.`
52+ } )
53+ } ) ;
54+ } ) ;
55+ } ;
Original file line number Diff line number Diff line change 1+ const db = require ( "../db.js" ) ;
2+
3+ module . exports . updateTodo = ( event , context , callback ) => {
4+ const todo_id = event . pathParameters . id ;
5+
6+ const body = JSON . parse ( event . body ) ;
7+
8+ db . todo
9+ . update ( body , {
10+ where : { id : todo_id } ,
11+ returning : true
12+ } )
13+ . then ( resArr => {
14+ console . log ( resArr ) ;
15+ const [ rowsAffected , todoArr ] = resArr ;
16+ console . log (
17+ `${ rowsAffected } row(s) were updated with this obj: ${ JSON . stringify (
18+ body
19+ ) } `
20+ ) ;
21+ return callback ( null , {
22+ statusCode : 200 ,
23+ body : JSON . stringify ( {
24+ todo : todoArr [ 0 ]
25+ } )
26+ } ) ;
27+ } )
28+ . catch ( error => {
29+ return callback ( null , {
30+ statusCode : 500 ,
31+ body : JSON . stringify ( {
32+ error : `There was an error updating todo id ${ todo_id } `
33+ } )
34+ } ) ;
35+ } ) ;
36+ } ;
Original file line number Diff line number Diff line change 1+ module . exports = ( sequelize , DataTypes ) => {
2+ return sequelize . define (
3+ "todo" ,
4+ {
5+ id : {
6+ type : DataTypes . INTEGER ,
7+ allowNull : false ,
8+ autoIncrement : true ,
9+ primaryKey : true
10+ } ,
11+ task : {
12+ type : DataTypes . STRING
13+ } ,
14+ completed : {
15+ type : DataTypes . BOOLEAN ,
16+ defaultValue : false
17+ } ,
18+ created_at : {
19+ type : DataTypes . DATE ,
20+ defaultValue : new Date ( ) ,
21+ allowNull : false
22+ } ,
23+ updated_at : {
24+ type : DataTypes . DATE ,
25+ defaultValue : new Date ( ) ,
26+ allowNull : false
27+ } ,
28+ deleted_at : {
29+ type : DataTypes . DATE
30+ }
31+ } ,
32+ {
33+ paranoid : true ,
34+ underscored : true
35+ }
36+ ) ;
37+ } ;
Original file line number Diff line number Diff line change 1+ {
2+ "name" : " securing_routes" ,
3+ "version" : " 1.0.0" ,
4+ "description" : " " ,
5+ "main" : " index.js" ,
6+ "scripts" : {
7+ "test" : " echo \" Error: no test specified\" && exit 1"
8+ },
9+ "keywords" : [],
10+ "author" : " " ,
11+ "license" : " ISC" ,
12+ "devDependencies" : {
13+ "serverless-offline" : " ^3.15.3"
14+ },
15+ "dependencies" : {
16+ "pg" : " 6.4.2" ,
17+ "sequelize" : " 3.30.4"
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ service : crud-api
2+
3+ plugins :
4+ - serverless-offline
5+
6+ provider :
7+ name : aws
8+ runtime : nodejs6.10
9+ stage : dev
10+ region : us-west-2
11+ timeout : 300
12+
13+
14+ package :
15+ include :
16+ - features/**
17+ - models/**
18+
19+
20+ functions :
21+ listTodos :
22+ handler : features/read.listTodos
23+ events :
24+ - http :
25+ path : todos
26+ method : get
27+
28+ getTodo :
29+ handler : features/read.getTodo
30+ events :
31+ - http :
32+ path : todo/{id}
33+ method : get
34+
35+ createTodo :
36+ handler : features/create.createTodo
37+ events :
38+ - http :
39+ path : todo
40+ method : post
41+
42+ deleteTodo :
43+ handler : features/delete.deleteTodo
44+ events :
45+ - http :
46+ path : todo/{id}
47+ method : delete
48+
49+ updateTodo :
50+ handler : features/update.updateTodo
51+ events :
52+ - http :
53+ path : todo/{id}
54+ method : patch
You can’t perform that action at this time.
0 commit comments