summaryrefslogtreecommitdiff
path: root/src/api/users.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/users.rs')
-rw-r--r--src/api/users.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/api/users.rs b/src/api/users.rs
new file mode 100644
index 0000000..283ec96
--- /dev/null
+++ b/src/api/users.rs
@@ -0,0 +1,52 @@
+use axum::{Router, response::Response, routing::post};
+use serde::Deserialize;
+use crate::types::{extract::{AuthorizedUser, Json}, response::ResponseCode, user::User};
+
+#[derive(Deserialize)]
+struct UserLoadRequest {
+ ids: Vec<u64>
+}
+
+async fn load_batch(AuthorizedUser(_user): AuthorizedUser, Json(body): Json<UserLoadRequest>) -> Response {
+
+ let users = User::from_user_ids(body.ids);
+ let Ok(json) = serde_json::to_string(&users) else {
+ return ResponseCode::InternalServerError.msg("Failed to fetch users")
+ };
+
+ ResponseCode::Success.json(&json)
+}
+
+#[derive(Deserialize)]
+struct UserPageReqiest {
+ page: u64
+}
+
+async fn load_page(AuthorizedUser(_user): AuthorizedUser, Json(body): Json<UserPageReqiest>) -> Response {
+
+ let Ok(users) = User::from_user_page(body.page) else {
+ return ResponseCode::InternalServerError.msg("Failed to fetch users")
+ };
+
+ let Ok(json) = serde_json::to_string(&users) else {
+ return ResponseCode::InternalServerError.msg("Failed to fetch users")
+ };
+
+ ResponseCode::Success.json(&json)
+}
+
+async fn load_self(AuthorizedUser(user): AuthorizedUser) -> Response {
+
+ let Ok(json) = serde_json::to_string(&user) else {
+ return ResponseCode::InternalServerError.msg("Failed to fetch user")
+ };
+
+ ResponseCode::Success.json(&json)
+}
+
+pub fn router() -> Router {
+ Router::new()
+ .route("/load", post(load_batch))
+ .route("/self", post(load_self))
+ .route("/page", post(load_page))
+} \ No newline at end of file