Skip to content

Commit c218e7b

Browse files
committed
feat(api): add externals 'create ticket' api endpoint
1 parent 7ece6c6 commit c218e7b

10 files changed

Lines changed: 92 additions & 2 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod tickets;
2+
3+
pub use tickets::*;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use crate::types::BotState;
2+
use axum::Json;
3+
use axum::extract::State;
4+
use axum::http::StatusCode;
5+
use rustmail_types::CreateTicket;
6+
use std::sync::Arc;
7+
use tokio::sync::Mutex;
8+
9+
pub async fn handle_external_ticket_create(
10+
State(bot_state): State<Arc<Mutex<BotState>>>,
11+
Json(update): Json<CreateTicket>,
12+
) -> Result<Json<serde_json::Value>, (StatusCode, String)> {
13+
let current_config = {
14+
let state = bot_state.lock().await;
15+
match &state.config {
16+
Some(c) => c.clone(),
17+
None => {
18+
return Err((
19+
StatusCode::INTERNAL_SERVER_ERROR,
20+
"Configuration not loaded".to_string(),
21+
));
22+
}
23+
}
24+
};
25+
26+
println!(
27+
"Discor ID : {:?} | Api key: {:?}",
28+
update.discord_id, update.api_key
29+
);
30+
31+
Ok(Json(serde_json::json!({"status": "ticket created"})))
32+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod create;
2+
3+
pub use create::*;

rustmail/src/api/handler/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
pub mod auth;
22
pub mod bot;
3+
pub mod externals;
34
pub mod panel;
45
pub mod user;
56

67
pub use auth::*;
78
pub use bot::*;
9+
pub use externals::*;
810
pub use panel::*;
911
pub use user::*;

rustmail/src/api/router.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ pub fn create_api_router(bot_state: Arc<Mutex<BotState>>) -> Router {
99
let auth_router = create_auth_router();
1010
let panel_router = create_panel_router(bot_state.clone());
1111
let user_router = create_user_router(bot_state.clone());
12+
let external_router = create_external_router(bot_state.clone());
1213

1314
let app = Router::new()
1415
.nest("/api/bot", bot_router)
1516
.nest("/api/auth", auth_router)
1617
.nest("/api/panel", panel_router)
1718
.nest("/api/user", user_router)
19+
.nest("/api/externals", external_router)
1820
.with_state(bot_state.clone());
1921

2022
app

rustmail/src/api/routes/bot.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::prelude::api::*;
22
use crate::prelude::types::*;
33
use axum::Router;
4-
use axum::routing::{get, post};
4+
use axum::routing::{get, post, put};
55
use std::sync::Arc;
66
use tokio::sync::Mutex;
77

@@ -13,7 +13,7 @@ pub fn create_bot_router(bot_state: Arc<Mutex<BotState>>) -> Router<Arc<Mutex<Bo
1313
.route("/status", get(handle_status_bot))
1414
.route("/tickets", get(handle_tickets_bot))
1515
.route("/config", get(handle_get_config))
16-
.route("/config", axum::routing::put(handle_update_config))
16+
.route("/config", put(handle_update_config))
1717
.layer(axum::middleware::from_fn_with_state(
1818
bot_state,
1919
auth_middleware,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
pub mod tickets;
2+
pub use tickets::*;
3+
4+
use crate::api::auth_middleware;
5+
use crate::types::BotState;
6+
use axum::Router;
7+
use std::sync::Arc;
8+
use tokio::sync::Mutex;
9+
10+
pub fn create_external_router(bot_state: Arc<Mutex<BotState>>) -> Router<Arc<Mutex<BotState>>> {
11+
let external_tickets_router = create_external_ticket_router(bot_state.clone());
12+
13+
let bot_router = Router::new()
14+
.nest("/tickets", external_tickets_router)
15+
.layer(axum::middleware::from_fn_with_state(
16+
bot_state,
17+
auth_middleware,
18+
));
19+
20+
bot_router
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use crate::api::{auth_middleware, handle_external_ticket_create};
2+
use crate::types::BotState;
3+
use axum::Router;
4+
use axum::routing::{get, post};
5+
use std::sync::Arc;
6+
use tokio::sync::Mutex;
7+
8+
pub fn create_external_ticket_router(
9+
bot_state: Arc<Mutex<BotState>>,
10+
) -> Router<Arc<Mutex<BotState>>> {
11+
let bot_router = Router::new()
12+
.route("/create", post(handle_external_ticket_create))
13+
.layer(axum::middleware::from_fn_with_state(
14+
bot_state,
15+
auth_middleware,
16+
));
17+
18+
bot_router
19+
}

rustmail/src/api/routes/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
pub mod auth;
22
pub mod bot;
3+
pub mod externals;
34
pub mod panel;
45
pub mod user;
56

67
pub use auth::*;
78
pub use bot::*;
9+
pub use externals::*;
810
pub use panel::*;
911
pub use user::*;

rustmail_types/src/api/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,9 @@ pub struct ConfigResponse {
1212
pub reminders: ReminderConfig,
1313
pub logs: LogsConfig,
1414
}
15+
16+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
17+
pub struct CreateTicket {
18+
pub discord_id: String,
19+
pub api_key: String,
20+
}

0 commit comments

Comments
 (0)