summaryrefslogtreecommitdiff
path: root/src/api/posts.rs
blob: 6aa074ff304a096b6894cce7887d216839f0c8b7 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use axum::{
    response::Response,
    routing::{patch, post},
    Router,
};
use serde::Deserialize;

use crate::types::{
    extract::{AuthorizedUser, Check, CheckResult, Json},
    http::ResponseCode,
    post::Post,
};

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

impl Check for PostCreateRequest {
    fn check(&self) -> CheckResult {
        Self::assert_length(
            &self.content,
            1,
            500,
            "Comments must be between 1-500 characters long",
        )?;
        Ok(())
    }
}

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.text("Failed to create post")
    };

    let Ok(json) = serde_json::to_string(&post) else {
        return ResponseCode::InternalServerError.text("Failed to create post")
    };

    ResponseCode::Created.json(&json)
}

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

impl Check for PostPageRequest {
    fn check(&self) -> CheckResult {
        Ok(())
    }
}

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

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

    ResponseCode::Success.json(&json)
}

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

impl Check for UsersPostsRequest {
    fn check(&self) -> CheckResult {
        Ok(())
    }
}

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

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

    ResponseCode::Success.json(&json)
}

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

impl Check for PostCommentRequest {
    fn check(&self) -> CheckResult {
        Self::assert_length(
            &self.content,
            1,
            255,
            "Comments must be between 1-255 characters long",
        )?;
        Ok(())
    }
}

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.text("Failed to add comment")
    };

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

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

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

impl Check for PostLikeRequest {
    fn check(&self) -> CheckResult {
        Ok(())
    }
}

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.text("Failed to fetch posts")
    };

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

    ResponseCode::Success.text("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))
}