116 lines
2.9 KiB
Rust
116 lines
2.9 KiB
Rust
use axum::{extract::State, http::StatusCode};
|
|
use serde::Deserialize;
|
|
use sqlx::PgPool;
|
|
use uuid::Uuid;
|
|
|
|
use super::{
|
|
dtos::user_app::{UserAppRequest, UserAppResponse},
|
|
extractors::{Auth, Json, Path, Query},
|
|
};
|
|
use crate::{app::AppError, repository};
|
|
|
|
pub fn create_router() -> axum::Router<crate::state::AppState> {
|
|
axum::Router::new()
|
|
.route("/", axum::routing::get(list_user_apps))
|
|
.route(
|
|
"/{app_id}",
|
|
axum::routing::get(get_user_app)
|
|
.put(upsert_user_app)
|
|
.delete(delete_user_app),
|
|
)
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct QueryParams {
|
|
pub limit: Option<u64>,
|
|
pub offset: Option<u64>,
|
|
}
|
|
|
|
#[axum::debug_handler]
|
|
#[tracing::instrument(skip(pool))]
|
|
async fn list_user_apps(
|
|
Auth(auth): Auth,
|
|
Path(user_id): Path<Uuid>,
|
|
Query(query): Query<QueryParams>,
|
|
State(pool): State<PgPool>,
|
|
) -> Result<Json<Vec<UserAppResponse>>, AppError> {
|
|
if auth.user_id() != user_id {
|
|
return Err(AppError::NotFound);
|
|
}
|
|
|
|
let query = repository::user_apps::Query {
|
|
limit: Some(query.limit.unwrap_or(20)),
|
|
offset: query.offset,
|
|
};
|
|
|
|
let apps = repository::user_apps::list(&pool, user_id, query)
|
|
.await?
|
|
.into_iter()
|
|
.map(UserAppResponse::from)
|
|
.collect();
|
|
|
|
Ok(Json(apps))
|
|
}
|
|
|
|
#[axum::debug_handler]
|
|
#[tracing::instrument(skip(pool))]
|
|
async fn get_user_app(
|
|
Auth(auth): Auth,
|
|
Path((user_id, app_id)): Path<(Uuid, Uuid)>,
|
|
State(pool): State<PgPool>,
|
|
) -> Result<Json<UserAppResponse>, AppError> {
|
|
if auth.user_id() != user_id {
|
|
return Err(AppError::NotFound);
|
|
}
|
|
|
|
let app = repository::user_apps::get_by_id(&pool, user_id, app_id)
|
|
.await?
|
|
.ok_or(AppError::NotFound)?;
|
|
|
|
Ok(Json(app.into()))
|
|
}
|
|
|
|
#[axum::debug_handler]
|
|
#[tracing::instrument(skip(pool, request))]
|
|
async fn upsert_user_app(
|
|
Auth(auth): Auth,
|
|
Path((user_id, app_id)): Path<(Uuid, Uuid)>,
|
|
State(pool): State<PgPool>,
|
|
Json(request): Json<UserAppRequest>,
|
|
) -> Result<(StatusCode, Json<UserAppResponse>), AppError> {
|
|
if auth.user_id() != user_id {
|
|
return Err(AppError::NotFound);
|
|
}
|
|
|
|
let app = request.into_domain(user_id, app_id);
|
|
let upserted = repository::user_apps::upsert(&pool, app)
|
|
.await?
|
|
.ok_or(AppError::AppIdAlreadyExists)?;
|
|
|
|
let status = if upserted.previous.is_some() {
|
|
StatusCode::OK
|
|
} else {
|
|
StatusCode::CREATED
|
|
};
|
|
|
|
Ok((status, Json(upserted.current.into())))
|
|
}
|
|
|
|
#[axum::debug_handler]
|
|
#[tracing::instrument(skip(pool))]
|
|
async fn delete_user_app(
|
|
Auth(auth): Auth,
|
|
Path((user_id, app_id)): Path<(Uuid, Uuid)>,
|
|
State(pool): State<PgPool>,
|
|
) -> Result<StatusCode, AppError> {
|
|
if auth.user_id() != user_id {
|
|
return Err(AppError::NotFound);
|
|
}
|
|
|
|
repository::user_apps::delete_by_id(&pool, user_id, app_id)
|
|
.await?
|
|
.ok_or(AppError::NotFound)?;
|
|
|
|
Ok(StatusCode::NO_CONTENT)
|
|
}
|