@@ -20,26 +20,27 @@ export {
20
20
apiGetProjectsForUser
21
21
} from './project.controller/getProjectsForUser' ;
22
22
23
- export function updateProject ( req , res ) {
24
- Project . findById ( req . params . project_id , ( findProjectErr , project ) => {
25
- if ( ! project . user . equals ( req . user . _id ) ) {
26
- res . status ( 403 ) . send ( {
27
- success : false ,
28
- message : 'Session does not match owner of project.'
29
- } ) ;
30
- return ;
31
- }
32
- if (
33
- req . body . updatedAt &&
34
- isAfter ( new Date ( project . updatedAt ) , new Date ( req . body . updatedAt ) )
35
- ) {
36
- res . status ( 409 ) . send ( {
37
- success : false ,
38
- message : 'Attempted to save stale version of project.'
39
- } ) ;
40
- return ;
41
- }
42
- Project . findByIdAndUpdate (
23
+ export async function updateProject ( req , res ) {
24
+ const project = await Project . findById ( req . params . project_id ) . exec ( ) ;
25
+ if ( ! project . user . equals ( req . user . _id ) ) {
26
+ res . status ( 403 ) . send ( {
27
+ success : false ,
28
+ message : 'Session does not match owner of project.'
29
+ } ) ;
30
+ return ;
31
+ }
32
+ if (
33
+ req . body . updatedAt &&
34
+ isAfter ( new Date ( project . updatedAt ) , new Date ( req . body . updatedAt ) )
35
+ ) {
36
+ res . status ( 409 ) . send ( {
37
+ success : false ,
38
+ message : 'Attempted to save stale version of project.'
39
+ } ) ;
40
+ return ;
41
+ }
42
+ try {
43
+ const updatedProject = await Project . findByIdAndUpdate (
43
44
req . params . project_id ,
44
45
{
45
46
$set : req . body
@@ -50,119 +51,90 @@ export function updateProject(req, res) {
50
51
}
51
52
)
52
53
. populate ( 'user' , 'username' )
53
- . exec ( ( updateProjectErr , updatedProject ) => {
54
- if ( updateProjectErr ) {
55
- console . log ( updateProjectErr ) ;
56
- res . status ( 400 ) . json ( { success : false } ) ;
57
- return ;
58
- }
59
- if (
60
- req . body . files &&
61
- updatedProject . files . length !== req . body . files . length
62
- ) {
63
- const oldFileIds = updatedProject . files . map ( ( file ) => file . id ) ;
64
- const newFileIds = req . body . files . map ( ( file ) => file . id ) ;
65
- const staleIds = oldFileIds . filter (
66
- ( id ) => newFileIds . indexOf ( id ) === - 1
67
- ) ;
68
- staleIds . forEach ( ( staleId ) => {
69
- updatedProject . files . id ( staleId ) . remove ( ) ;
70
- } ) ;
71
- updatedProject . save ( ( innerErr , savedProject ) => {
72
- if ( innerErr ) {
73
- console . log ( innerErr ) ;
74
- res . status ( 400 ) . json ( { success : false } ) ;
75
- return ;
76
- }
77
- res . json ( savedProject ) ;
78
- } ) ;
79
- } else {
80
- res . json ( updatedProject ) ;
81
- }
54
+ . exec ( ) ;
55
+ if (
56
+ req . body . files &&
57
+ updatedProject . files . length !== req . body . files . length
58
+ ) {
59
+ const oldFileIds = updatedProject . files . map ( ( file ) => file . id ) ;
60
+ const newFileIds = req . body . files . map ( ( file ) => file . id ) ;
61
+ const staleIds = oldFileIds . filter ( ( id ) => newFileIds . indexOf ( id ) === - 1 ) ;
62
+ staleIds . forEach ( ( staleId ) => {
63
+ updatedProject . files . id ( staleId ) . remove ( ) ;
82
64
} ) ;
83
- } ) ;
65
+ const savedProject = await updatedProject . save ( ) ;
66
+ res . json ( savedProject ) ;
67
+ } else {
68
+ res . json ( updatedProject ) ;
69
+ }
70
+ } catch ( error ) {
71
+ res . status ( 400 ) . json ( { success : false } ) ;
72
+ }
84
73
}
85
74
86
- export function getProject ( req , res ) {
75
+ export async function getProject ( req , res ) {
87
76
const { project_id : projectId , username } = req . params ;
88
- User . findByUsername ( username , ( err , user ) => { // eslint-disable-line
89
- if ( ! user ) {
90
- return res
91
- . status ( 404 )
92
- . send ( { message : 'Project with that username does not exist' } ) ;
93
- }
94
- Project . findOne ( {
95
- user : user . _id ,
96
- $or : [ { _id : projectId } , { slug : projectId } ]
97
- } )
98
- . populate ( 'user' , 'username' )
99
- . exec ( ( err , project ) => { // eslint-disable-line
100
- if ( err ) {
101
- console . log ( err ) ;
102
- return res
103
- . status ( 404 )
104
- . send ( { message : 'Project with that id does not exist' } ) ;
105
- }
106
- return res . json ( project ) ;
107
- } ) ;
108
- } ) ;
77
+ const user = await User . findByUsername ( username ) ;
78
+ if ( ! user ) {
79
+ return res
80
+ . status ( 404 )
81
+ . send ( { message : 'User with that username does not exist' } ) ;
82
+ }
83
+ const project = await Project . findOne ( {
84
+ user : user . _id ,
85
+ $or : [ { _id : projectId } , { slug : projectId } ]
86
+ } ) . populate ( 'user' , 'username' ) ;
87
+ if ( ! project ) {
88
+ return res
89
+ . status ( 404 )
90
+ . send ( { message : 'Project with that id does not exist' } ) ;
91
+ }
92
+ return res . json ( project ) ;
109
93
}
110
94
111
95
export function getProjectsForUserId ( userId ) {
112
- return new Promise ( ( resolve , reject ) => {
113
- Project . find ( { user : userId } )
114
- . sort ( '-createdAt' )
115
- . select ( 'name files id createdAt updatedAt' )
116
- . exec ( ( err , projects ) => {
117
- if ( err ) {
118
- console . log ( err ) ;
119
- }
120
- resolve ( projects ) ;
121
- } ) ;
122
- } ) ;
96
+ return Project . find ( { user : userId } )
97
+ . sort ( '-createdAt' )
98
+ . select ( 'name files id createdAt updatedAt' )
99
+ . exec ( ) ;
123
100
}
124
101
125
- export function getProjectAsset ( req , res ) {
102
+ export async function getProjectAsset ( req , res ) {
126
103
const projectId = req . params . project_id ;
127
- Project . findOne ( { $or : [ { _id : projectId } , { slug : projectId } ] } )
104
+ const project = await Project . findOne ( {
105
+ $or : [ { _id : projectId } , { slug : projectId } ]
106
+ } )
128
107
. populate ( 'user' , 'username' )
129
- . exec ( async ( err , project ) => { // eslint-disable-line
130
- if ( err ) {
131
- return res
132
- . status ( 404 )
133
- . send ( { message : 'Project with that id does not exist' } ) ;
134
- }
135
- if ( ! project ) {
136
- return res
137
- . status ( 404 )
138
- . send ( { message : 'Project with that id does not exist' } ) ;
139
- }
108
+ . exec ( ) ;
109
+ if ( ! project ) {
110
+ return res
111
+ . status ( 404 )
112
+ . send ( { message : 'Project with that id does not exist' } ) ;
113
+ }
140
114
141
- const filePath = req . params [ 0 ] ;
142
- const resolvedFile = resolvePathToFile ( filePath , project . files ) ;
143
- if ( ! resolvedFile ) {
144
- return res . status ( 404 ) . send ( { message : 'Asset does not exist' } ) ;
145
- }
146
- if ( ! resolvedFile . url ) {
147
- return res . send ( resolvedFile . content ) ;
148
- }
115
+ const filePath = req . params [ 0 ] ;
116
+ const resolvedFile = resolvePathToFile ( filePath , project . files ) ;
117
+ if ( ! resolvedFile ) {
118
+ return res . status ( 404 ) . send ( { message : 'Asset does not exist' } ) ;
119
+ }
120
+ if ( ! resolvedFile . url ) {
121
+ return res . send ( resolvedFile . content ) ;
122
+ }
149
123
150
- try {
151
- const { data } = await axios . get ( resolvedFile . url , {
152
- responseType : 'arraybuffer'
153
- } ) ;
154
- res . send ( data ) ;
155
- } catch ( error ) {
156
- res . status ( 404 ) . send ( { message : 'Asset does not exist' } ) ;
157
- }
124
+ try {
125
+ const { data } = await axios . get ( resolvedFile . url , {
126
+ responseType : 'arraybuffer'
158
127
} ) ;
128
+ return res . send ( data ) ;
129
+ } catch ( error ) {
130
+ return res . status ( 404 ) . send ( { message : 'Asset does not exist' } ) ;
131
+ }
159
132
}
160
133
161
- export function getProjects ( req , res ) {
134
+ export async function getProjects ( req , res ) {
162
135
if ( req . user ) {
163
- getProjectsForUserId ( req . user . _id ) . then ( ( projects ) => {
164
- res . json ( projects ) ;
165
- } ) ;
136
+ const projects = await getProjectsForUserId ( req . user . _id ) ;
137
+ res . json ( projects ) ;
166
138
} else {
167
139
// could just move this to client side
168
140
res . json ( [ ] ) ;
@@ -264,9 +236,11 @@ async function buildZip(project, req, res) {
264
236
}
265
237
}
266
238
267
- export function downloadProjectAsZip ( req , res ) {
268
- Project . findById ( req . params . project_id , ( err , project ) => {
269
- // save project to some path
270
- buildZip ( project , req , res ) ;
271
- } ) ;
239
+ export async function downloadProjectAsZip ( req , res ) {
240
+ const project = await Project . findById ( req . params . project_id ) ;
241
+ if ( ! project ) {
242
+ res . status ( 404 ) . send ( { message : 'Project with that id does not exist' } ) ;
243
+ }
244
+ // save project to some path
245
+ buildZip ( project , req , res ) ;
272
246
}
0 commit comments