forked from Chewing_Bever/rusty-bever
Completely restructured codebase
This commit is contained in:
parent
b45c93cdc9
commit
b13b760e2f
16 changed files with 67 additions and 60 deletions
|
|
@ -1,5 +1,8 @@
|
|||
use crate::errors::RBError;
|
||||
use crate::models::{NewRefreshToken, NewUser, RefreshToken, User};
|
||||
use crate::db::{
|
||||
users::{User, NewUser},
|
||||
tokens::{RefreshToken, NewRefreshToken}
|
||||
};
|
||||
use crate::schema::refresh_tokens::dsl as refresh_tokens;
|
||||
use crate::schema::users::dsl as users;
|
||||
use argon2::verify_encoded;
|
||||
2
src/db/mod.rs
Normal file
2
src/db/mod.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod users;
|
||||
pub mod tokens;
|
||||
20
src/db/tokens.rs
Normal file
20
src/db/tokens.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
use uuid::Uuid;
|
||||
use diesel::{Queryable, Insertable};
|
||||
use crate::schema::refresh_tokens;
|
||||
|
||||
|
||||
#[derive(Queryable)]
|
||||
pub struct RefreshToken {
|
||||
pub token: Vec<u8>,
|
||||
pub user_id: Uuid,
|
||||
pub expires_at: chrono::NaiveDateTime,
|
||||
pub last_used_at: Option<chrono::NaiveDateTime>,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[table_name = "refresh_tokens"]
|
||||
pub struct NewRefreshToken {
|
||||
pub token: Vec<u8>,
|
||||
pub user_id: Uuid,
|
||||
pub expires_at: chrono::NaiveDateTime,
|
||||
}
|
||||
29
src/db/users.rs
Normal file
29
src/db/users.rs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
use crate::schema::users;
|
||||
use diesel::{AsChangeset, Insertable, Queryable, prelude::*};
|
||||
use serde::Serialize;
|
||||
use uuid::Uuid;
|
||||
use crate::schema::users::dsl::*;
|
||||
use crate::errors::RBError;
|
||||
|
||||
#[derive(Queryable, Serialize)]
|
||||
pub struct User {
|
||||
pub id: Uuid,
|
||||
pub username: String,
|
||||
#[serde(skip_serializing)]
|
||||
pub password: String,
|
||||
#[serde(skip_serializing)]
|
||||
pub blocked: bool,
|
||||
pub admin: bool,
|
||||
}
|
||||
|
||||
#[derive(Insertable, AsChangeset)]
|
||||
#[table_name = "users"]
|
||||
pub struct NewUser {
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
pub admin: bool,
|
||||
}
|
||||
|
||||
pub fn all(conn: &PgConnection) -> crate::Result<Vec<User>> {
|
||||
users.load::<User>(conn).map_err(|_| RBError::DBError)
|
||||
}
|
||||
|
|
@ -1,10 +1,9 @@
|
|||
#[macro_use]
|
||||
extern crate diesel;
|
||||
|
||||
pub mod db;
|
||||
pub mod auth;
|
||||
pub mod errors;
|
||||
pub mod admin;
|
||||
pub mod models;
|
||||
pub(crate) mod schema;
|
||||
|
||||
pub use errors::Result;
|
||||
|
|
@ -10,9 +10,8 @@ extern crate diesel_migrations;
|
|||
use rocket::{fairing::AdHoc, Build, Rocket};
|
||||
use rocket_sync_db_pools::{database, diesel};
|
||||
|
||||
mod auth;
|
||||
mod admin;
|
||||
pub(crate) mod guards;
|
||||
mod routes;
|
||||
|
||||
embed_migrations!();
|
||||
|
||||
|
|
@ -55,6 +54,6 @@ fn rocket() -> _ {
|
|||
run_db_migrations,
|
||||
))
|
||||
.attach(AdHoc::try_on_ignite("Create admin user", create_admin_user))
|
||||
.mount("/api/auth", auth::routes())
|
||||
.mount("/api/admin", admin::routes())
|
||||
.mount("/api/auth", routes::auth::routes())
|
||||
.mount("/api/admin", routes::admin::routes())
|
||||
}
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
use crate::errors::RBError;
|
||||
use crate::models::User;
|
||||
use diesel::prelude::*;
|
||||
use crate::schema::users::dsl as users;
|
||||
|
||||
pub fn get_users(conn: &PgConnection) -> crate::Result<Vec<User>> {
|
||||
users::users.load::<User>(conn).map_err(|_| RBError::DBError)
|
||||
}
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
use crate::schema::{refresh_tokens, users};
|
||||
use diesel::{AsChangeset, Insertable, Queryable};
|
||||
use serde::Serialize;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Queryable, Serialize)]
|
||||
pub struct User {
|
||||
pub id: Uuid,
|
||||
pub username: String,
|
||||
#[serde(skip_serializing)]
|
||||
pub password: String,
|
||||
#[serde(skip_serializing)]
|
||||
pub blocked: bool,
|
||||
pub admin: bool,
|
||||
}
|
||||
|
||||
#[derive(Insertable, AsChangeset)]
|
||||
#[table_name = "users"]
|
||||
pub struct NewUser {
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
pub admin: bool,
|
||||
}
|
||||
|
||||
#[derive(Queryable)]
|
||||
pub struct RefreshToken {
|
||||
pub token: Vec<u8>,
|
||||
pub user_id: Uuid,
|
||||
pub expires_at: chrono::NaiveDateTime,
|
||||
pub last_used_at: Option<chrono::NaiveDateTime>,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[table_name = "refresh_tokens"]
|
||||
pub struct NewRefreshToken {
|
||||
pub token: Vec<u8>,
|
||||
pub user_id: Uuid,
|
||||
pub expires_at: chrono::NaiveDateTime,
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
use crate::guards::Admin;
|
||||
use crate::RbDbConn;
|
||||
use rb::models::User;
|
||||
use rb::db::users::User;
|
||||
use rocket::serde::json::Json;
|
||||
|
||||
pub fn routes() -> Vec<rocket::Route> {
|
||||
|
|
@ -9,5 +9,5 @@ pub fn routes() -> Vec<rocket::Route> {
|
|||
|
||||
#[get("/users")]
|
||||
async fn get_users(admin: Admin, conn: RbDbConn) -> rb::Result<Json<Vec<User>>> {
|
||||
Ok(Json(conn.run(|c| rb::admin::get_users(c)).await?))
|
||||
Ok(Json(conn.run(|c| rb::db::users::all(c)).await?))
|
||||
}
|
||||
2
src/routes/mod.rs
Normal file
2
src/routes/mod.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod auth;
|
||||
pub mod admin;
|
||||
Loading…
Add table
Add a link
Reference in a new issue