Skip to content

Commit

Permalink
Add a page listing all valid category slugs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
carols10cents committed Nov 29, 2016
1 parent 8db77c4 commit cc7e92d
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/adapters/category-slug.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
5 changes: 5 additions & 0 deletions app/models/category-slug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import DS from 'ember-data';

export default DS.Model.extend({
slug: DS.attr('string'),
});
1 change: 1 addition & 0 deletions app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
});

Expand Down
12 changes: 12 additions & 0 deletions app/routes/category-slugs.js
Original file line number Diff line number Diff line change
@@ -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);
},
});
14 changes: 14 additions & 0 deletions app/templates/category_slugs.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{{ title 'Category Slugs' }}

<div id='crates-heading'>
<img class='logo crate' src="/assets/crate.png"/>
<h1>All Valid Category Slugs</h1>
</div>

<div class='white-rows'>
<ul>
{{#each model as |slug|}}
<li>{{slug.slug}}</li>
{{/each}}
</ul>
</div>
20 changes: 20 additions & 0 deletions src/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,23 @@ pub fn show(req: &mut Request) -> CargoResult<Response> {
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<Response> {
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<Slug> = rows.iter().map(|r| {
let slug: String = r.get("slug");
Slug { id: slug.clone(), slug: slug }
}).collect();

#[derive(RustcEncodable)]
struct R { category_slugs: Vec<Slug> }
Ok(req.json(&R { category_slugs: slugs }))
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ pub fn middleware(app: Arc<App>) -> 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));

Expand Down

0 comments on commit cc7e92d

Please sign in to comment.