Skip to content

Commit 78ddcf5

Browse files
committed
Sort authors in sql add a todo for why this works
1 parent 93fd8e5 commit 78ddcf5

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

src/version.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub struct Version {
3434
}
3535

3636
pub enum Author {
37-
User(User),
37+
User(User), //TODO: not used. this should be removed.
3838
Name(String),
3939
}
4040

@@ -167,25 +167,22 @@ impl Version {
167167

168168
pub fn authors(&self, conn: &GenericConnection) -> CargoResult<Vec<Author>> {
169169
let stmt = conn.prepare("SELECT * FROM version_authors
170-
WHERE version_id = $1")?;
170+
WHERE version_id = $1
171+
ORDER BY name ASC")?;
171172
let rows = stmt.query(&[&self.id])?;
172-
let mut authors = rows.into_iter().map(|row| {
173+
rows.into_iter().map(|row| {
173174
let user_id: Option<i32> = row.get("user_id");
174175
let name: String = row.get("name");
175176
Ok(match user_id {
176177
Some(id) => Author::User(User::find(conn, id)?),
177178
None => Author::Name(name),
178179
})
179-
}).collect::<CargoResult<Vec<Author>>>()?;
180-
authors.sort_by(|ref a, ref b| a.name().cmp(&b.name()));
181-
Ok(authors)
180+
}).collect()
182181
}
183182

184183
pub fn add_author(&self,
185184
conn: &GenericConnection,
186185
name: &str) -> CargoResult<()> {
187-
println!("add author: {}", name);
188-
// TODO: at least try to link `name` to a pre-existing user
189186
conn.execute("INSERT INTO version_authors (version_id, name)
190187
VALUES ($1, $2)", &[&self.id, &name])?;
191188
Ok(())
@@ -219,15 +216,6 @@ impl Model for Version {
219216
fn table_name(_: Option<Version>) -> &'static str { "versions" }
220217
}
221218

222-
impl Author {
223-
fn name(&self) -> Option<&str> {
224-
match self {
225-
&Author::Name(ref n) => {Some(&n)},
226-
&Author::User(ref u) => {u.name.as_ref().map(String::as_str)}
227-
}
228-
}
229-
}
230-
231219
/// Handles the `GET /versions` route.
232220
pub fn index(req: &mut Request) -> CargoResult<Response> {
233221
let conn = req.tx()?;

0 commit comments

Comments
 (0)