use crate::{ApiResponse, error::ApiError}; use actix_web::{HttpResponse, Result, web}; use room::ws_context::WsUserContext; use service::AppService; use session::Session; use utoipa::IntoParams; use uuid::Uuid; #[derive(Debug, serde::Deserialize, IntoParams)] pub struct RoomListQuery { pub only_public: Option, } #[utoipa::path( get, path = "/api/project_room/{project_name}/rooms", params( ("project_name" = String, Path), ("only_public" = Option, Query), ), responses( (status = 200, description = "List rooms", body = ApiResponse>), (status = 401, description = "Unauthorized"), (status = 404, description = "Not found"), ), tag = "Room" )] pub async fn room_list( service: web::Data, session: Session, path: web::Path, query: web::Query, ) -> Result { let project_name = path.into_inner(); let user_id = session .user() .ok_or_else(|| ApiError::from(service::error::AppError::Unauthorized))?; let ctx = WsUserContext::new(user_id); let resp = service .room .room_list(project_name, query.only_public, &ctx) .await .map_err(ApiError::from)?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( get, path = "/api/rooms/{room_id}", params( ("room_id" = Uuid, Path), ), responses( (status = 200, description = "Get room", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 404, description = "Not found"), ), tag = "Room" )] pub async fn room_get( service: web::Data, session: Session, path: web::Path, ) -> Result { let room_id = path.into_inner(); let user_id = session .user() .ok_or_else(|| ApiError::from(service::error::AppError::Unauthorized))?; let ctx = WsUserContext::new(user_id); let resp = service .room .room_get(room_id, &ctx) .await .map_err(ApiError::from)?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( post, path = "/api/project_room/{project_name}/rooms", params( ("project_name" = String, Path), ), request_body = room::RoomCreateRequest, responses( (status = 200, description = "Create room", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 403, description = "Forbidden"), (status = 404, description = "Not found"), ), tag = "Room" )] pub async fn room_create( service: web::Data, session: Session, path: web::Path, body: web::Json, ) -> Result { let project_name = path.into_inner(); let user_id = session .user() .ok_or_else(|| ApiError::from(service::error::AppError::Unauthorized))?; let ctx = WsUserContext::new(user_id); let resp = service .room .room_create(project_name, body.into_inner(), &ctx) .await .map_err(ApiError::from)?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( patch, path = "/api/rooms/{room_id}", params( ("room_id" = Uuid, Path), ), request_body = room::RoomUpdateRequest, responses( (status = 200, description = "Update room", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 403, description = "Forbidden"), (status = 404, description = "Not found"), ), tag = "Room" )] pub async fn room_update( service: web::Data, session: Session, path: web::Path, body: web::Json, ) -> Result { let room_id = path.into_inner(); let user_id = session .user() .ok_or_else(|| ApiError::from(service::error::AppError::Unauthorized))?; let ctx = WsUserContext::new(user_id); let resp = service .room .room_update(room_id, body.into_inner(), &ctx) .await .map_err(ApiError::from)?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( delete, path = "/api/rooms/{room_id}", params( ("room_id" = Uuid, Path), ), responses( (status = 200, description = "Delete room"), (status = 401, description = "Unauthorized"), (status = 403, description = "Forbidden"), (status = 404, description = "Not found"), ), tag = "Room" )] pub async fn room_delete( service: web::Data, session: Session, path: web::Path, ) -> Result { let room_id = path.into_inner(); let user_id = session .user() .ok_or_else(|| ApiError::from(service::error::AppError::Unauthorized))?; let ctx = WsUserContext::new(user_id); service .room .room_delete(room_id, &ctx) .await .map_err(ApiError::from)?; Ok(ApiResponse::ok(true).to_response()) }