backend sliced by modules
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
use axum::{
|
||||
extract::{Extension, State},
|
||||
middleware,
|
||||
routing::post,
|
||||
Json, Router,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use serde_json::{json, Value};
|
||||
|
||||
use crate::{api::ApiState, db::models::User, error::AppError, modules::auth::guard::auth_guard};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct SubPayload {
|
||||
pub plan: String,
|
||||
}
|
||||
|
||||
pub fn router(state: ApiState) -> Router<ApiState> {
|
||||
Router::new()
|
||||
.route("/subscribe", post(buy_sub_api))
|
||||
.route("/trial", post(activate_trial_api))
|
||||
.route_layer(middleware::from_fn_with_state(state, auth_guard))
|
||||
}
|
||||
|
||||
async fn buy_sub_api(
|
||||
Extension(user): Extension<User>,
|
||||
State(state): State<ApiState>,
|
||||
Json(payload): Json<SubPayload>,
|
||||
) -> Result<Json<Value>, AppError> {
|
||||
state
|
||||
.billing_service
|
||||
.buy_subscription(user.tg_id, &payload.plan)
|
||||
.await?;
|
||||
Ok(Json(
|
||||
json!({"status": "success", "message": format!("Mock payment for {} successful", payload.plan)}),
|
||||
))
|
||||
}
|
||||
|
||||
async fn activate_trial_api(
|
||||
Extension(user): Extension<User>,
|
||||
State(state): State<ApiState>,
|
||||
) -> Result<Json<Value>, AppError> {
|
||||
state.billing_service.activate_trial(user.tg_id).await?;
|
||||
Ok(Json(
|
||||
json!({"status": "success", "message": "Trial Uplink Activated"}),
|
||||
))
|
||||
}
|
||||
Reference in New Issue
Block a user