Splitting all types into types directory

This commit is contained in:
Imbus 2023-11-28 02:19:49 +01:00
parent 89ebbf9269
commit 1058c8b477
9 changed files with 123 additions and 102 deletions

View file

@ -0,0 +1,33 @@
use serde::{Deserialize, Serialize};
/// The comment as it is received from the client
#[derive(Debug, Serialize, Deserialize)]
pub struct NewComment {
pub parent_post_id: i64,
pub parent_comment_id: Option<i64>,
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<i64>,
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<i64>,
pub offset: Option<i64>,
}

7
server/src/types/mod.rs Normal file
View file

@ -0,0 +1,7 @@
mod comment;
mod post;
mod user;
pub use comment::*;
pub use post::*;
pub use user::*;

31
server/src/types/post.rs Normal file
View file

@ -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<i64>,
pub offset: Option<i64>,
}

42
server/src/types/user.rs Normal file
View file

@ -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,
}