Cleanup. Make use of .first() and .optional() from Diesel

This commit is contained in:
Magnus Hoff 2017-09-20 23:40:07 +02:00
parent debf44623c
commit 631e76324b

View file

@ -75,9 +75,8 @@ impl State {
Ok(article_revisions::table Ok(article_revisions::table
.filter(article_revisions::article_id.eq(article_id)) .filter(article_revisions::article_id.eq(article_id))
.filter(article_revisions::revision.eq(revision)) .filter(article_revisions::revision.eq(revision))
.limit(1) .first::<models::ArticleRevision>(&*connection_pool.get()?)
.load::<models::ArticleRevision>(&*connection_pool.get()?)? .optional()?)
.pop())
}) })
} }
@ -100,14 +99,13 @@ impl State {
Ok(match article_revisions::table Ok(match article_revisions::table
.filter(article_revisions::slug.eq(slug)) .filter(article_revisions::slug.eq(slug))
.order(article_revisions::sequence_number.desc()) .order(article_revisions::sequence_number.desc())
.limit(1)
.select(( .select((
article_revisions::article_id, article_revisions::article_id,
article_revisions::revision, article_revisions::revision,
article_revisions::latest, article_revisions::latest,
)) ))
.load::<ArticleRevisionStub>(&*conn)? .first::<ArticleRevisionStub>(&*conn)
.pop() .optional()?
{ {
None => SlugLookup::Miss, None => SlugLookup::Miss,
Some(ref stub) if stub.latest => SlugLookup::Hit { Some(ref stub) if stub.latest => SlugLookup::Hit {
@ -118,11 +116,8 @@ impl State {
article_revisions::table article_revisions::table
.filter(article_revisions::latest.eq(true)) .filter(article_revisions::latest.eq(true))
.filter(article_revisions::article_id.eq(stub.article_id)) .filter(article_revisions::article_id.eq(stub.article_id))
.limit(1)
.select(article_revisions::slug) .select(article_revisions::slug)
.load::<String>(&*conn)? .first::<String>(&*conn)?
.pop()
.expect("Data model requires this to exist")
) )
}) })
}) })
@ -141,15 +136,12 @@ impl State {
let (latest_revision, prev_title, prev_slug) = article_revisions::table let (latest_revision, prev_title, prev_slug) = article_revisions::table
.filter(article_revisions::article_id.eq(article_id)) .filter(article_revisions::article_id.eq(article_id))
.order(article_revisions::revision.desc()) .order(article_revisions::revision.desc())
.limit(1)
.select(( .select((
article_revisions::revision, article_revisions::revision,
article_revisions::title, article_revisions::title,
article_revisions::slug, article_revisions::slug,
)) ))
.load::<(i32, String, String)>(&*conn)? .first::<(i32, String, String)>(&*conn)?;
.pop()
.unwrap_or_else(|| unimplemented!("TODO Missing an error type"));
if latest_revision != base_revision { if latest_revision != base_revision {
// TODO: If it is the same edit repeated, just respond OK // TODO: If it is the same edit repeated, just respond OK