diff --git a/src/admin.rs b/src/admin.rs index 084bd9f..904e5c2 100644 --- a/src/admin.rs +++ b/src/admin.rs @@ -10,11 +10,11 @@ use crate::{ RbDbConn, }; -#[get("/users")] -pub async fn get_users(_admin: Admin, conn: RbDbConn) -> RbResult>> -{ - Ok(Json(conn.run(|c| db::users::all(c)).await?)) -} +// #[get("/users")] +// pub async fn get_users(_admin: Admin, conn: RbDbConn) -> RbResult>> +// { +// Ok(Json(conn.run(|c| db::users::all(c)).await?)) +// } #[post("/users", data = "")] pub async fn create_user(_admin: Admin, conn: RbDbConn, user: Json) -> RbResult<()> @@ -48,8 +48,11 @@ pub fn create_admin_user(conn: &PgConnection, username: &str, password: &str) -> admin: true, }; - db::users::create_or_update(conn, &new_user) - .map_err(|_| RbError::Custom("Couldn't create admin."))?; + if db::users::find_by_username(conn, username).is_ok() { + db::users::create(conn, &new_user); + } + // db::users::create_or_update(conn, &new_user) + // .map_err(|_| RbError::Custom("Couldn't create admin."))?; Ok(true) } diff --git a/src/db/posts.rs b/src/db/posts.rs index f435b9e..109da6d 100644 --- a/src/db/posts.rs +++ b/src/db/posts.rs @@ -52,7 +52,7 @@ pub fn create(conn: &PgConnection, new_post: &NewPost) -> RbResult { Ok(insert_into(posts) .values(new_post) - .get_result::(conn) + .get_result(conn) .map_err(|_| RbError::DbError("Couldn't insert post."))?) // TODO check for conflict? @@ -62,7 +62,7 @@ pub fn update(conn: &PgConnection, post_id: &Uuid, patch_post: &PatchPost) -> Rb { Ok(diesel::update(posts.filter(id.eq(post_id))) .set(patch_post) - .get_result::(conn) + .get_result(conn) .map_err(|_| RbError::DbError("Couldn't update post."))?) } diff --git a/src/db/sections.rs b/src/db/sections.rs index 5c69352..4adcc35 100644 --- a/src/db/sections.rs +++ b/src/db/sections.rs @@ -44,7 +44,7 @@ pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult(conn) + .load(conn) .map_err(|_| RbError::DbError("Couldn't query sections."))?) } @@ -52,7 +52,7 @@ pub fn create(conn: &PgConnection, new_post: &NewSection) -> RbResult
{ Ok(insert_into(sections) .values(new_post) - .get_result::
(conn) + .get_result(conn) .map_err(|_| RbError::DbError("Couldn't insert section."))?) // TODO check for conflict? @@ -62,7 +62,7 @@ pub fn update(conn: &PgConnection, post_id: &Uuid, patch_post: &PatchSection) -> { Ok(diesel::update(sections.filter(id.eq(post_id))) .set(patch_post) - .get_result::
(conn) + .get_result(conn) .map_err(|_| RbError::DbError("Couldn't update section."))?) } diff --git a/src/db/tokens.rs b/src/db/tokens.rs index cbb8898..f2226c3 100644 --- a/src/db/tokens.rs +++ b/src/db/tokens.rs @@ -2,6 +2,7 @@ use diesel::{insert_into, prelude::*, Insertable, PgConnection, Queryable}; use uuid::Uuid; +use serde::{Serialize, Deserialize}; use crate::{ errors::{RbError, RbResult}, @@ -9,7 +10,7 @@ use crate::{ }; /// A refresh token as stored in the database -#[derive(Queryable)] +#[derive(Queryable, Serialize)] pub struct RefreshToken { pub token: Vec, @@ -19,7 +20,7 @@ pub struct RefreshToken } /// A new refresh token to be added into the database -#[derive(Insertable)] +#[derive(Deserialize, Insertable)] #[table_name = "refresh_tokens"] pub struct NewRefreshToken { @@ -28,33 +29,46 @@ pub struct NewRefreshToken pub expires_at: chrono::NaiveDateTime, } -// TODO add pagination as this could grow very quickly -/// Returns all refresh tokens contained in the database. -/// -/// # Arguments -/// -/// * `conn` - database connection to use -pub fn all(conn: &PgConnection) -> RbResult> +#[derive(Deserialize, AsChangeset)] +#[table_name = "refresh_tokens"] +pub struct PatchRefreshToken { - refresh_tokens - .load::(conn) - .map_err(|_| RbError::DbError("Couldn't get all refresh tokens.")) + pub expires_at: Option, + pub last_used_at: Option, } -/// Insert a new refresh token into the database. -/// -/// # Arguments -/// -/// * `conn` - database connection to use -/// * `new_refresh_token` - token to insert -pub fn create(conn: &PgConnection, new_refresh_token: &NewRefreshToken) -> RbResult<()> +pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult> { - insert_into(refresh_tokens) - .values(new_refresh_token) - .execute(conn) - .map_err(|_| RbError::DbError("Couldn't insert refresh token."))?; + Ok(refresh_tokens + .offset(offset_.into()) + .limit(limit_.into()) + .load(conn) + .map_err(|_| RbError::DbError("Couldn't query tokens."))?) +} + +pub fn create(conn: &PgConnection, new_token: &NewRefreshToken) -> RbResult +{ + Ok(insert_into(refresh_tokens) + .values(new_token) + .get_result(conn) + .map_err(|_| RbError::DbError("Couldn't insert refresh token."))?) // TODO check for conflict? +} + +pub fn update(conn: &PgConnection, token_: &[u8], patch_token: &PatchRefreshToken) -> RbResult +{ + Ok(diesel::update(refresh_tokens.filter(token.eq(token_))) + .set(patch_token) + .get_result(conn) + .map_err(|_| RbError::DbError("Couldn't update token."))?) +} + +pub fn delete(conn: &PgConnection, token_: &[u8]) -> RbResult<()> +{ + diesel::delete(refresh_tokens.filter(token.eq(token_))) + .execute(conn) + .map_err(|_| RbError::DbError("Couldn't delete token."))?; Ok(()) } @@ -67,13 +81,13 @@ pub fn create(conn: &PgConnection, new_refresh_token: &NewRefreshToken) -> RbRes /// * `token_val` - token value to search for pub fn find_with_user( conn: &PgConnection, - token_val: &[u8], + token_: &[u8], ) -> Option<(RefreshToken, super::users::User)> { // TODO actually check for errors here refresh_tokens .inner_join(crate::schema::users::dsl::users) - .filter(token.eq(token_val)) + .filter(token.eq(token_)) .first::<(RefreshToken, super::users::User)>(conn) .map_err(|_| RbError::DbError("Couldn't get refresh token & user.")) .ok() diff --git a/src/db/users.rs b/src/db/users.rs index 37ef9c2..4929a15 100644 --- a/src/db/users.rs +++ b/src/db/users.rs @@ -1,5 +1,3 @@ -//! Handles user-related database operations. - use diesel::{prelude::*, AsChangeset, Insertable, Queryable}; use serde::{Deserialize, Serialize}; use uuid::Uuid; @@ -9,7 +7,6 @@ use crate::{ schema::{users, users::dsl::*}, }; -/// A user as stored in the database. #[derive(Queryable, Serialize)] pub struct User { @@ -21,8 +18,7 @@ pub struct User pub admin: bool, } -/// A new user to add to the database. -#[derive(Insertable, AsChangeset, Deserialize)] +#[derive(Insertable, Deserialize)] #[table_name = "users"] pub struct NewUser { @@ -31,35 +27,29 @@ pub struct NewUser pub admin: bool, } -/// Returns all users in the database. -/// -/// # Arguments -/// -/// * `conn` - database connection to use -pub fn all(conn: &PgConnection) -> RbResult> +#[derive(Deserialize, AsChangeset)] +#[table_name = "users"] +#[serde(rename_all = "camelCase")] +pub struct PatchSection { - users - .load::(conn) - .map_err(|_| RbError::DbError("Couldn't get all users.")) + username: Option, + admin: Option, +} + +pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult> +{ + Ok(users + .offset(offset_.into()) + .limit(limit_.into()) + .load(conn) + .map_err(|_| RbError::DbError("Couldn't query users."))?) } -/// Find a user with a given ID. -/// -/// # Arguments -/// -/// * `conn` - database connection to use -/// * `user_id` - ID to search for pub fn find(conn: &PgConnection, user_id: Uuid) -> Option { users.find(user_id).first::(conn).ok() } -/// Find a user with a given username. -/// -/// # Arguments -/// -/// * `conn` - database connection to use -/// * `username_` - username to search for pub fn find_by_username(conn: &PgConnection, username_: &str) -> RbResult { Ok(users @@ -94,18 +84,18 @@ pub fn create(conn: &PgConnection, new_user: &NewUser) -> RbResult<()> /// /// * `conn` - database connection to use /// * `new_user` - user to insert/update -pub fn create_or_update(conn: &PgConnection, new_user: &NewUser) -> RbResult<()> -{ - diesel::insert_into(users) - .values(new_user) - .on_conflict(username) - .do_update() - .set(new_user) - .execute(conn) - .map_err(|_| RbError::DbError("Couldn't create or update user."))?; +// pub fn create_or_update(conn: &PgConnection, new_user: &NewUser) -> RbResult<()> +// { +// diesel::insert_into(users) +// .values(new_user) +// .on_conflict(username) +// .do_update() +// .set(new_user) +// .execute(conn) +// .map_err(|_| RbError::DbError("Couldn't create or update user."))?; - Ok(()) -} +// Ok(()) +// } /// Delete the user with the given ID. /// diff --git a/src/main.rs b/src/main.rs index f9e6217..bd32e6c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -109,7 +109,7 @@ fn rocket() -> _ ) .mount( "/api/admin", - routes![admin::get_users, admin::create_user, admin::get_user_info], + routes![admin::create_user, admin::get_user_info], ) .mount("/api/sections", routes![sections::create_section]) } diff --git a/src/sections.rs b/src/sections.rs index 013c0d3..adc9a2d 100644 --- a/src/sections.rs +++ b/src/sections.rs @@ -16,9 +16,9 @@ pub async fn create_section( _admin: Admin, conn: RbDbConn, new_section: Json, -) -> RbResult<()> +) -> RbResult> { - Ok(conn + Ok(Json(conn .run(move |c| db::sections::create(c, &new_section.into_inner())) - .await?) + .await?)) }