Make State::update_article return a Future.
In preparation for threading
This commit is contained in:
parent
db4f18807d
commit
50b9ebf59e
2 changed files with 57 additions and 57 deletions
26
src/site.rs
26
src/site.rs
|
@ -178,21 +178,15 @@ impl Resource for ArticleResource {
|
|||
|
||||
body
|
||||
.concat2()
|
||||
.map_err(|x| Box::new(x) as Box<::std::error::Error + Send + Sync>)
|
||||
.and_then(move |body| {
|
||||
let update: UpdateArticle = match serde_urlencoded::from_bytes(&body) {
|
||||
Ok(x) => x,
|
||||
Err(err) => return futures::finished(Response::new()
|
||||
.with_status(hyper::StatusCode::BadRequest)
|
||||
.with_body(format!("{:#?}", err))
|
||||
).boxed()
|
||||
};
|
||||
|
||||
let updated = match self.state.update_article(self.data.article_id, update.base_revision, &update.body) {
|
||||
Ok(x) => x,
|
||||
Err(x) => return futures::failed(x).boxed(),
|
||||
};
|
||||
|
||||
.map_err(Into::into)
|
||||
.and_then(|body| {
|
||||
serde_urlencoded::from_bytes(&body)
|
||||
.map_err(Into::into)
|
||||
})
|
||||
.and_then(move |update: UpdateArticle| {
|
||||
self.state.update_article(self.data.article_id, update.base_revision, update.body)
|
||||
})
|
||||
.and_then(|updated| {
|
||||
futures::finished(Response::new()
|
||||
.with_status(hyper::StatusCode::Ok)
|
||||
.with_header(ContentType(APPLICATION_JSON.clone()))
|
||||
|
@ -201,7 +195,7 @@ impl Resource for ArticleResource {
|
|||
rendered: &render_markdown(&updated.body),
|
||||
created: &Local.from_utc_datetime(&updated.created).to_string(),
|
||||
}).expect("Should never fail"))
|
||||
).boxed()
|
||||
)
|
||||
})
|
||||
.boxed()
|
||||
}
|
||||
|
|
88
src/state.rs
88
src/state.rs
|
@ -3,6 +3,7 @@ use std;
|
|||
use diesel;
|
||||
use diesel::sqlite::SqliteConnection;
|
||||
use diesel::prelude::*;
|
||||
use futures::{self, Future, IntoFuture};
|
||||
use r2d2::Pool;
|
||||
use r2d2_diesel::ConnectionManager;
|
||||
|
||||
|
@ -31,51 +32,56 @@ impl State {
|
|||
.pop())
|
||||
}
|
||||
|
||||
pub fn update_article(&self, article_id: i32, base_revision: i32, body: &str) -> Result<models::ArticleRevision, Error> {
|
||||
let conn = self.connection_pool.get()?;
|
||||
conn.transaction(|| {
|
||||
use schema::article_revisions;
|
||||
pub fn update_article(&self, article_id: i32, base_revision: i32, body: String) -> futures::BoxFuture<models::ArticleRevision, Error> {
|
||||
self.connection_pool.get().into_future()
|
||||
.map_err(Into::into)
|
||||
.and_then(move |conn| {
|
||||
conn.transaction(|| {
|
||||
use schema::article_revisions;
|
||||
|
||||
let (latest_revision, title) = article_revisions::table
|
||||
.filter(article_revisions::article_id.eq(article_id))
|
||||
.order(article_revisions::revision.desc())
|
||||
.limit(1)
|
||||
.select((article_revisions::revision, article_revisions::title))
|
||||
.load::<(i32, String)>(&*conn)?
|
||||
.pop()
|
||||
.unwrap_or_else(|| unimplemented!("TODO Missing an error type"));
|
||||
let (latest_revision, title) = article_revisions::table
|
||||
.filter(article_revisions::article_id.eq(article_id))
|
||||
.order(article_revisions::revision.desc())
|
||||
.limit(1)
|
||||
.select((article_revisions::revision, article_revisions::title))
|
||||
.load::<(i32, String)>(&*conn)?
|
||||
.pop()
|
||||
.unwrap_or_else(|| unimplemented!("TODO Missing an error type"));
|
||||
|
||||
if latest_revision != base_revision {
|
||||
// TODO: If it is the same edit repeated, just respond OK
|
||||
// TODO: If there is a conflict, transform the edit to work seamlessly
|
||||
unimplemented!("TODO Missing handling of revision conflicts");
|
||||
}
|
||||
let new_revision = base_revision + 1;
|
||||
if latest_revision != base_revision {
|
||||
// TODO: If it is the same edit repeated, just respond OK
|
||||
// TODO: If there is a conflict, transform the edit to work seamlessly
|
||||
unimplemented!("TODO Missing handling of revision conflicts");
|
||||
}
|
||||
let new_revision = base_revision + 1;
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[table_name="article_revisions"]
|
||||
struct NewRevision<'a> {
|
||||
article_id: i32,
|
||||
revision: i32,
|
||||
title: &'a str,
|
||||
body: &'a str,
|
||||
}
|
||||
#[derive(Insertable)]
|
||||
#[table_name="article_revisions"]
|
||||
struct NewRevision<'a> {
|
||||
article_id: i32,
|
||||
revision: i32,
|
||||
title: &'a str,
|
||||
body: &'a str,
|
||||
}
|
||||
|
||||
diesel::insert(&NewRevision {
|
||||
article_id,
|
||||
revision: new_revision,
|
||||
title: &title,
|
||||
body
|
||||
diesel::insert(&NewRevision {
|
||||
article_id,
|
||||
revision: new_revision,
|
||||
title: &title,
|
||||
body: &body,
|
||||
})
|
||||
.into(article_revisions::table)
|
||||
.execute(&*conn)?;
|
||||
|
||||
Ok(article_revisions::table
|
||||
.filter(article_revisions::article_id.eq(article_id))
|
||||
.filter(article_revisions::revision.eq(new_revision))
|
||||
.load::<models::ArticleRevision>(&*conn)?
|
||||
.pop()
|
||||
.expect("We just inserted this row!")
|
||||
)
|
||||
})
|
||||
.into(article_revisions::table)
|
||||
.execute(&*conn)?;
|
||||
|
||||
Ok(article_revisions::table
|
||||
.filter(article_revisions::article_id.eq(article_id))
|
||||
.filter(article_revisions::revision.eq(new_revision))
|
||||
.load::<models::ArticleRevision>(&*conn)?
|
||||
.pop()
|
||||
.expect("We just inserted this row!"))
|
||||
})
|
||||
})
|
||||
.boxed()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue