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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
use axum::{
response::Response,
routing::{patch, post},
Router,
};
use serde::Deserialize;
use crate::{types::{
extract::{AuthorizedUser, Check, CheckResult, Json},
http::ResponseCode,
post::Post,
}, public::docs::{EndpointDocumentation, EndpointMethod}};
pub const POSTS_CREATE: EndpointDocumentation = EndpointDocumentation {
uri: "/api/posts/create",
method: EndpointMethod::Post,
description: "Creates a new post",
body: Some(r#"
{
"content" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
}
"#),
responses: &[
(201, "Successfully created post"),
(400, "Body does not match parameters"),
(401, "Unauthorized"),
(500, "Failed to create post")
],
cookie: Some("auth"),
};
#[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)
}
pub const POSTS_PAGE: EndpointDocumentation = EndpointDocumentation {
uri: "/api/posts/page",
method: EndpointMethod::Post,
description: "Load a section of posts from newest to oldest",
body: Some(r#"
{
"page": 0
}
"#),
responses: &[
(200, "Returns posts in <span>application/json<span>"),
(400, "Body does not match parameters"),
(401, "Unauthorized"),
(500, "Failed to fetch posts")
],
cookie: Some("auth"),
};
#[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)
}
pub const POSTS_USER: EndpointDocumentation = EndpointDocumentation {
uri: "/api/posts/user",
method: EndpointMethod::Post,
description: "Load a section of posts from newest to oldest from a specific user",
body: Some(r#"
{
"user_id": 3,
"page": 0
}
"#),
responses: &[
(200, "Returns posts in <span>application/json<span>"),
(400, "Body does not match parameters"),
(401, "Unauthorized"),
(500, "Failed to fetch posts")
],
cookie: Some("auth"),
};
#[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)
}
pub const POSTS_COMMENT: EndpointDocumentation = EndpointDocumentation {
uri: "/api/posts/comment",
method: EndpointMethod::Patch,
description: "Add a comment to a post",
body: Some(r#"
{
"content": "This is a very cool comment",
"post_id": 0
}
"#),
responses: &[
(200, "Successfully added comment"),
(400, "Body does not match parameters"),
(401, "Unauthorized"),
(500, "Failed to add comment")
],
cookie: Some("auth"),
};
#[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")
}
pub const POSTS_LIKE: EndpointDocumentation = EndpointDocumentation {
uri: "/api/posts/like",
method: EndpointMethod::Patch,
description: "Set like status on a post",
body: Some(r#"
{
"post_id" : 0,
"status" : true
}
"#),
responses: &[
(200, "Successfully set like status"),
(400, "Body does not match parameters"),
(401, "Unauthorized"),
(500, "Failed to set like status")
],
cookie: Some("auth"),
};
#[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))
}
|