diff --git a/server/src/db.rs b/server/src/db.rs index 1baed3e..14e9db9 100644 --- a/server/src/db.rs +++ b/server/src/db.rs @@ -1,4 +1,4 @@ -use crate::routes::{Comment, Post, User}; +use crate::types::{Comment, Post, User}; use argon2::{ password_hash::{rand_core::OsRng, SaltString}, Argon2, PasswordHasher, PasswordVerifier, diff --git a/server/src/main.rs b/server/src/main.rs index e57aaf0..06ed9ce 100755 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -8,6 +8,7 @@ mod db; mod jwt; mod routes; mod state; +mod types; mod util; use routes::{get_comments, get_posts, login, new_comment, new_post, post_by_id, register}; diff --git a/server/src/routes/comment.rs b/server/src/routes/comment.rs index e67a2a2..3df7e00 100644 --- a/server/src/routes/comment.rs +++ b/server/src/routes/comment.rs @@ -1,49 +1,21 @@ use crate::db::{db_get_comments, db_new_comment}; use crate::jwt::validate_token; +use crate::types::{CommentQueryParams, NewComment}; use crate::ServerState; use actix_web::get; use actix_web::web::{Data, Query}; use actix_web::{post, web::Json, HttpResponse, Responder, Result}; use log::info; -use serde::{Deserialize, Serialize}; - -#[derive(Debug, Serialize, Deserialize)] -pub struct NewComment { - pub parent_post_id: i64, - pub parent_comment_id: Option, - pub user_token: String, - pub content: String, -} - -#[derive(Debug, Serialize, Deserialize, Clone, sqlx::FromRow)] -pub struct Comment { - pub id: i64, - pub parent_post_id: i64, - pub parent_comment_id: Option, - pub author_user_id: i64, - pub upvotes: i64, - pub downvotes: i64, - pub content: String, - pub created_at: chrono::NaiveDateTime, - pub updated_at: chrono::NaiveDateTime, -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct CommentQueryParams { - post_id: i64, - limit: Option, - offset: Option, -} #[get("/comments")] pub async fn get_comments( - commentiflter: Query, + comment_filter: Query, state: Data, ) -> Result { - let post_id = commentiflter.post_id; - let limit = commentiflter.limit.unwrap_or(10); - let offset = commentiflter.offset.unwrap_or(0); + let post_id = comment_filter.post_id; + let limit = comment_filter.limit.unwrap_or(10); + let offset = comment_filter.offset.unwrap_or(0); info!( "Getting comments for post {} with limit {} and offset {}", diff --git a/server/src/routes/post.rs b/server/src/routes/post.rs index 286ac56..0eb4ec5 100755 --- a/server/src/routes/post.rs +++ b/server/src/routes/post.rs @@ -1,57 +1,17 @@ use crate::db::{db_get_latest_posts, db_get_post, db_new_post}; use crate::jwt::validate_token; +use crate::types::{NewPost, PostQueryParams}; use crate::ServerState; use actix_web::web::{Data, Path, Query}; use actix_web::{get, post, web::Json, HttpResponse, Responder, Result}; use log::info; -use serde::{Deserialize, Serialize}; -use sqlx::FromRow; - -// The post as it is received from the client -// The token is used to identify the user -#[derive(Debug, Serialize, Deserialize)] -pub struct NewPost { - pub content: String, - pub token: String, -} - -// The post as it is stored in the database, with all the related metadata -#[derive(Debug, Serialize, Deserialize, Clone, FromRow)] -pub struct Post { - pub id: i64, - pub user_id: i64, - pub content: String, - pub upvotes: i64, - pub downvotes: i64, - pub created_at: chrono::NaiveDateTime, - pub updated_at: chrono::NaiveDateTime, -} - -/// The user as it is stored in the database, with all the related metadata -#[derive(Debug, Serialize, Deserialize, Clone, FromRow)] -pub struct User { - pub id: i64, - pub username: String, - pub password: String, - pub created_at: chrono::NaiveDateTime, - pub updated_at: chrono::NaiveDateTime, -} - -// These look like /posts?limit=10&offset=20 in the URL -// Note that these are optional -/// Query parameters for the /posts endpoint -#[derive(Debug, Serialize, Deserialize)] -pub struct QueryParams { - limit: Option, - offset: Option, -} /// Gets all posts from the database, query parameters are optional /// If limit is not specified, it defaults to a sane value #[get("/posts")] pub async fn get_posts( - query: Query, + query: Query, state: Data, ) -> Result { if let (Some(lim), Some(ofs)) = (query.limit, query.offset) { diff --git a/server/src/routes/users.rs b/server/src/routes/users.rs index 60cfc4f..21013dd 100755 --- a/server/src/routes/users.rs +++ b/server/src/routes/users.rs @@ -1,32 +1,13 @@ use crate::db::{db_new_user, db_user_login}; use crate::jwt::token_factory; use crate::state::CaptchaState; +use crate::types::{AuthResponse, LoginData, RegisterData}; use crate::ServerState; use actix_web::web::Data; use actix_web::{post, web::Json, HttpResponse, Responder, Result}; use biosvg::BiosvgBuilder; use log::*; -use serde::{Deserialize, Serialize}; - -#[derive(Debug, Serialize, Deserialize)] -pub struct LoginData { - username: String, - password: String, -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct AuthResponse { - username: String, - token: String, -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct RegisterData { - username: String, - password: String, - captcha: String, -} #[post("/register")] pub async fn register( @@ -77,12 +58,6 @@ pub async fn login(data: Json, state: Data) -> Result, + pub user_token: String, + pub content: String, +} + +/// The comment as it is stored in the database, with all the related metadata +/// This is also the comment as it is sent to the client +#[derive(Debug, Serialize, Deserialize, Clone, sqlx::FromRow)] +pub struct Comment { + pub id: i64, + pub parent_post_id: i64, + pub parent_comment_id: Option, + pub author_user_id: i64, + pub upvotes: i64, + pub downvotes: i64, + pub content: String, + pub created_at: chrono::NaiveDateTime, + pub updated_at: chrono::NaiveDateTime, +} + +/// Query parameters for the /comments endpoint +#[derive(Debug, Serialize, Deserialize)] +pub struct CommentQueryParams { + pub post_id: i64, + pub limit: Option, + pub offset: Option, +} diff --git a/server/src/types/mod.rs b/server/src/types/mod.rs new file mode 100644 index 0000000..2cb8992 --- /dev/null +++ b/server/src/types/mod.rs @@ -0,0 +1,7 @@ +mod comment; +mod post; +mod user; + +pub use comment::*; +pub use post::*; +pub use user::*; diff --git a/server/src/types/post.rs b/server/src/types/post.rs new file mode 100644 index 0000000..f880766 --- /dev/null +++ b/server/src/types/post.rs @@ -0,0 +1,31 @@ +use serde::{Deserialize, Serialize}; +use sqlx::FromRow; + +// The post as it is received from the client +// The token is used to identify the user +#[derive(Debug, Serialize, Deserialize)] +pub struct NewPost { + pub content: String, + pub token: String, +} + +// The post as it is stored in the database, with all the related metadata +#[derive(Debug, Serialize, Deserialize, Clone, FromRow)] +pub struct Post { + pub id: i64, + pub user_id: i64, + pub content: String, + pub upvotes: i64, + pub downvotes: i64, + pub created_at: chrono::NaiveDateTime, + pub updated_at: chrono::NaiveDateTime, +} + +// These look like /posts?limit=10&offset=20 in the URL +// Note that these are optional +/// Query parameters for the /posts endpoint +#[derive(Debug, Serialize, Deserialize)] +pub struct PostQueryParams { + pub limit: Option, + pub offset: Option, +} diff --git a/server/src/types/user.rs b/server/src/types/user.rs new file mode 100644 index 0000000..4caa229 --- /dev/null +++ b/server/src/types/user.rs @@ -0,0 +1,42 @@ +use serde::{Deserialize, Serialize}; +use sqlx::FromRow; + +/// The user as it is stored in the database, with all the related metadata +#[derive(Debug, Serialize, Deserialize, Clone, FromRow)] +pub struct User { + pub id: i64, + pub username: String, + pub password: String, + pub created_at: chrono::NaiveDateTime, + pub updated_at: chrono::NaiveDateTime, +} + +/// The data recieved from the login form +#[derive(Debug, Serialize, Deserialize)] +pub struct LoginData { + pub username: String, + pub password: String, +} + +/// The data recieved from the registration form +#[derive(Debug, Serialize, Deserialize)] +pub struct RegisterData { + pub username: String, + pub password: String, + pub captcha: String, +} + +/// The response sent to the client after a successful login or registration +#[derive(Debug, Serialize, Deserialize)] +pub struct AuthResponse { + pub username: String, + pub token: String, +} + +/// Data sent to the client to render the captcha +/// The captcha_id is used to identify the captcha in the database +#[derive(Debug, Serialize, Deserialize)] +pub struct CaptchaResponse { + pub captcha_svg: String, + pub captcha_id: i32, +}