1
1
import { Router } from 'express' ;
2
- import { renderIndex } from '../views/index' ;
3
- import { get404Sketch } from '../views/404Page' ;
2
+ import sendHtml , { renderIndex } from '../views/index' ;
4
3
import { userExists } from '../controllers/user.controller' ;
5
4
import {
6
5
projectExists ,
@@ -27,40 +26,46 @@ router.get('/signup', (req, res) => {
27
26
return res . send ( renderIndex ( ) ) ;
28
27
} ) ;
29
28
30
- router . get ( '/projects/:project_id' , ( req , res ) => {
31
- projectExists ( req . params . project_id , ( exists ) =>
32
- exists ? res . send ( renderIndex ( ) ) : get404Sketch ( ( html ) => res . send ( html ) )
33
- ) ;
29
+ router . get ( '/projects/:project_id' , async ( req , res ) => {
30
+ const exists = await projectExists ( req . params . project_id ) ;
31
+ sendHtml ( req , res , exists ) ;
34
32
} ) ;
35
33
36
- router . get ( '/:username/sketches/:project_id/add-to-collection' , ( req , res ) => {
37
- projectForUserExists ( req . params . username , req . params . project_id , ( exists ) =>
38
- exists ? res . send ( renderIndex ( ) ) : get404Sketch ( ( html ) => res . send ( html ) )
39
- ) ;
40
- } ) ;
34
+ router . get (
35
+ '/:username/sketches/:project_id/add-to-collection' ,
36
+ async ( req , res ) => {
37
+ const exists = await projectForUserExists (
38
+ req . params . username ,
39
+ req . params . project_id
40
+ ) ;
41
+ sendHtml ( req , res , exists ) ;
42
+ }
43
+ ) ;
41
44
42
- router . get ( '/:username/sketches/:project_id' , ( req , res ) => {
43
- projectForUserExists ( req . params . username , req . params . project_id , ( exists ) =>
44
- exists ? res . send ( renderIndex ( ) ) : get404Sketch ( ( html ) => res . send ( html ) )
45
+ router . get ( '/:username/sketches/:project_id' , async ( req , res ) => {
46
+ const exists = await projectForUserExists (
47
+ req . params . username ,
48
+ req . params . project_id
45
49
) ;
50
+ sendHtml ( req , res , exists ) ;
46
51
} ) ;
47
52
48
- router . get ( '/:username/sketches' , ( req , res ) => {
49
- userExists ( req . params . username , ( exists ) =>
50
- exists ? res . send ( renderIndex ( ) ) : get404Sketch ( ( html ) => res . send ( html ) )
51
- ) ;
53
+ router . get ( '/:username/sketches' , async ( req , res ) => {
54
+ const exists = await userExists ( req . params . username ) ;
55
+ sendHtml ( req , res , exists ) ;
52
56
} ) ;
53
57
54
- router . get ( '/:username/full/:project_id' , ( req , res ) => {
55
- projectForUserExists ( req . params . username , req . params . project_id , ( exists ) =>
56
- exists ? res . send ( renderIndex ( ) ) : get404Sketch ( ( html ) => res . send ( html ) )
58
+ router . get ( '/:username/full/:project_id' , async ( req , res ) => {
59
+ const exists = await projectForUserExists (
60
+ req . params . username ,
61
+ req . params . project_id
57
62
) ;
63
+ sendHtml ( req , res , exists ) ;
58
64
} ) ;
59
65
60
- router . get ( '/full/:project_id' , ( req , res ) => {
61
- projectExists ( req . params . project_id , ( exists ) =>
62
- exists ? res . send ( renderIndex ( ) ) : get404Sketch ( ( html ) => res . send ( html ) )
63
- ) ;
66
+ router . get ( '/full/:project_id' , async ( req , res ) => {
67
+ const exists = await projectExists ( req . params . project_id ) ;
68
+ sendHtml ( req , res , exists ) ;
64
69
} ) ;
65
70
66
71
router . get ( '/login' , ( req , res ) => {
@@ -98,15 +103,11 @@ router.get('/assets', (req, res) => {
98
103
}
99
104
} ) ;
100
105
101
- router . get ( '/:username/assets' , ( req , res ) => {
102
- userExists ( req . params . username , ( exists ) => {
103
- const isLoggedInUser =
104
- req . user && req . user . username === req . params . username ;
105
- const canAccess = exists && isLoggedInUser ;
106
- return canAccess
107
- ? res . send ( renderIndex ( ) )
108
- : get404Sketch ( ( html ) => res . send ( html ) ) ;
109
- } ) ;
106
+ router . get ( '/:username/assets' , async ( req , res ) => {
107
+ const exists = await userExists ( req . params . username ) ;
108
+ const isLoggedInUser = req . user && req . user . username === req . params . username ;
109
+ const canAccess = exists && isLoggedInUser ;
110
+ sendHtml ( req , res , canAccess ) ;
110
111
} ) ;
111
112
112
113
router . get ( '/account' , ( req , res ) => {
@@ -121,16 +122,17 @@ router.get('/about', (req, res) => {
121
122
res . send ( renderIndex ( ) ) ;
122
123
} ) ;
123
124
124
- router . get ( '/:username/collections/:id' , ( req , res ) => {
125
- collectionForUserExists ( req . params . username , req . params . id , ( exists ) =>
126
- exists ? res . send ( renderIndex ( ) ) : get404Sketch ( ( html ) => res . send ( html ) )
125
+ router . get ( '/:username/collections/:id' , async ( req , res ) => {
126
+ const exists = await collectionForUserExists (
127
+ req . params . username ,
128
+ req . params . id
127
129
) ;
130
+ sendHtml ( req , res , exists ) ;
128
131
} ) ;
129
132
130
- router . get ( '/:username/collections' , ( req , res ) => {
131
- userExists ( req . params . username , ( exists ) =>
132
- exists ? res . send ( renderIndex ( ) ) : get404Sketch ( ( html ) => res . send ( html ) )
133
- ) ;
133
+ router . get ( '/:username/collections' , async ( req , res ) => {
134
+ const exists = await userExists ( req . params . username ) ;
135
+ sendHtml ( req , res , exists ) ;
134
136
} ) ;
135
137
136
138
router . get ( '/privacy-policy' , ( req , res ) => {
0 commit comments