Skip to content

Port categories/slugs over to Diesel #759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 6 additions & 17 deletions src/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,29 +309,18 @@ pub fn show(req: &mut Request) -> CargoResult<Response> {

/// Handles the `GET /category_slugs` route.
pub fn slugs(req: &mut Request) -> CargoResult<Response> {
let conn = req.tx()?;
let stmt = conn.prepare(
"SELECT slug FROM categories \
ORDER BY slug",
)?;
let rows = stmt.query(&[])?;
let conn = req.db_conn()?;
let slugs = categories::table
.select((categories::slug, categories::slug))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd still like the order by to ensure that the slugs are alphabetical, please :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol wtf why did I write this

.order(categories::slug)
.load(&*conn)?;

#[derive(Serialize)]
#[derive(Serialize, Queryable)]
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(Serialize)]
struct R {
category_slugs: Vec<Slug>,
Expand Down
42 changes: 42 additions & 0 deletions src/tests/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,45 @@ fn update_crate() {
assert_eq!(cnt!(&mut req, "cat1::bar"), 1);
assert_eq!(cnt!(&mut req, "category-2"), 0);
}

#[test]
fn category_slugs_returns_all_slugs_in_alphabetical_order() {
let (_b, app, middle) = ::app();
{
let conn = app.diesel_database.get().unwrap();
::new_category("Foo", "foo")
.create_or_update(&conn)
.unwrap();
::new_category("Bar", "bar")
.create_or_update(&conn)
.unwrap();
}

let mut req = ::req(app, Method::Get, "/api/v1/category_slugs");

#[derive(Deserialize, Debug, PartialEq)]
struct Slug {
id: String,
slug: String,
}

#[derive(Deserialize, Debug, PartialEq)]
struct Slugs {
category_slugs: Vec<Slug>,
}

let response = ::json(&mut ok_resp!(middle.call(&mut req)));
let expected_response = Slugs {
category_slugs: vec![
Slug {
id: "bar".into(),
slug: "bar".into(),
},
Slug {
id: "foo".into(),
slug: "foo".into(),
},
],
};
assert_eq!(expected_response, response);
}