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