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

@ -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<i64>,
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<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,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct CommentQueryParams {
post_id: i64,
limit: Option<i64>,
offset: Option<i64>,
}
#[get("/comments")]
pub async fn get_comments(
commentiflter: Query<CommentQueryParams>,
comment_filter: Query<CommentQueryParams>,
state: Data<ServerState>,
) -> Result<impl Responder> {
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 {}",

View file

@ -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<i64>,
offset: Option<i64>,
}
/// 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<QueryParams>,
query: Query<PostQueryParams>,
state: Data<ServerState>,
) -> Result<impl Responder> {
if let (Some(lim), Some(ofs)) = (query.limit, query.offset) {

View file

@ -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<LoginData>, state: Data<ServerState>) -> Result<im
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct CaptchaResponse {
captcha_svg: String,
captcha_id: i32,
}
/// Request a captcha from the captcha service
#[allow(unreachable_code, unused_variables)]
#[post("/captcha")]