11import { Router } from 'express' ;
2- import { renderIndex } from '../views/index' ;
3- import { get404Sketch } from '../views/404Page' ;
2+ import sendHtml , { renderIndex } from '../views/index' ;
43import { userExists } from '../controllers/user.controller' ;
54import {
65 projectExists ,
@@ -27,40 +26,46 @@ router.get('/signup', (req, res) => {
2726 return res . send ( renderIndex ( ) ) ;
2827} ) ;
2928
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 ) ;
3432} ) ;
3533
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+ ) ;
4144
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
4549 ) ;
50+ sendHtml ( req , res , exists ) ;
4651} ) ;
4752
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 ) ;
5256} ) ;
5357
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
5762 ) ;
63+ sendHtml ( req , res , exists ) ;
5864} ) ;
5965
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 ) ;
6469} ) ;
6570
6671router . get ( '/login' , ( req , res ) => {
@@ -98,15 +103,11 @@ router.get('/assets', (req, res) => {
98103 }
99104} ) ;
100105
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 ) ;
110111} ) ;
111112
112113router . get ( '/account' , ( req , res ) => {
@@ -121,16 +122,17 @@ router.get('/about', (req, res) => {
121122 res . send ( renderIndex ( ) ) ;
122123} ) ;
123124
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
127129 ) ;
130+ sendHtml ( req , res , exists ) ;
128131} ) ;
129132
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 ) ;
134136} ) ;
135137
136138router . get ( '/privacy-policy' , ( req , res ) => {
0 commit comments