Compare commits

..

No commits in common. "dev" and "main" have entirely different histories.
dev ... main

8 changed files with 28 additions and 45 deletions

View File

@ -1,5 +0,0 @@
pipeline:
lint:
image: 'rustlang/rust:nightly'
commands:
- cargo fmt -- --check

View File

@ -1,15 +0,0 @@
# When block for each step is temporary until top-level when is properly implemented
pipeline:
clippy:
image: 'rust:1.57'
commands:
- cargo clippy -- -D clippy::all --no-deps
when:
event: pull_request
tests:
image: 'rust:1.57'
commands:
- cargo test
when:
event: pull_request

View File

@ -40,11 +40,11 @@ pub struct PatchPost
/// by `super::MAX_POSTS`. /// by `super::MAX_POSTS`.
pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult<Vec<Post>> pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult<Vec<Post>>
{ {
posts Ok(posts
.offset(offset_.into()) .offset(offset_.into())
.limit(std::cmp::min(limit_, super::MAX_POSTS).into()) .limit(std::cmp::min(limit_, super::MAX_POSTS).into())
.load(conn) .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). /// Try to find a post given its id (primary key).
@ -60,10 +60,10 @@ pub fn find(conn: &PgConnection, id_: &Uuid) -> RbOption<Post>
/// Create a new post & store it in the database. /// Create a new post & store it in the database.
pub fn create(conn: &PgConnection, new_post: &NewPost) -> RbResult<Post> pub fn create(conn: &PgConnection, new_post: &NewPost) -> RbResult<Post>
{ {
insert_into(posts) Ok(insert_into(posts)
.values(new_post) .values(new_post)
.get_result(conn) .get_result(conn)
.map_err(|_| RbError::DbError("Couldn't insert post.")) .map_err(|_| RbError::DbError("Couldn't insert post."))?)
// TODO check for conflict? // TODO check for conflict?
} }
@ -71,10 +71,10 @@ pub fn create(conn: &PgConnection, new_post: &NewPost) -> RbResult<Post>
/// Update a post in the database with a given ID, returning the updated row. /// 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<Post> pub fn update(conn: &PgConnection, post_id: &Uuid, patch_post: &PatchPost) -> RbResult<Post>
{ {
diesel::update(posts.filter(id.eq(post_id))) Ok(diesel::update(posts.filter(id.eq(post_id)))
.set(patch_post) .set(patch_post)
.get_result(conn) .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. /// Delete a post with a given ID.

View File

@ -54,11 +54,11 @@ pub struct PatchSection
/// `limit_` is determined by `super::MAX_SECTIONS`. /// `limit_` is determined by `super::MAX_SECTIONS`.
pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult<Vec<Section>> pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult<Vec<Section>>
{ {
sections Ok(sections
.offset(offset_.into()) .offset(offset_.into())
.limit(std::cmp::min(limit_, super::MAX_SECTIONS).into()) .limit(std::cmp::min(limit_, super::MAX_SECTIONS).into())
.load(conn) .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. /// Try to find a section given its shortname.
@ -74,10 +74,10 @@ pub fn find_with_shortname(conn: &PgConnection, shortname_: &str) -> RbOption<Se
/// Create a new section. /// Create a new section.
pub fn create(conn: &PgConnection, new_section: &NewSection) -> RbResult<Section> pub fn create(conn: &PgConnection, new_section: &NewSection) -> RbResult<Section>
{ {
insert_into(sections) Ok(insert_into(sections)
.values(new_section) .values(new_section)
.get_result(conn) .get_result(conn)
.map_err(|_| RbError::DbError("Couldn't insert section.")) .map_err(|_| RbError::DbError("Couldn't insert section."))?)
// TODO check for conflict? // TODO check for conflict?
} }
@ -85,10 +85,10 @@ pub fn create(conn: &PgConnection, new_section: &NewSection) -> RbResult<Section
/// Update a section given its ID. /// Update a section given its ID.
pub fn update(conn: &PgConnection, post_id: &Uuid, patch_post: &PatchSection) -> RbResult<Section> pub fn update(conn: &PgConnection, post_id: &Uuid, patch_post: &PatchSection) -> RbResult<Section>
{ {
diesel::update(sections.filter(id.eq(post_id))) Ok(diesel::update(sections.filter(id.eq(post_id)))
.set(patch_post) .set(patch_post)
.get_result(conn) .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. // Update a section given its shortname.
@ -98,10 +98,10 @@ pub fn update_with_shortname(
patch_section: &PatchSection, patch_section: &PatchSection,
) -> RbResult<Section> ) -> RbResult<Section>
{ {
diesel::update(sections.filter(shortname.eq(shortname_))) Ok(diesel::update(sections.filter(shortname.eq(shortname_)))
.set(patch_section) .set(patch_section)
.get_result(conn) .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. /// Delete a section given its ID.

View File

@ -44,11 +44,11 @@ pub struct PatchVersion
pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult<Vec<Version>> pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult<Vec<Version>>
{ {
versions Ok(versions
.offset(offset_.into()) .offset(offset_.into())
.limit(std::cmp::min(limit_, super::MAX_VERSIONS).into()) .limit(std::cmp::min(limit_, super::MAX_VERSIONS).into())
.load(conn) .load(conn)
.map_err(|_| RbError::DbError("Couldn't query versions.")) .map_err(|_| RbError::DbError("Couldn't query versions."))?)
} }
pub fn get_for_post( pub fn get_for_post(
@ -58,12 +58,12 @@ pub fn get_for_post(
limit_: u32, limit_: u32,
) -> RbResult<Vec<Version>> ) -> RbResult<Vec<Version>>
{ {
versions Ok(versions
.offset(offset_.into()) .offset(offset_.into())
.filter(post_id.eq(post_id_)) .filter(post_id.eq(post_id_))
.limit(std::cmp::min(limit_, super::MAX_VERSIONS).into()) .limit(std::cmp::min(limit_, super::MAX_VERSIONS).into())
.load(conn) .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<Version> pub fn find(conn: &PgConnection, id_: &Uuid) -> RbOption<Version>
@ -77,20 +77,20 @@ pub fn find(conn: &PgConnection, id_: &Uuid) -> RbOption<Version>
pub fn create(conn: &PgConnection, new: &NewVersion) -> RbResult<Version> pub fn create(conn: &PgConnection, new: &NewVersion) -> RbResult<Version>
{ {
insert_into(versions) Ok(insert_into(versions)
.values(new) .values(new)
.get_result(conn) .get_result(conn)
.map_err(|_| RbError::DbError("Couldn't insert version.")) .map_err(|_| RbError::DbError("Couldn't insert version."))?)
// TODO check for conflict? // TODO check for conflict?
} }
pub fn update(conn: &PgConnection, id_: &Uuid, patch: &PatchVersion) -> RbResult<Version> pub fn update(conn: &PgConnection, id_: &Uuid, patch: &PatchVersion) -> RbResult<Version>
{ {
diesel::update(versions.filter(id.eq(id_))) Ok(diesel::update(versions.filter(id.eq(id_)))
.set(patch) .set(patch)
.get_result(conn) .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<()> pub fn delete(conn: &PgConnection, id_: &Uuid) -> RbResult<()>

View File

@ -36,7 +36,8 @@ pub async fn find(conn: RbDbConn, id: uuid::Uuid) -> RbOption<Json<db::Post>>
{ {
Ok(conn Ok(conn
.run(move |c| db::posts::find(c, &id)) .run(move |c| db::posts::find(c, &id))
.await?.map(|p| Json(p))) .await?
.and_then(|p| Some(Json(p))))
} }
/// Patch a post given its ID. /// Patch a post given its ID.

View File

@ -40,7 +40,8 @@ pub async fn find(conn: RbDbConn, shortname: String) -> RbOption<Json<db::Sectio
{ {
Ok(conn Ok(conn
.run(move |c| db::sections::find_with_shortname(c, &shortname)) .run(move |c| db::sections::find_with_shortname(c, &shortname))
.await?.map(|p| Json(p))) .await?
.and_then(|p| Some(Json(p))))
} }
/// Patch a section given its shortname. /// Patch a section given its shortname.

View File

@ -49,7 +49,8 @@ pub async fn find(conn: RbDbConn, id: uuid::Uuid) -> RbOption<Json<db::Version>>
{ {
Ok(conn Ok(conn
.run(move |c| db::versions::find(c, &id)) .run(move |c| db::versions::find(c, &id))
.await?.map(|p| Json(p))) .await?
.and_then(|p| Some(Json(p))))
} }
#[patch("/<id>", data = "<patch>")] #[patch("/<id>", data = "<patch>")]