1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
use warp::Filter;
use std::collections::HashMap;

use super::handlers;
use super::models::{comment::Comment, reply::Reply, upvote::Upvote, downvote::Downvote};
use super::db::DbConn;

/// Comment filters combined
pub fn comments(
    db_conn: DbConn,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
    list_comments( db_conn.clone() )
        .or( more_comments( db_conn.clone() ) )
        .or( comments_create( db_conn.clone() ) )
        .or( comments_update( db_conn.clone() ) )
        .or( comments_delete( db_conn.clone() ) )
        .or( get_comment( db_conn.clone() ) )
        .or( update_comment_status( db_conn.clone() ) )
        .or( update_comment_message( db_conn.clone() ) )
        .or( comments_total( db_conn.clone() ) )
    .or( replies_create( db_conn.clone() ) )
        .or( list_replies( db_conn.clone() ) )
        .or( replies_update( db_conn.clone() ) )
        .or( replies_delete( db_conn.clone() ) )
        .or( get_reply( db_conn.clone() ) )
        .or( update_reply_message( db_conn.clone() ) )
        .or( upvotes_create( db_conn.clone() ) )
        .or( upvotes_delete( db_conn.clone() ) )
        .or( upvotes_total( db_conn.clone() ) )
        .or( downvotes_create( db_conn.clone() ) )
        .or( downvotes_delete( db_conn.clone() ) )
        .or( downvotes_total( db_conn.clone() ) )
}

/// GET /comments  -> get all comments
pub fn list_comments(
    db_conn: DbConn,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
    warp::path!("comments")
        .and(warp::get())
        .and(with_db_conn(db_conn))
        .and_then(handlers::list_comments)
}

/// GET /more_comments/count  -> get all comments
pub fn more_comments(
    db_conn: DbConn,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
    warp::path!("more_comments" / usize)
        .and(warp::get())
        .and(with_db_conn(db_conn))
        .and_then(handlers::more_comments)
}

/// POST /comments with JSON body 
pub fn comments_create(
    db_conn: DbConn,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
    warp::path!("comments")
        .and(warp::post())
        .and(json_comment_body())
        .and(with_db_conn(db_conn))
        .and_then(handlers::add_comment)
}

/// PUT /comments/:uid with JSON body 
pub fn comments_update(
    db_conn: DbConn,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
    warp::path!("comments" / String)
        .and(warp::put())
        .and(json_comment_body())
        .and(with_db_conn(db_conn))
        .and_then(handlers::update_comment)
}

/// PUT /comment/:status/:uid with JSON body; status are: disable, enable, trash, spam
pub fn update_comment_status(
    db_conn: DbConn,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
    warp::path!("comment" / String / String)
        .and(warp::put())
        .and(with_db_conn(db_conn))
        .and_then(handlers::update_comment_status)
}

/// PUT /comment_message/:uid with JSON body
pub fn update_comment_message(
    db_conn: DbConn,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
    warp::path!("comment_message" / String)
        .and(warp::put())
        .and(json_message_body())
        .and(with_db_conn(db_conn))
        .and_then(handlers::update_comment_message)
}

/// DELETE /comments/:uid
pub fn comments_delete(
    db_conn: DbConn,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
    // make one of the endpoints admin-only to show how authentication filters are used
    let admin_only = warp::header::exact("authorization", "Bearer admin");
    warp::path!("comments" / String)
        // It is important to put the auth check _after_ the path filters. 
        // If we put the auth check before, the request `PUT /todos/invalid-string`
        // would try this filter and reject because the authorization header 
        // doesn't match, rather because the param is wrong for that other path. 
        .and(admin_only)
        .and(warp::delete())
        .and(with_db_conn(db_conn))
        .and_then(handlers::delete_comment)
}

/// GET /comments/:uid 
pub fn get_comment(
    db_conn: DbConn
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
    warp::path!("comments" / String)
        .and(warp::get())
        .and(with_db_conn(db_conn))
        .and_then(handlers::get_comment)
}

fn with_db_conn(db_conn: DbConn) -> impl Filter<Extract = (DbConn,), Error = std::convert::Infallible> + Clone {
    warp::any().map(move || db_conn.clone())
}

fn json_comment_body() -> impl Filter<Extract = (Comment,), Error = warp::Rejection> + Clone {
    // When accepting a body, we want a JSON body 
    // (and to reject huge payloads)... 
    warp::body::content_length_limit(1024 * 16).and(warp::body::json()) 
}

fn json_message_body() -> impl Filter<Extract = (HashMap<String, String>,), Error = warp::Rejection> + Clone {
    warp::body::content_length_limit(1024 * 16).and(warp::body::json())
}

/// GET /comments_total  -> get total number of comments
pub fn comments_total(
    db_conn: DbConn,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
    warp::path!("comments_total")
        .and(warp::get())
        .and(with_db_conn(db_conn))
        .and_then(handlers::comments_total)
}

/// POST /replies with JSON body 
pub fn replies_create(
    db_conn: DbConn,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
    warp::path!("replies")
        .and(warp::post())
        .and(json_reply_body())
        .and(with_db_conn(db_conn))
        .and_then(handlers::add_reply)
}

fn json_reply_body() -> impl Filter<Extract = (Reply,), Error = warp::Rejection> + Clone {
    // When accepting a body, we want a JSON body 
    // (and to reject huge payloads)... 
    warp::body::content_length_limit(1024 * 16).and( warp::body::json() ) 
}

/// GET /replies/comment_id  -> get all replies
pub fn list_replies(
    db_conn: DbConn,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
    warp::path!("replies" / String)
        .and(warp::get())
        .and(with_db_conn(db_conn))
        .and_then(handlers::list_replies)
}

/// PUT /replies/:uid with JSON body 
pub fn replies_update(
    db_conn: DbConn,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
    warp::path!("replies" / String)
        .and(warp::put())
        .and(json_reply_body())
        .and(with_db_conn(db_conn))
        .and_then(handlers::update_reply)
}

/// PUT /reply_message/:uid with JSON body
pub fn update_reply_message(
    db_conn: DbConn,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
    warp::path!("reply_message" / String)
        .and(warp::put())
        .and(json_message_body())
        .and(with_db_conn(db_conn))
        .and_then(handlers::update_reply_message)
}

/// DELETE /replies/:uid
pub fn replies_delete(
    db_conn: DbConn,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
    // make one of the endpoints admin-only to show how authentication filters are used
    let admin_only = warp::header::exact("authorization", "Bearer admin");
    warp::path!("replies" / String)
        // It is important to put the auth check _after_ the path filters. 
        // If we put the auth check before, the request `PUT /todos/invalid-string`
        // would try this filter and reject because the authorization header 
        // doesn't match, rather because the param is wrong for that other path. 
        .and(admin_only)
        .and(warp::delete())
        .and(with_db_conn(db_conn))
        .and_then(handlers::delete_reply)
}

/// GET /reply/:uid 
pub fn get_reply(
    db_conn: DbConn
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
    warp::path!("reply" / String)
        .and(warp::get())
        .and(with_db_conn(db_conn))
        .and_then(handlers::get_reply)
}

/// POST /upvotes with JSON body 
pub fn upvotes_create(
    db_conn: DbConn,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
    warp::path!("upvotes")
        .and(warp::post())
        .and(json_upvote_body())
        .and(with_db_conn(db_conn))
        .and_then(handlers::add_upvote)
}

fn json_upvote_body() -> impl Filter<Extract = (Upvote,), Error = warp::Rejection> + Clone {
    // When accepting a body, we want a JSON body 
    // (and to reject huge payloads)... 
    warp::body::content_length_limit(1024 * 16).and(warp::body::json()) 
}

/// DELETE /upvotes/:comment_id/:user_id
pub fn upvotes_delete(
    db_conn: DbConn,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
    // make one of the endpoints admin-only to show how authentication filters are used
    let admin_only = warp::header::exact("authorization", "Bearer admin");
    warp::path!("upvotes" / String / String)
        // It is important to put the auth check _after_ the path filters. 
        // If we put the auth check before, the request `PUT /todos/invalid-string`
        // would try this filter and reject because the authorization header 
        // doesn't match, rather because the param is wrong for that other path. 
        .and(admin_only)
        .and(warp::delete())
        .and(with_db_conn(db_conn))
        .and_then(handlers::delete_upvote)
}

/// GET /upvotes_total/:comment_id  -> get total number of upvotes
pub fn upvotes_total(
    db_conn: DbConn,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
    warp::path!("upvotes_total" / String)
        .and(warp::get())
        .and(with_db_conn(db_conn))
        .and_then(handlers::upvotes_total)
}

/// POST /downvotes with JSON body 
pub fn downvotes_create(
    db_conn: DbConn,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
    warp::path!("downvotes")
        .and(warp::post())
        .and(json_downvote_body())
        .and(with_db_conn(db_conn))
        .and_then(handlers::add_downvote)
}

fn json_downvote_body() -> impl Filter<Extract = (Downvote,), Error = warp::Rejection> + Clone {
    // When accepting a body, we want a JSON body 
    // (and to reject huge payloads)... 
    warp::body::content_length_limit(1024 * 16).and(warp::body::json()) 
}

/// DELETE /downvotes/:comment_id/:user_id
pub fn downvotes_delete(
    db_conn: DbConn,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
    // make one of the endpoints admin-only to show how authentication filters are used
    let admin_only = warp::header::exact("authorization", "Bearer admin");
    warp::path!("downvotes" / String / String)
        // It is important to put the auth check _after_ the path filters. 
        // If we put the auth check before, the request `PUT /todos/invalid-string`
        // would try this filter and reject because the authorization header 
        // doesn't match, rather because the param is wrong for that other path. 
        .and(admin_only)
        .and(warp::delete())
        .and(with_db_conn(db_conn))
        .and_then(handlers::delete_downvote)
}

/// GET /downvotes_total/:comment_id  -> get total number of downvotes
pub fn downvotes_total(
    db_conn: DbConn,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
    warp::path!("downvotes_total" / String)
        .and(warp::get())
        .and(with_db_conn(db_conn))
        .and_then(handlers::downvotes_total)
}