Skip to content
This repository has been archived by the owner on Nov 23, 2022. It is now read-only.

Implement fetch bots by user #71

Merged
merged 14 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ferrischat_webserver/src/entrypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ pub async fn entrypoint() {
expand_version!("users/{user_id}/bots"),
web::post().to(create_bot),
)
// GET /users/{user_id}/bots
.route(
expand_version!("users/{user_id}/bots"),
web::get().to(get_bots_by_user),
)
// PATCH /users/{user_id}/bots/{bot_id}
.route(
expand_version!("users/{user_id}/bots/{bot_id}"),
Expand Down
55 changes: 55 additions & 0 deletions ferrischat_webserver/src/users/bots/get_bots_by_user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use actix_web::{HttpRequest, HttpResponse, Responder};

use ferrischat_common::types::{
BotsOwnedByUser, InternalServerErrorJson, NotFoundJson, User, UserFlags,
};

/// GET `/api/v0/users/{user_id}/bots`
/// Get all bots owned by the user
pub async fn get_bots_by_user(req: HttpRequest, auth: crate::Authorization) -> impl Responder {
let bigint_user_id = u128_to_bigdecimal!(auth.0);

let db = get_db_or_fail!();

let resp = sqlx::query!("SELECT * FROM bots WHERE owner_id = $1", bigint_user_id)
.fetch_all(db)
.await;

let bots = match resp {
Ok(resp) => resp
.iter()
.filter_map(|x| {
let resp = sqlx::query!("SELECT * FROM users WHERE id = $1", x.bot_id)
.fetch_one(db)
.await;

match resp {
Ok(resp) => User {
id: resp.id,
name: resp.name,
avatar: None,
guilds: None,
discriminator: resp.discriminator,
flags: UserFlags::from_bits_truncate(resp.flags),
},
Err(e) => {
return HttpResponse::InternalServerError().json(InternalServerErrorJson {
reason: format!("database returned a error: {}", e),
is_bug: false,
link: None,
});
}
}
})
.collect(),
Err(e) => {
return HttpResponse::InternalServerError().json(InternalServerErrorJson {
reason: format!("database returned a error: {}", e),
is_bug: false,
link: None,
});
}
};

return HttpResponse::Ok().json(BotsOwnedByUser { bots });
}
2 changes: 2 additions & 0 deletions ferrischat_webserver/src/users/bots/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod create_bot;
mod delete_bot;
mod edit_bot;
mod get_bots_by_user;

pub use create_bot::*;
pub use delete_bot::*;
pub use edit_bot::*;
pub use get_bots_by_user::*;