|
1 | | -import {} from 'dotenv/config'; |
2 | | -import pool from '../db/db'; |
3 | | -import joi from 'joi'; |
4 | | -import dateFormat from 'date-fns/format'; |
| 1 | +import {} from 'dotenv/config' |
| 2 | +import joi from 'joi' |
| 3 | +import dateFormat from 'date-fns/format' |
5 | 4 |
|
6 | | -import { User } from '../models/User'; |
7 | | -import { Note } from '../models/Note'; |
| 5 | +import { User } from '../models/User' |
| 6 | +import { Note } from '../models/Note' |
8 | 7 |
|
9 | 8 | const noteSchema = joi.object({ |
10 | 9 | id: joi.number().integer(), |
11 | 10 | userId: joi.number().integer().required(), |
12 | 11 | title: joi.string().required(), |
13 | 12 | content: joi.string().required(), |
14 | 13 | ipAddress: joi.string(), |
15 | | -}); |
| 14 | +}) |
16 | 15 |
|
17 | 16 | class NoteController { |
18 | 17 | async index(ctx) { |
| 18 | + const query = ctx.query |
| 19 | + |
19 | 20 | //Attach logged in user |
20 | | - const user = new User(ctx.state.user[0]); |
21 | | - ctx.query.userId = user.id; |
| 21 | + const user = new User(ctx.state.user) |
| 22 | + query.userId = user.id |
22 | 23 |
|
23 | 24 | //Init a new note object |
24 | | - const note = new Note(); |
| 25 | + const note = new Note() |
25 | 26 |
|
26 | 27 | //Let's check that the sort options were set. Sort can be empty |
27 | | - if (!ctx.query.order || !ctx.query.page || !ctx.query.limit) { |
28 | | - ctx.throw(400, 'INVALID_ROUTE_OPTIONS'); |
| 28 | + if (!query.order || !query.page || !query.limit) { |
| 29 | + ctx.throw(400, 'INVALID_ROUTE_OPTIONS') |
29 | 30 | } |
30 | 31 |
|
| 32 | + //Get paginated list of notes |
31 | 33 | try { |
32 | | - let result = await note.all(ctx.query); |
33 | | - ctx.body = result; |
| 34 | + let result = await note.all(query) |
| 35 | + ctx.body = result |
34 | 36 | } catch (error) { |
35 | | - console.log(error); |
36 | | - ctx.throw(400, 'INVALID_DATA'); |
| 37 | + console.log(error) |
| 38 | + ctx.throw(400, 'INVALID_DATA' + error) |
37 | 39 | } |
38 | 40 | } |
39 | 41 |
|
40 | 42 | async show(ctx) { |
41 | | - //Make sure they've chosen an id to show |
42 | | - if (!ctx.params.id) ctx.throw(400, 'INVALID_DATA'); |
| 43 | + const params = ctx.params |
| 44 | + if (!params.id) ctx.throw(400, 'INVALID_DATA') |
43 | 45 |
|
44 | 46 | //Initialize note |
45 | | - const note = new Note(); |
| 47 | + const note = new Note() |
46 | 48 |
|
47 | 49 | try { |
48 | 50 | //Find and show note |
49 | | - await note.find(ctx.params); |
50 | | - ctx.body = note; |
| 51 | + await note.find(params.id) |
| 52 | + ctx.body = note |
51 | 53 | } catch (error) { |
52 | | - console.log(error); |
53 | | - ctx.throw(400, 'INVALID_DATA'); |
| 54 | + console.log(error) |
| 55 | + ctx.throw(400, 'INVALID_DATA') |
54 | 56 | } |
55 | 57 | } |
56 | 58 |
|
57 | 59 | async create(ctx) { |
| 60 | + const request = ctx.request.body |
| 61 | + |
58 | 62 | //Attach logged in user |
59 | | - const user = new User(ctx.state.user[0]); |
60 | | - ctx.request.body.userId = user.id; |
| 63 | + const user = new User(ctx.state.user) |
| 64 | + request.userId = user.id |
61 | 65 |
|
62 | 66 | //Add ip |
63 | | - ctx.request.body.ipAddress = ctx.ip; |
| 67 | + request.ipAddress = ctx.ip |
64 | 68 |
|
65 | 69 | //Create a new note object using the request params |
66 | | - const note = new Note(ctx.request.body); |
| 70 | + const note = new Note(request) |
67 | 71 |
|
68 | 72 | //Validate the newly created note |
69 | | - const validator = joi.validate(note, noteSchema); |
70 | | - if (validator.error) ctx.throw(400, validator.error.details[0].message); |
| 73 | + const validator = joi.validate(note, noteSchema) |
| 74 | + if (validator.error) ctx.throw(400, validator.error.details[0].message) |
71 | 75 |
|
72 | 76 | try { |
73 | | - let result = await note.store(); |
74 | | - ctx.body = { message: 'SUCCESS', id: result.insertId }; |
| 77 | + let result = await note.store() |
| 78 | + ctx.body = { message: 'SUCCESS', id: result } |
75 | 79 | } catch (error) { |
76 | | - console.log(error); |
77 | | - ctx.throw(400, 'INVALID_DATA'); |
| 80 | + console.log(error) |
| 81 | + ctx.throw(400, 'INVALID_DATA') |
78 | 82 | } |
79 | 83 | } |
80 | 84 |
|
81 | 85 | async update(ctx) { |
| 86 | + const params = ctx.params |
| 87 | + const request = ctx.request.body |
| 88 | + |
82 | 89 | //Make sure they've specified a note |
83 | | - if (!ctx.params.id) ctx.throw(400, 'INVALID_DATA'); |
| 90 | + if (!params.id) ctx.throw(400, 'INVALID_DATA') |
84 | 91 |
|
85 | | - //Find that note |
86 | | - const note = new Note(); |
87 | | - await note.find(ctx.params); |
88 | | - if (!note) ctx.throw(400, 'INVALID_DATA'); |
| 92 | + //Find and set that note |
| 93 | + const note = new Note() |
| 94 | + await note.find(params.id) |
| 95 | + if (!note) ctx.throw(400, 'INVALID_DATA') |
89 | 96 |
|
90 | 97 | //Grab the user //If it's not their note - error out |
91 | | - const user = new User(ctx.state.user[0]); |
92 | | - if (note.userId !== user.id) ctx.throw(400, 'INVALID_DATA'); |
| 98 | + const user = new User(ctx.state.user) |
| 99 | + if (note.userId !== user.id) ctx.throw(400, 'INVALID_DATA') |
93 | 100 |
|
94 | 101 | //Add the updated date value |
95 | | - note.updatedAt = dateFormat(new Date(), 'YYYY-MM-DD HH:mm:ss'); |
| 102 | + note.updatedAt = dateFormat(new Date(), 'YYYY-MM-DD HH:mm:ss') |
96 | 103 |
|
97 | 104 | //Add the ip |
98 | | - ctx.request.body.ipAddress = ctx.ip; |
| 105 | + request.ipAddress = ctx.ip |
99 | 106 |
|
100 | 107 | //Replace the note data with the new updated note data |
101 | 108 | Object.keys(ctx.request.body).forEach(function(parameter, index) { |
102 | | - note[parameter] = ctx.request.body[parameter]; |
103 | | - }); |
| 109 | + note[parameter] = request[parameter] |
| 110 | + }) |
104 | 111 |
|
105 | 112 | try { |
106 | | - await note.save(); |
107 | | - ctx.body = { message: 'SUCCESS' }; |
| 113 | + await note.save() |
| 114 | + ctx.body = { message: 'SUCCESS' } |
108 | 115 | } catch (error) { |
109 | | - console.log(error); |
110 | | - ctx.throw(400, 'INVALID_DATA'); |
| 116 | + console.log(error) |
| 117 | + ctx.throw(400, 'INVALID_DATA') |
111 | 118 | } |
112 | 119 | } |
113 | 120 |
|
114 | 121 | async delete(ctx) { |
115 | | - if (!ctx.params.id) ctx.throw(400, 'INVALID_DATA'); |
| 122 | + const params = ctx.params |
| 123 | + if (!params.id) ctx.throw(400, 'INVALID_DATA') |
116 | 124 |
|
117 | 125 | //Find that note |
118 | | - const note = new Note(); |
119 | | - await note.find(ctx.params); |
120 | | - if (!note) ctx.throw(400, 'INVALID_DATA'); |
| 126 | + const note = new Note() |
| 127 | + await note.find(params.id) |
| 128 | + if (!note) ctx.throw(400, 'INVALID_DATA') |
121 | 129 |
|
122 | 130 | //Grab the user //If it's not their note - error out |
123 | | - const user = new User(ctx.state.user[0]); |
124 | | - if (note.userId !== user.id) ctx.throw(400, 'INVALID_DATA'); |
| 131 | + const user = new User(ctx.state.user) |
| 132 | + if (note.userId !== user.id) ctx.throw(400, 'INVALID_DATA') |
125 | 133 |
|
126 | 134 | try { |
127 | | - await note.destroy(); |
128 | | - ctx.body = { message: 'SUCCESS' }; |
| 135 | + await note.destroy() |
| 136 | + ctx.body = { message: 'SUCCESS' } |
129 | 137 | } catch (error) { |
130 | | - console.log(error); |
131 | | - ctx.throw(400, 'INVALID_DATA'); |
| 138 | + console.log(error) |
| 139 | + ctx.throw(400, 'INVALID_DATA') |
132 | 140 | } |
133 | 141 | } |
134 | 142 | } |
135 | 143 |
|
136 | | -export default NoteController; |
| 144 | +export default NoteController |
0 commit comments