Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add teams get admin api #43

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use axum::{
pub fn routes<S: StateTrait>(state: S) -> Router<S> {
Router::new()
.route("/register", post(register::register::<S>))
.nest("/team", team::routes::<S>())
.nest("/team", team::routes::<S>(state.clone()))
.nest("/problem", problem::routes::<S>(state.clone()))
.nest("/competition", competition::routes::<S>(state.clone()))
.route("/ws", get(socket::ws_handler::<S>))
Expand Down
54 changes: 54 additions & 0 deletions src/handlers/team/get.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use crate::{error::Result, json::Json, StateTrait};
use axum::extract::State;
use entity::{teams, users};
use sea_orm::{EntityTrait, FromQueryResult, TransactionTrait};
use serde::Serialize;
use uuid::Uuid;

#[derive(Debug, Serialize, FromQueryResult)]
pub struct Member {
id: Uuid,
school: String,
class: i16,
}

#[derive(Debug, Serialize)]
pub struct Team {
id: Uuid,
name: String,
owner: Uuid,
co_owner: Option<Uuid>,
locked: bool,
join_code: String,
members: Vec<Member>,
}

pub type Response = Json<Vec<Team>>;

pub async fn get_all_teams<S: StateTrait>(State(state): State<S>) -> Result<Response> {
let txn = state.db().begin().await?;

let teams = teams::Entity::find().all(&txn).await?;

let mut response = Vec::with_capacity(teams.len());

for team in teams {
let members = users::Entity::find_in_team(&team.id)
.into_model::<Member>()
.all(&txn)
.await?;

response.push(Team {
id: team.id,
name: team.name,
owner: team.owner,
co_owner: team.co_owner,
locked: team.locked,
join_code: team.join_code,
members,
})
}

txn.commit().await?;
Ok(Json(response))
}
15 changes: 10 additions & 5 deletions src/handlers/team/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
mod code;
mod create;
mod disband;
mod get;
mod join;
mod kick;
mod leave;
mod update;

use crate::state::StateTrait;
use crate::{middlewares::PermissionsLayer, state::StateTrait};
use axum::{
routing::{patch, post},
routing::{get, patch, post},
Router,
};

Expand All @@ -28,9 +29,8 @@ use axum::{
/// POST /team/code
///
/// # Admin actions
/// GET /team/:id
/// GET /teams
pub fn routes<S: StateTrait>() -> Router<S> {
/// GET /team
pub fn routes<S: StateTrait>(state: S) -> Router<S> {
Router::new()
.route("/create", post(create::create_team::<S>))
.route("/join", post(join::join_team::<S>))
Expand All @@ -39,4 +39,9 @@ pub fn routes<S: StateTrait>() -> Router<S> {
.route("/disband", post(disband::disband_team::<S>))
.route("/kick", post(kick::kick_user::<S>))
.route("/code", post(code::regenerate_code::<S>))
.route(
"/",
get(get::get_all_teams::<S>)
.layer(PermissionsLayer::new(state, &["mathcompetition.admin"])),
)
}
Loading
Loading