summaryrefslogtreecommitdiff
path: root/src/api/posts.rs
blob: 405dfa600288bcd3d1adebc68a83cc287e6e0ff1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use axum::{response::Response, Router, routing::{post, patch}};
use serde::Deserialize;

use crate::types::{extract::{AuthorizedUser, Json}, post::Post, response::ResponseCode};


#[derive(Deserialize)]
struct PostCreateRequest {
    content: String
}

async fn create(AuthorizedUser(user): AuthorizedUser, Json(body): Json<PostCreateRequest>) -> Response {

    let Ok(_post) = Post::new(user.user_id, body.content) else {
        return ResponseCode::InternalServerError.msg("Failed to create post")
    };

    ResponseCode::Created.msg("Successfully created new post")
}

#[derive(Deserialize)]
struct PostPageRequest {
    page: u64
}

async fn page(AuthorizedUser(_user): AuthorizedUser, Json(body): Json<PostPageRequest>) -> Response {
    
    let Ok(posts) = Post::from_post_page(body.page) else {
        return ResponseCode::InternalServerError.msg("Failed to fetch posts")
    };

    let Ok(json) = serde_json::to_string(&posts) else {
        return ResponseCode::InternalServerError.msg("Failed to fetch posts")
    };

    ResponseCode::Success.json(&json)
}

#[derive(Deserialize)]
struct UsersPostsRequest {
    user_id: u64
}

async fn user(AuthorizedUser(_user): AuthorizedUser, Json(body): Json<UsersPostsRequest>) -> Response {
    
    let Ok(posts) = Post::from_user_id(body.user_id) else {
        return ResponseCode::InternalServerError.msg("Failed to fetch posts")
    };

    let Ok(json) = serde_json::to_string(&posts) else {
        return ResponseCode::InternalServerError.msg("Failed to fetch posts")
    };

    ResponseCode::Success.json(&json)
}

#[derive(Deserialize)]
struct PostCommentRequest {
    content: String,
    post_id: u64
}

async fn comment(AuthorizedUser(user): AuthorizedUser, Json(body): Json<PostCommentRequest>) -> Response {
    
    let Ok(mut post) = Post::from_post_id(body.post_id) else {
        return ResponseCode::InternalServerError.msg("Failed to fetch posts")
    };

    if let Err(err) = post.comment(user.user_id, body.content) {
        return err;
    }

    ResponseCode::Success.msg("Successfully commented on post")
}

#[derive(Deserialize)]
struct PostLikeRequest {
    state: bool,
    post_id: u64
}

async fn like(AuthorizedUser(user): AuthorizedUser, Json(body): Json<PostLikeRequest>) -> Response {
    
    let Ok(mut post) = Post::from_post_id(body.post_id) else {
        return ResponseCode::InternalServerError.msg("Failed to fetch posts")
    };

    if let Err(err) = post.like(user.user_id, body.state) {
        return err;
    }

    ResponseCode::Success.msg("Successfully changed like status on post")
}

pub fn router() -> Router {
    Router::new()
    .route("/create", post(create))
    .route("/page", post(page))
    .route("/user", post(user))
    .route("/comment", patch(comment))
    .route("/like", patch(like))
}