Some changes
parent
5cbb1c1a97
commit
3e9c5e4fe7
17
src/admin.rs
17
src/admin.rs
|
@ -10,11 +10,11 @@ use crate::{
|
|||
RbDbConn,
|
||||
};
|
||||
|
||||
#[get("/users")]
|
||||
pub async fn get_users(_admin: Admin, conn: RbDbConn) -> RbResult<Json<Vec<db::User>>>
|
||||
{
|
||||
Ok(Json(conn.run(|c| db::users::all(c)).await?))
|
||||
}
|
||||
// #[get("/users")]
|
||||
// pub async fn get_users(_admin: Admin, conn: RbDbConn) -> RbResult<Json<Vec<db::User>>>
|
||||
// {
|
||||
// Ok(Json(conn.run(|c| db::users::all(c)).await?))
|
||||
// }
|
||||
|
||||
#[post("/users", data = "<user>")]
|
||||
pub async fn create_user(_admin: Admin, conn: RbDbConn, user: Json<db::NewUser>) -> 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)
|
||||
}
|
||||
|
|
|
@ -52,7 +52,7 @@ pub fn create(conn: &PgConnection, new_post: &NewPost) -> RbResult<Post>
|
|||
{
|
||||
Ok(insert_into(posts)
|
||||
.values(new_post)
|
||||
.get_result::<Post>(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::<Post>(conn)
|
||||
.get_result(conn)
|
||||
.map_err(|_| RbError::DbError("Couldn't update post."))?)
|
||||
}
|
||||
|
||||
|
|
|
@ -44,7 +44,7 @@ pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult<Vec<Secti
|
|||
Ok(sections
|
||||
.offset(offset_.into())
|
||||
.limit(limit_.into())
|
||||
.load::<Section>(conn)
|
||||
.load(conn)
|
||||
.map_err(|_| RbError::DbError("Couldn't query sections."))?)
|
||||
}
|
||||
|
||||
|
@ -52,7 +52,7 @@ pub fn create(conn: &PgConnection, new_post: &NewSection) -> RbResult<Section>
|
|||
{
|
||||
Ok(insert_into(sections)
|
||||
.values(new_post)
|
||||
.get_result::<Section>(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::<Section>(conn)
|
||||
.get_result(conn)
|
||||
.map_err(|_| RbError::DbError("Couldn't update section."))?)
|
||||
}
|
||||
|
||||
|
|
|
@ -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<u8>,
|
||||
|
@ -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<Vec<RefreshToken>>
|
||||
#[derive(Deserialize, AsChangeset)]
|
||||
#[table_name = "refresh_tokens"]
|
||||
pub struct PatchRefreshToken
|
||||
{
|
||||
refresh_tokens
|
||||
.load::<RefreshToken>(conn)
|
||||
.map_err(|_| RbError::DbError("Couldn't get all refresh tokens."))
|
||||
pub expires_at: Option<chrono::NaiveDateTime>,
|
||||
pub last_used_at: Option<chrono::NaiveDateTime>,
|
||||
}
|
||||
|
||||
/// 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<Vec<RefreshToken>>
|
||||
{
|
||||
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<RefreshToken>
|
||||
{
|
||||
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<RefreshToken>
|
||||
{
|
||||
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()
|
||||
|
|
|
@ -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<Vec<User>>
|
||||
#[derive(Deserialize, AsChangeset)]
|
||||
#[table_name = "users"]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PatchSection
|
||||
{
|
||||
users
|
||||
.load::<User>(conn)
|
||||
.map_err(|_| RbError::DbError("Couldn't get all users."))
|
||||
username: Option<String>,
|
||||
admin: Option<bool>,
|
||||
}
|
||||
|
||||
pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult<Vec<User>>
|
||||
{
|
||||
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<User>
|
||||
{
|
||||
users.find(user_id).first::<User>(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<User>
|
||||
{
|
||||
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.
|
||||
///
|
||||
|
|
|
@ -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])
|
||||
}
|
||||
|
|
|
@ -16,9 +16,9 @@ pub async fn create_section(
|
|||
_admin: Admin,
|
||||
conn: RbDbConn,
|
||||
new_section: Json<db::NewSection>,
|
||||
) -> RbResult<()>
|
||||
) -> RbResult<Json<db::Section>>
|
||||
{
|
||||
Ok(conn
|
||||
Ok(Json(conn
|
||||
.run(move |c| db::sections::create(c, &new_section.into_inner()))
|
||||
.await?)
|
||||
.await?))
|
||||
}
|
||||
|
|
Reference in New Issue