1
1
// users.js
2
2
// Routes to CRUD users.
3
3
4
+ var URL = require ( 'url' ) ;
5
+
6
+ var errors = require ( '../models/errors' ) ;
4
7
var User = require ( '../models/user' ) ;
5
8
9
+ function getUserURL ( user ) {
10
+ return '/users/' + encodeURIComponent ( user . username ) ;
11
+ }
12
+
6
13
/**
7
14
* GET /users
8
15
*/
9
16
exports . list = function ( req , res , next ) {
10
17
User . getAll ( function ( err , users ) {
11
18
if ( err ) return next ( err ) ;
12
19
res . render ( 'users' , {
13
- users : users
20
+ User : User ,
21
+ users : users ,
22
+ username : req . query . username , // Support pre-filling create form
23
+ error : req . query . error , // Errors creating; see create route
14
24
} ) ;
15
25
} ) ;
16
26
} ;
17
27
18
28
/**
19
- * POST /users
29
+ * POST /users {username, ...}
20
30
*/
21
31
exports . create = function ( req , res , next ) {
22
32
User . create ( {
23
- username : req . body [ ' username' ]
33
+ username : req . body . username
24
34
} , function ( err , user ) {
25
- if ( err ) return next ( err ) ;
26
- res . redirect ( '/users/' + user . username ) ;
35
+ if ( err ) {
36
+ if ( err instanceof errors . ValidationError ) {
37
+ // Return to the create form and show the error message.
38
+ // TODO: Assuming username is the issue; hardcoding for that
39
+ // being the only input right now.
40
+ // TODO: It'd be better to use a cookie to "remember" this info,
41
+ // e.g. using a flash session.
42
+ return res . redirect ( URL . format ( {
43
+ pathname : '/users' ,
44
+ query : {
45
+ username : req . body . username ,
46
+ error : err . message ,
47
+ } ,
48
+ } ) ) ;
49
+ } else {
50
+ return next ( err ) ;
51
+ }
52
+ }
53
+ res . redirect ( getUserURL ( user ) ) ;
27
54
} ) ;
28
55
} ;
29
56
@@ -32,28 +59,50 @@ exports.create = function (req, res, next) {
32
59
*/
33
60
exports . show = function ( req , res , next ) {
34
61
User . get ( req . params . username , function ( err , user ) {
62
+ // TODO: Gracefully "no such user" error. E.g. 404 page.
35
63
if ( err ) return next ( err ) ;
36
64
// TODO: Also fetch and show followers? (Not just follow*ing*.)
37
65
user . getFollowingAndOthers ( function ( err , following , others ) {
38
66
if ( err ) return next ( err ) ;
39
67
res . render ( 'user' , {
68
+ User : User ,
40
69
user : user ,
41
70
following : following ,
42
- others : others
71
+ others : others ,
72
+ username : req . query . username , // Support pre-filling edit form
73
+ error : req . query . error , // Errors editing; see edit route
43
74
} ) ;
44
75
} ) ;
45
76
} ) ;
46
77
} ;
47
78
48
79
/**
49
- * POST /users/:username
80
+ * POST /users/:username {username, ...}
50
81
*/
51
82
exports . edit = function ( req , res , next ) {
52
83
User . get ( req . params . username , function ( err , user ) {
84
+ // TODO: Gracefully "no such user" error. E.g. 404 page.
53
85
if ( err ) return next ( err ) ;
54
86
user . patch ( req . body , function ( err ) {
55
- if ( err ) return next ( err ) ;
56
- res . redirect ( '/users/' + user . username ) ;
87
+ if ( err ) {
88
+ if ( err instanceof errors . ValidationError ) {
89
+ // Return to the edit form and show the error message.
90
+ // TODO: Assuming username is the issue; hardcoding for that
91
+ // being the only input right now.
92
+ // TODO: It'd be better to use a cookie to "remember" this
93
+ // info, e.g. using a flash session.
94
+ return res . redirect ( URL . format ( {
95
+ pathname : getUserURL ( user ) ,
96
+ query : {
97
+ username : req . body . username ,
98
+ error : err . message ,
99
+ } ,
100
+ } ) ) ;
101
+ } else {
102
+ return next ( err ) ;
103
+ }
104
+ }
105
+ res . redirect ( getUserURL ( user ) ) ;
57
106
} ) ;
58
107
} ) ;
59
108
} ;
@@ -63,6 +112,8 @@ exports.edit = function (req, res, next) {
63
112
*/
64
113
exports . del = function ( req , res , next ) {
65
114
User . get ( req . params . username , function ( err , user ) {
115
+ // TODO: Gracefully handle "no such user" error somehow.
116
+ // E.g. redirect back to /users with an info message?
66
117
if ( err ) return next ( err ) ;
67
118
user . del ( function ( err ) {
68
119
if ( err ) return next ( err ) ;
@@ -72,32 +123,42 @@ exports.del = function (req, res, next) {
72
123
} ;
73
124
74
125
/**
75
- * POST /users/:username/follow
126
+ * POST /users/:username/follow {otherUsername}
76
127
*/
77
128
exports . follow = function ( req , res , next ) {
78
129
User . get ( req . params . username , function ( err , user ) {
130
+ // TODO: Gracefully handle "no such user" error somehow.
131
+ // This is the source user, so e.g. 404 page?
79
132
if ( err ) return next ( err ) ;
80
133
User . get ( req . body . otherUsername , function ( err , other ) {
134
+ // TODO: Gracefully handle "no such user" error somehow.
135
+ // This is the target user, so redirect back to the source user w/
136
+ // an info message?
81
137
if ( err ) return next ( err ) ;
82
138
user . follow ( other , function ( err ) {
83
139
if ( err ) return next ( err ) ;
84
- res . redirect ( '/users/' + user . username ) ;
140
+ res . redirect ( getUserURL ( user ) ) ;
85
141
} ) ;
86
142
} ) ;
87
143
} ) ;
88
144
} ;
89
145
90
146
/**
91
- * POST /users/:username/unfollow
147
+ * POST /users/:username/unfollow {otherUsername}
92
148
*/
93
149
exports . unfollow = function ( req , res , next ) {
94
150
User . get ( req . params . username , function ( err , user ) {
151
+ // TODO: Gracefully handle "no such user" error somehow.
152
+ // This is the source user, so e.g. 404 page?
95
153
if ( err ) return next ( err ) ;
96
154
User . get ( req . body . otherUsername , function ( err , other ) {
155
+ // TODO: Gracefully handle "no such user" error somehow.
156
+ // This is the target user, so redirect back to the source user w/
157
+ // an info message?
97
158
if ( err ) return next ( err ) ;
98
159
user . unfollow ( other , function ( err ) {
99
160
if ( err ) return next ( err ) ;
100
- res . redirect ( '/users/' + user . username ) ;
161
+ res . redirect ( getUserURL ( user ) ) ;
101
162
} ) ;
102
163
} ) ;
103
164
} ) ;
0 commit comments