diff --git a/src/db/posts.rs b/src/db/posts.rs index 3952425..a2ac6fb 100644 --- a/src/db/posts.rs +++ b/src/db/posts.rs @@ -40,11 +40,11 @@ pub struct PatchPost /// by `super::MAX_POSTS`. pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult> { - Ok(posts + posts .offset(offset_.into()) .limit(std::cmp::min(limit_, super::MAX_POSTS).into()) .load(conn) - .map_err(|_| RbError::DbError("Couldn't query posts."))?) + .map_err(|_| RbError::DbError("Couldn't query posts.")) } /// Try to find a post given its id (primary key). @@ -60,10 +60,10 @@ pub fn find(conn: &PgConnection, id_: &Uuid) -> RbOption /// Create a new post & store it in the database. pub fn create(conn: &PgConnection, new_post: &NewPost) -> RbResult { - Ok(insert_into(posts) + insert_into(posts) .values(new_post) .get_result(conn) - .map_err(|_| RbError::DbError("Couldn't insert post."))?) + .map_err(|_| RbError::DbError("Couldn't insert post.")) // TODO check for conflict? } @@ -71,10 +71,10 @@ pub fn create(conn: &PgConnection, new_post: &NewPost) -> RbResult /// Update a post in the database with a given ID, returning the updated row. pub fn update(conn: &PgConnection, post_id: &Uuid, patch_post: &PatchPost) -> RbResult { - Ok(diesel::update(posts.filter(id.eq(post_id))) + diesel::update(posts.filter(id.eq(post_id))) .set(patch_post) .get_result(conn) - .map_err(|_| RbError::DbError("Couldn't update post."))?) + .map_err(|_| RbError::DbError("Couldn't update post.")) } /// Delete a post with a given ID. diff --git a/src/db/sections.rs b/src/db/sections.rs index e78ea65..8b1b08a 100644 --- a/src/db/sections.rs +++ b/src/db/sections.rs @@ -54,11 +54,11 @@ pub struct PatchSection /// `limit_` is determined by `super::MAX_SECTIONS`. pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult> { - Ok(sections + sections .offset(offset_.into()) .limit(std::cmp::min(limit_, super::MAX_SECTIONS).into()) .load(conn) - .map_err(|_| RbError::DbError("Couldn't query sections."))?) + .map_err(|_| RbError::DbError("Couldn't query sections.")) } /// Try to find a section given its shortname. @@ -74,10 +74,10 @@ pub fn find_with_shortname(conn: &PgConnection, shortname_: &str) -> RbOption RbResult
{ - Ok(insert_into(sections) + insert_into(sections) .values(new_section) .get_result(conn) - .map_err(|_| RbError::DbError("Couldn't insert section."))?) + .map_err(|_| RbError::DbError("Couldn't insert section.")) // TODO check for conflict? } @@ -85,10 +85,10 @@ pub fn create(conn: &PgConnection, new_section: &NewSection) -> RbResult
RbResult
{ - Ok(diesel::update(sections.filter(id.eq(post_id))) + diesel::update(sections.filter(id.eq(post_id))) .set(patch_post) .get_result(conn) - .map_err(|_| RbError::DbError("Couldn't update section."))?) + .map_err(|_| RbError::DbError("Couldn't update section.")) } // Update a section given its shortname. @@ -98,10 +98,10 @@ pub fn update_with_shortname( patch_section: &PatchSection, ) -> RbResult
{ - Ok(diesel::update(sections.filter(shortname.eq(shortname_))) + diesel::update(sections.filter(shortname.eq(shortname_))) .set(patch_section) .get_result(conn) - .map_err(|_| RbError::DbError("Couldn't update section with shortname."))?) + .map_err(|_| RbError::DbError("Couldn't update section with shortname.")) } /// Delete a section given its ID. diff --git a/src/db/versions.rs b/src/db/versions.rs index ae7e020..a478ee2 100644 --- a/src/db/versions.rs +++ b/src/db/versions.rs @@ -44,11 +44,11 @@ pub struct PatchVersion pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult> { - Ok(versions + versions .offset(offset_.into()) .limit(std::cmp::min(limit_, super::MAX_VERSIONS).into()) .load(conn) - .map_err(|_| RbError::DbError("Couldn't query versions."))?) + .map_err(|_| RbError::DbError("Couldn't query versions.")) } pub fn get_for_post( @@ -58,12 +58,12 @@ pub fn get_for_post( limit_: u32, ) -> RbResult> { - Ok(versions + versions .offset(offset_.into()) .filter(post_id.eq(post_id_)) .limit(std::cmp::min(limit_, super::MAX_VERSIONS).into()) .load(conn) - .map_err(|_| RbError::DbError("Couldn't query versions."))?) + .map_err(|_| RbError::DbError("Couldn't query versions.")) } pub fn find(conn: &PgConnection, id_: &Uuid) -> RbOption @@ -77,20 +77,20 @@ pub fn find(conn: &PgConnection, id_: &Uuid) -> RbOption pub fn create(conn: &PgConnection, new: &NewVersion) -> RbResult { - Ok(insert_into(versions) + insert_into(versions) .values(new) .get_result(conn) - .map_err(|_| RbError::DbError("Couldn't insert version."))?) + .map_err(|_| RbError::DbError("Couldn't insert version.")) // TODO check for conflict? } pub fn update(conn: &PgConnection, id_: &Uuid, patch: &PatchVersion) -> RbResult { - Ok(diesel::update(versions.filter(id.eq(id_))) + diesel::update(versions.filter(id.eq(id_))) .set(patch) .get_result(conn) - .map_err(|_| RbError::DbError("Couldn't update version."))?) + .map_err(|_| RbError::DbError("Couldn't update version.")) } pub fn delete(conn: &PgConnection, id_: &Uuid) -> RbResult<()> diff --git a/src/v1/posts.rs b/src/v1/posts.rs index 3c65e7f..58c0d2f 100644 --- a/src/v1/posts.rs +++ b/src/v1/posts.rs @@ -36,8 +36,7 @@ pub async fn find(conn: RbDbConn, id: uuid::Uuid) -> RbOption> { Ok(conn .run(move |c| db::posts::find(c, &id)) - .await? - .and_then(|p| Some(Json(p)))) + .await?.map(|p| Json(p))) } /// Patch a post given its ID. diff --git a/src/v1/sections.rs b/src/v1/sections.rs index 1bd3624..95f02dc 100644 --- a/src/v1/sections.rs +++ b/src/v1/sections.rs @@ -40,8 +40,7 @@ pub async fn find(conn: RbDbConn, shortname: String) -> RbOption RbOption> { Ok(conn .run(move |c| db::versions::find(c, &id)) - .await? - .and_then(|p| Some(Json(p)))) + .await?.map(|p| Json(p))) } #[patch("/", data = "")]