From cc7e92dceda94fa3a5b44a76b78a7bedc3d26014 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 28 Nov 2016 16:52:44 -0500 Subject: [PATCH] Add a page listing all valid category slugs To direct people to when they have specified an invalid slug. JSON containing all the slugs is available at /api/v1/category_slugs, but visiting that in a browser doesn't work. --- app/adapters/category-slug.js | 11 +++++++++++ app/models/category-slug.js | 5 +++++ app/router.js | 1 + app/routes/category-slugs.js | 12 ++++++++++++ app/templates/category_slugs.hbs | 14 ++++++++++++++ src/category.rs | 20 ++++++++++++++++++++ src/lib.rs | 1 + 7 files changed, 64 insertions(+) create mode 100644 app/adapters/category-slug.js create mode 100644 app/models/category-slug.js create mode 100644 app/routes/category-slugs.js create mode 100644 app/templates/category_slugs.hbs diff --git a/app/adapters/category-slug.js b/app/adapters/category-slug.js new file mode 100644 index 00000000000..109a6447095 --- /dev/null +++ b/app/adapters/category-slug.js @@ -0,0 +1,11 @@ +import ApplicationAdapter from './application'; +import Ember from 'ember'; + +export default ApplicationAdapter.extend({ + pathForType(modelName) { + var decamelized = Ember.String.underscore( + Ember.String.decamelize(modelName) + ); + return Ember.String.pluralize(decamelized); + } +}); diff --git a/app/models/category-slug.js b/app/models/category-slug.js new file mode 100644 index 00000000000..167c436b97c --- /dev/null +++ b/app/models/category-slug.js @@ -0,0 +1,5 @@ +import DS from 'ember-data'; + +export default DS.Model.extend({ + slug: DS.attr('string'), +}); \ No newline at end of file diff --git a/app/router.js b/app/router.js index e65d3ea9a4f..ee35611828b 100644 --- a/app/router.js +++ b/app/router.js @@ -39,6 +39,7 @@ Router.map(function() { this.route('category', { path: '/categories/:category_id' }, function() { this.route('index', { path: '/' }); }); + this.route('category_slugs'); this.route('catchAll', { path: '*path' }); }); diff --git a/app/routes/category-slugs.js b/app/routes/category-slugs.js new file mode 100644 index 00000000000..8416e4d23d6 --- /dev/null +++ b/app/routes/category-slugs.js @@ -0,0 +1,12 @@ +import Ember from 'ember'; + +export default Ember.Route.extend({ + queryParams: { + page: { refreshModel: true }, + sort: { refreshModel: true }, + }, + + model(params) { + return this.store.query('category-slug', params); + }, +}); diff --git a/app/templates/category_slugs.hbs b/app/templates/category_slugs.hbs new file mode 100644 index 00000000000..6e36210b1c2 --- /dev/null +++ b/app/templates/category_slugs.hbs @@ -0,0 +1,14 @@ +{{ title 'Category Slugs' }} + +
+ +

All Valid Category Slugs

+
+ +
+ +
\ No newline at end of file diff --git a/src/category.rs b/src/category.rs index 9cca8f8268b..859886aa446 100644 --- a/src/category.rs +++ b/src/category.rs @@ -233,3 +233,23 @@ pub fn show(req: &mut Request) -> CargoResult { struct R { category: EncodableCategoryWithSubcategories} Ok(req.json(&R { category: cat_with_subcats })) } + +/// Handles the `GET /category_slugs` route. +pub fn slugs(req: &mut Request) -> CargoResult { + let conn = try!(req.tx()); + let stmt = try!(conn.prepare("SELECT slug FROM categories \ + ORDER BY slug")); + let rows = try!(stmt.query(&[])); + + #[derive(RustcEncodable)] + struct Slug { id: String, slug: String } + + let slugs: Vec = rows.iter().map(|r| { + let slug: String = r.get("slug"); + Slug { id: slug.clone(), slug: slug } + }).collect(); + + #[derive(RustcEncodable)] + struct R { category_slugs: Vec } + Ok(req.json(&R { category_slugs: slugs })) +} diff --git a/src/lib.rs b/src/lib.rs index 2782db45db5..f03cfc751ef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -105,6 +105,7 @@ pub fn middleware(app: Arc) -> MiddlewareBuilder { api_router.get("/keywords/:keyword_id", C(keyword::show)); api_router.get("/categories", C(category::index)); api_router.get("/categories/:category_id", C(category::show)); + api_router.get("/category_slugs", C(category::slugs)); api_router.get("/users/:user_id", C(user::show)); let api_router = Arc::new(R404(api_router));