diff options
Diffstat (limited to 'src/routers/queue/manage.rs')
| -rw-r--r-- | src/routers/queue/manage.rs | 78 |
1 files changed, 43 insertions, 35 deletions
diff --git a/src/routers/queue/manage.rs b/src/routers/queue/manage.rs index 8f42e07..b799458 100644 --- a/src/routers/queue/manage.rs +++ b/src/routers/queue/manage.rs | |||
| @@ -1,50 +1,32 @@ | |||
| 1 | use axum::extract::State; | 1 | use axum::extract::State; |
| 2 | use entity::{queues, users}; | 2 | use entity::queues; |
| 3 | use sea_orm::{ | 3 | use sea_orm::{ |
| 4 | ActiveModelTrait, ActiveValue::Set, ColumnTrait, DatabaseConnection, EntityTrait, | 4 | ActiveModelTrait, ActiveValue::Set, ColumnTrait, EntityTrait, IntoActiveModel, ModelTrait, |
| 5 | IntoActiveModel, ModelTrait, QueryFilter, | 5 | QueryFilter, |
| 6 | }; | 6 | }; |
| 7 | use serde::Deserialize; | 7 | use serde::Deserialize; |
| 8 | use utoipa::{IntoParams, ToSchema}; | 8 | use utoipa::{IntoParams, ToSchema}; |
| 9 | use utoipa_axum::{router::OpenApiRouter, routes}; | 9 | use utoipa_axum::{router::OpenApiRouter, routes}; |
| 10 | 10 | ||
| 11 | use crate::{ | 11 | use crate::{ |
| 12 | ApiError, ApiResult, AppState, ClientError, GlobalResponses, SuccessResponse, | 12 | ApiResult, AppState, ClientError, GlobalResponses, SuccessResponse, |
| 13 | extract::{ApiJson, ApiQuery, Auth}, | 13 | extract::{ApiJson, ApiQuery, Auth}, |
| 14 | models::Queue, | 14 | models::Queue, |
| 15 | tags::QUEUE, | 15 | tags::QUEUE, |
| 16 | util::{get_owned_queue, user_exists}, | ||
| 16 | }; | 17 | }; |
| 17 | 18 | ||
| 18 | async fn user_exists(id: i64, db: &DatabaseConnection) -> Result<bool, ApiError> { | ||
| 19 | Ok(users::Entity::find_by_id(id).one(db).await?.is_some()) | ||
| 20 | } | ||
| 21 | |||
| 22 | async fn get_owned_queue( | ||
| 23 | id: i64, | ||
| 24 | owner_id: i64, | ||
| 25 | db: &DatabaseConnection, | ||
| 26 | ) -> Result<queues::Model, ApiError> { | ||
| 27 | let queue = queues::Entity::find_by_id(id) | ||
| 28 | .one(db) | ||
| 29 | .await? | ||
| 30 | .ok_or(ClientError::QueueNotFound { id })?; | ||
| 31 | |||
| 32 | if queue.owner_id != owner_id { | ||
| 33 | return Err(ClientError::NotQueueOwner { id: queue.id }.into()); | ||
| 34 | } | ||
| 35 | |||
| 36 | Ok(queue) | ||
| 37 | } | ||
| 38 | |||
| 39 | #[derive(Deserialize, IntoParams)] | 19 | #[derive(Deserialize, IntoParams)] |
| 40 | #[into_params(parameter_in = Query)] | 20 | #[into_params(parameter_in = Query)] |
| 41 | struct GetByIdQueueQuery { | 21 | struct GetQueueByIdQuery { |
| 22 | #[param(example = 1)] | ||
| 42 | id: i64, | 23 | id: i64, |
| 43 | } | 24 | } |
| 44 | 25 | ||
| 45 | #[derive(Deserialize, IntoParams)] | 26 | #[derive(Deserialize, IntoParams)] |
| 46 | #[into_params(parameter_in = Query)] | 27 | #[into_params(parameter_in = Query)] |
| 47 | struct GetByOwnerIdQuery { | 28 | struct GetByOwnerIdQuery { |
| 29 | #[param(example = 1)] | ||
| 48 | owner_id: i64, | 30 | owner_id: i64, |
| 49 | } | 31 | } |
| 50 | 32 | ||
| @@ -82,11 +64,11 @@ struct DeleteQueueRequest { | |||
| 82 | 64 | ||
| 83 | #[utoipa::path( | 65 | #[utoipa::path( |
| 84 | get, | 66 | get, |
| 85 | path = "/get/id", | 67 | path = "/get/by_id", |
| 86 | tag = QUEUE, | 68 | tag = QUEUE, |
| 87 | summary = "Get by id", | 69 | summary = "Get by id", |
| 88 | description = "Get the queue by id", | 70 | description = "Get the queue by id", |
| 89 | params(GetByIdQueueQuery), | 71 | params(GetQueueByIdQuery), |
| 90 | responses( | 72 | responses( |
| 91 | ( | 73 | ( |
| 92 | status = 200, body = SuccessResponse<Option<Queue>>, | 74 | status = 200, body = SuccessResponse<Option<Queue>>, |
| @@ -97,7 +79,7 @@ struct DeleteQueueRequest { | |||
| 97 | )] | 79 | )] |
| 98 | async fn get_by_id( | 80 | async fn get_by_id( |
| 99 | State(state): State<AppState>, | 81 | State(state): State<AppState>, |
| 100 | ApiQuery(req): ApiQuery<GetByIdQueueQuery>, | 82 | ApiQuery(req): ApiQuery<GetQueueByIdQuery>, |
| 101 | ) -> ApiResult<Option<Queue>> { | 83 | ) -> ApiResult<Option<Queue>> { |
| 102 | Ok(SuccessResponse::ok( | 84 | Ok(SuccessResponse::ok( |
| 103 | queues::Entity::find_by_id(req.id) | 85 | queues::Entity::find_by_id(req.id) |
| @@ -109,10 +91,10 @@ async fn get_by_id( | |||
| 109 | 91 | ||
| 110 | #[utoipa::path( | 92 | #[utoipa::path( |
| 111 | get, | 93 | get, |
| 112 | path = "/get/owner", | 94 | path = "/get/by_owner", |
| 113 | tag = QUEUE, | 95 | tag = QUEUE, |
| 114 | summary = "Get by owner", | 96 | summary = "Get by owner", |
| 115 | description = "Get queues for a given owner", | 97 | description = "Get queues by the owner id", |
| 116 | params(GetByOwnerIdQuery), | 98 | params(GetByOwnerIdQuery), |
| 117 | responses( | 99 | responses( |
| 118 | ( | 100 | ( |
| @@ -126,7 +108,7 @@ async fn get_by_owner( | |||
| 126 | State(state): State<AppState>, | 108 | State(state): State<AppState>, |
| 127 | ApiQuery(req): ApiQuery<GetByOwnerIdQuery>, | 109 | ApiQuery(req): ApiQuery<GetByOwnerIdQuery>, |
| 128 | ) -> ApiResult<Vec<Queue>> { | 110 | ) -> ApiResult<Vec<Queue>> { |
| 129 | return Ok(SuccessResponse::ok( | 111 | Ok(SuccessResponse::ok( |
| 130 | queues::Entity::find() | 112 | queues::Entity::find() |
| 131 | .filter(queues::Column::OwnerId.eq(req.owner_id)) | 113 | .filter(queues::Column::OwnerId.eq(req.owner_id)) |
| 132 | .all(&state.db) | 114 | .all(&state.db) |
| @@ -134,7 +116,32 @@ async fn get_by_owner( | |||
| 134 | .into_iter() | 116 | .into_iter() |
| 135 | .map(Into::into) | 117 | .map(Into::into) |
| 136 | .collect(), | 118 | .collect(), |
| 137 | )); | 119 | )) |
| 120 | } | ||
| 121 | |||
| 122 | #[utoipa::path( | ||
| 123 | get, | ||
| 124 | path = "/get/owned", | ||
| 125 | tag = QUEUE, | ||
| 126 | summary = "Get owned", | ||
| 127 | description = "Get your queues", | ||
| 128 | responses( | ||
| 129 | ( | ||
| 130 | status = 200, body = SuccessResponse<Vec<Queue>>, | ||
| 131 | description = "Success response with queues owned by you" | ||
| 132 | ), | ||
| 133 | GlobalResponses | ||
| 134 | ), | ||
| 135 | )] | ||
| 136 | async fn get_owned(State(state): State<AppState>, Auth(user): Auth) -> ApiResult<Vec<Queue>> { | ||
| 137 | Ok(SuccessResponse::ok( | ||
| 138 | user.find_related(queues::Entity) | ||
| 139 | .all(&state.db) | ||
| 140 | .await? | ||
| 141 | .into_iter() | ||
| 142 | .map(Into::into) | ||
| 143 | .collect(), | ||
| 144 | )) | ||
| 138 | } | 145 | } |
| 139 | 146 | ||
| 140 | #[utoipa::path( | 147 | #[utoipa::path( |
| @@ -171,7 +178,7 @@ async fn create( | |||
| 171 | } | 178 | } |
| 172 | 179 | ||
| 173 | #[utoipa::path( | 180 | #[utoipa::path( |
| 174 | put, | 181 | patch, |
| 175 | path = "/update/name", | 182 | path = "/update/name", |
| 176 | tag = QUEUE, | 183 | tag = QUEUE, |
| 177 | summary = "Change name", | 184 | summary = "Change name", |
| @@ -202,7 +209,7 @@ async fn update_name( | |||
| 202 | } | 209 | } |
| 203 | 210 | ||
| 204 | #[utoipa::path( | 211 | #[utoipa::path( |
| 205 | put, | 212 | patch, |
| 206 | path = "/update/owner", | 213 | path = "/update/owner", |
| 207 | tag = QUEUE, | 214 | tag = QUEUE, |
| 208 | summary = "Change owner", | 215 | summary = "Change owner", |
| @@ -269,6 +276,7 @@ pub fn router() -> OpenApiRouter<AppState> { | |||
| 269 | OpenApiRouter::new() | 276 | OpenApiRouter::new() |
| 270 | .routes(routes!(get_by_id)) | 277 | .routes(routes!(get_by_id)) |
| 271 | .routes(routes!(get_by_owner)) | 278 | .routes(routes!(get_by_owner)) |
| 279 | .routes(routes!(get_owned)) | ||
| 272 | .routes(routes!(create)) | 280 | .routes(routes!(create)) |
| 273 | .routes(routes!(update_name)) | 281 | .routes(routes!(update_name)) |
| 274 | .routes(routes!(update_owner)) | 282 | .routes(routes!(update_owner)) |
