1
1
use chrono:: NaiveDateTime ;
2
- use conduit:: { Request , Response } ;
3
- use conduit_router:: RequestParams ;
4
2
use diesel:: * ;
5
3
6
- use db:: RequestTransaction ;
7
- use util:: { CargoResult , RequestUtils } ;
8
-
9
4
use models:: Crate ;
10
5
use schema:: * ;
6
+ use views:: EncodableCategory ;
11
7
12
8
#[ derive( Clone , Identifiable , Queryable , QueryableByName , Debug ) ]
13
9
#[ table_name = "categories" ]
@@ -30,27 +26,6 @@ pub struct CrateCategory {
30
26
category_id : i32 ,
31
27
}
32
28
33
- #[ derive( Serialize , Deserialize , Debug ) ]
34
- pub struct EncodableCategory {
35
- pub id : String ,
36
- pub category : String ,
37
- pub slug : String ,
38
- pub description : String ,
39
- #[ serde( with = "::util::rfc3339" ) ] pub created_at : NaiveDateTime ,
40
- pub crates_cnt : i32 ,
41
- }
42
-
43
- #[ derive( Serialize , Deserialize , Debug ) ]
44
- pub struct EncodableCategoryWithSubcategories {
45
- pub id : String ,
46
- pub category : String ,
47
- pub slug : String ,
48
- pub description : String ,
49
- #[ serde( with = "::util::rfc3339" ) ] pub created_at : NaiveDateTime ,
50
- pub crates_cnt : i32 ,
51
- pub subcategories : Vec < EncodableCategory > ,
52
- }
53
-
54
29
impl Category {
55
30
pub fn encodable ( self ) -> EncodableCategory {
56
31
let Category {
@@ -180,97 +155,11 @@ impl<'a> NewCategory<'a> {
180
155
}
181
156
}
182
157
183
- /// Handles the `GET /categories` route.
184
- pub fn index ( req : & mut Request ) -> CargoResult < Response > {
185
- let conn = req. db_conn ( ) ?;
186
- let ( offset, limit) = req. pagination ( 10 , 100 ) ?;
187
- let query = req. query ( ) ;
188
- let sort = query. get ( "sort" ) . map_or ( "alpha" , String :: as_str) ;
189
-
190
- let categories = Category :: toplevel ( & conn, sort, limit, offset) ?;
191
- let categories = categories. into_iter ( ) . map ( Category :: encodable) . collect ( ) ;
192
-
193
- // Query for the total count of categories
194
- let total = Category :: count_toplevel ( & conn) ?;
195
-
196
- #[ derive( Serialize ) ]
197
- struct R {
198
- categories : Vec < EncodableCategory > ,
199
- meta : Meta ,
200
- }
201
- #[ derive( Serialize ) ]
202
- struct Meta {
203
- total : i64 ,
204
- }
205
-
206
- Ok ( req. json ( & R {
207
- categories : categories,
208
- meta : Meta { total : total } ,
209
- } ) )
210
- }
211
-
212
- /// Handles the `GET /categories/:category_id` route.
213
- pub fn show ( req : & mut Request ) -> CargoResult < Response > {
214
- let slug = & req. params ( ) [ "category_id" ] ;
215
- let conn = req. db_conn ( ) ?;
216
- let cat = categories:: table
217
- . filter ( categories:: slug. eq ( :: lower ( slug) ) )
218
- . first :: < Category > ( & * conn) ?;
219
- let subcats = cat. subcategories ( & conn) ?
220
- . into_iter ( )
221
- . map ( Category :: encodable)
222
- . collect ( ) ;
223
-
224
- let cat = cat. encodable ( ) ;
225
- let cat_with_subcats = EncodableCategoryWithSubcategories {
226
- id : cat. id ,
227
- category : cat. category ,
228
- slug : cat. slug ,
229
- description : cat. description ,
230
- created_at : cat. created_at ,
231
- crates_cnt : cat. crates_cnt ,
232
- subcategories : subcats,
233
- } ;
234
-
235
- #[ derive( Serialize ) ]
236
- struct R {
237
- category : EncodableCategoryWithSubcategories ,
238
- }
239
- Ok ( req. json ( & R {
240
- category : cat_with_subcats,
241
- } ) )
242
- }
243
-
244
- /// Handles the `GET /category_slugs` route.
245
- pub fn slugs ( req : & mut Request ) -> CargoResult < Response > {
246
- let conn = req. db_conn ( ) ?;
247
- let slugs = categories:: table
248
- . select ( ( categories:: slug, categories:: slug) )
249
- . order ( categories:: slug)
250
- . load ( & * conn) ?;
251
-
252
- #[ derive( Serialize , Queryable ) ]
253
- struct Slug {
254
- id : String ,
255
- slug : String ,
256
- }
257
-
258
- #[ derive( Serialize ) ]
259
- struct R {
260
- category_slugs : Vec < Slug > ,
261
- }
262
- Ok ( req. json ( & R {
263
- category_slugs : slugs,
264
- } ) )
265
- }
266
-
267
158
#[ cfg( test) ]
268
159
mod tests {
269
160
use super :: * ;
270
- use chrono:: NaiveDate ;
271
161
use diesel:: connection:: SimpleConnection ;
272
162
use dotenv:: dotenv;
273
- use serde_json;
274
163
use std:: env;
275
164
276
165
fn pg_connection ( ) -> PgConnection {
@@ -401,42 +290,4 @@ mod tests {
401
290
let expected = vec ! [ ( "Cat 3" . to_string( ) , 6 ) , ( "Cat 1" . to_string( ) , 3 ) ] ;
402
291
assert_eq ! ( expected, categories) ;
403
292
}
404
-
405
- #[ test]
406
- fn category_dates_serializes_to_rfc3339 ( ) {
407
- let cat = EncodableCategory {
408
- id : "" . to_string ( ) ,
409
- category : "" . to_string ( ) ,
410
- slug : "" . to_string ( ) ,
411
- description : "" . to_string ( ) ,
412
- crates_cnt : 1 ,
413
- created_at : NaiveDate :: from_ymd ( 2017 , 1 , 6 ) . and_hms ( 14 , 23 , 11 ) ,
414
- } ;
415
- let json = serde_json:: to_string ( & cat) . unwrap ( ) ;
416
- assert ! (
417
- json. as_str( )
418
- . find( r#""created_at":"2017-01-06T14:23:11+00:00""# )
419
- . is_some( )
420
- ) ;
421
- }
422
-
423
- #[ test]
424
- fn category_with_sub_dates_serializes_to_rfc3339 ( ) {
425
- let cat = EncodableCategoryWithSubcategories {
426
- id : "" . to_string ( ) ,
427
- category : "" . to_string ( ) ,
428
- slug : "" . to_string ( ) ,
429
- description : "" . to_string ( ) ,
430
- crates_cnt : 1 ,
431
- created_at : NaiveDate :: from_ymd ( 2017 , 1 , 6 ) . and_hms ( 14 , 23 , 11 ) ,
432
- subcategories : vec ! [ ] ,
433
- } ;
434
- let json = serde_json:: to_string ( & cat) . unwrap ( ) ;
435
- assert ! (
436
- json. as_str( )
437
- . find( r#""created_at":"2017-01-06T14:23:11+00:00""# )
438
- . is_some( )
439
- ) ;
440
- }
441
-
442
293
}
0 commit comments