2026-04-02 22:29:08 +07:00
|
|
|
use super::dto::*;
|
2026-04-02 15:15:00 +07:00
|
|
|
use crate::middleware::hackathon_auth::HackathonAuthUser;
|
|
|
|
|
use crate::teams::domain::service::TeamService;
|
2026-04-02 22:29:08 +07:00
|
|
|
use axum::{
|
|
|
|
|
Extension, Json,
|
|
|
|
|
extract::{Path, Query},
|
|
|
|
|
response::IntoResponse,
|
|
|
|
|
};
|
|
|
|
|
use imphnen_utils::{
|
|
|
|
|
errors::AppError,
|
|
|
|
|
response_format::{ApiMessage, ApiSuccess},
|
|
|
|
|
};
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
use uuid::Uuid;
|
2026-04-02 15:15:00 +07:00
|
|
|
|
|
|
|
|
pub async fn create_team_handler(
|
2026-04-02 22:29:08 +07:00
|
|
|
Extension(service): Extension<Arc<dyn TeamService>>,
|
|
|
|
|
Extension(auth): Extension<HackathonAuthUser>,
|
|
|
|
|
Json(body): Json<CreateTeamRequest>,
|
2026-04-02 15:15:00 +07:00
|
|
|
) -> Result<axum::response::Response, AppError> {
|
2026-04-02 22:29:08 +07:00
|
|
|
let team = service.create_team(auth.user_id, body.into()).await?;
|
|
|
|
|
Ok(ApiSuccess(TeamResponse::from(team)).into_response())
|
2026-04-02 15:15:00 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn get_team_handler(
|
2026-04-02 22:29:08 +07:00
|
|
|
Extension(service): Extension<Arc<dyn TeamService>>,
|
|
|
|
|
Path(team_id): Path<Uuid>,
|
2026-04-02 15:15:00 +07:00
|
|
|
) -> Result<axum::response::Response, AppError> {
|
2026-04-02 22:29:08 +07:00
|
|
|
let team = service.get_team_by_id(team_id).await?;
|
|
|
|
|
Ok(ApiSuccess(TeamResponse::from(team)).into_response())
|
2026-04-02 15:15:00 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn browse_teams_handler(
|
2026-04-02 22:29:08 +07:00
|
|
|
Extension(service): Extension<Arc<dyn TeamService>>,
|
|
|
|
|
Query(query): Query<BrowseTeamsQuery>,
|
2026-04-02 15:15:00 +07:00
|
|
|
) -> Result<axum::response::Response, AppError> {
|
2026-04-02 22:29:08 +07:00
|
|
|
let result = service.browse_teams(query.into()).await?;
|
|
|
|
|
Ok(
|
|
|
|
|
ApiSuccess(TeamListResponse {
|
|
|
|
|
data: result.teams.into_iter().map(TeamResponse::from).collect(),
|
|
|
|
|
total: result.total,
|
|
|
|
|
page: result.page,
|
|
|
|
|
per_page: result.per_page,
|
|
|
|
|
})
|
|
|
|
|
.into_response(),
|
|
|
|
|
)
|
2026-04-02 15:15:00 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn get_my_teams_handler(
|
2026-04-02 22:29:08 +07:00
|
|
|
Extension(service): Extension<Arc<dyn TeamService>>,
|
|
|
|
|
Extension(auth): Extension<HackathonAuthUser>,
|
2026-04-02 15:15:00 +07:00
|
|
|
) -> Result<axum::response::Response, AppError> {
|
2026-04-02 22:29:08 +07:00
|
|
|
let teams = service.get_user_teams(auth.user_id).await?;
|
|
|
|
|
Ok(
|
|
|
|
|
ApiSuccess(
|
|
|
|
|
teams
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(TeamResponse::from)
|
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
|
)
|
|
|
|
|
.into_response(),
|
|
|
|
|
)
|
2026-04-02 15:15:00 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn update_team_handler(
|
2026-04-02 22:29:08 +07:00
|
|
|
Extension(service): Extension<Arc<dyn TeamService>>,
|
|
|
|
|
Extension(auth): Extension<HackathonAuthUser>,
|
|
|
|
|
Path(team_id): Path<Uuid>,
|
|
|
|
|
Json(body): Json<UpdateTeamRequest>,
|
2026-04-02 15:15:00 +07:00
|
|
|
) -> Result<axum::response::Response, AppError> {
|
2026-04-02 22:29:08 +07:00
|
|
|
let team = service
|
|
|
|
|
.update_team(team_id, auth.user_id, body.into())
|
|
|
|
|
.await?;
|
|
|
|
|
Ok(ApiSuccess(TeamResponse::from(team)).into_response())
|
2026-04-02 15:15:00 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn delete_team_handler(
|
2026-04-02 22:29:08 +07:00
|
|
|
Extension(service): Extension<Arc<dyn TeamService>>,
|
|
|
|
|
Extension(auth): Extension<HackathonAuthUser>,
|
|
|
|
|
Path(team_id): Path<Uuid>,
|
2026-04-02 15:15:00 +07:00
|
|
|
) -> Result<axum::response::Response, AppError> {
|
2026-04-02 22:29:08 +07:00
|
|
|
service.delete_team(team_id, auth.user_id).await?;
|
|
|
|
|
Ok(ApiMessage::ok("Team deleted successfully").into_response())
|
2026-04-02 15:15:00 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn leave_team_handler(
|
2026-04-02 22:29:08 +07:00
|
|
|
Extension(service): Extension<Arc<dyn TeamService>>,
|
|
|
|
|
Extension(auth): Extension<HackathonAuthUser>,
|
|
|
|
|
Path(team_id): Path<Uuid>,
|
2026-04-02 15:15:00 +07:00
|
|
|
) -> Result<axum::response::Response, AppError> {
|
2026-04-02 22:29:08 +07:00
|
|
|
service.leave_team(team_id, auth.user_id).await?;
|
|
|
|
|
Ok(ApiMessage::ok("Left team successfully").into_response())
|
2026-04-02 15:15:00 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn remove_member_handler(
|
2026-04-02 22:29:08 +07:00
|
|
|
Extension(service): Extension<Arc<dyn TeamService>>,
|
|
|
|
|
Extension(auth): Extension<HackathonAuthUser>,
|
|
|
|
|
Path((team_id, member_id)): Path<(Uuid, Uuid)>,
|
2026-04-02 15:15:00 +07:00
|
|
|
) -> Result<axum::response::Response, AppError> {
|
2026-04-02 22:29:08 +07:00
|
|
|
service
|
|
|
|
|
.remove_team_member(team_id, auth.user_id, member_id)
|
|
|
|
|
.await?;
|
|
|
|
|
Ok(ApiMessage::ok("Member removed successfully").into_response())
|
2026-04-02 15:15:00 +07:00
|
|
|
}
|