aboutsummaryrefslogtreecommitdiff
path: root/src/routers/queue/access.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/routers/queue/access.rs')
-rw-r--r--src/routers/queue/access.rs257
1 files changed, 255 insertions, 2 deletions
diff --git a/src/routers/queue/access.rs b/src/routers/queue/access.rs
index 1cba0b1..1aee8da 100644
--- a/src/routers/queue/access.rs
+++ b/src/routers/queue/access.rs
@@ -1,7 +1,260 @@
1use utoipa_axum::router::OpenApiRouter; 1use axum::extract::State;
2use chrono::{DateTime, Utc};
3use entity::invite_tokens;
4use sea_orm::{ActiveModelTrait, ActiveValue::Set, IntoActiveModel, ModelTrait};
5use serde::Deserialize;
6use utoipa::{IntoParams, ToSchema};
7use utoipa_axum::{router::OpenApiRouter, routes};
2 8
3use crate::AppState; 9use crate::{
10 ApiResult, AppState, GlobalResponses, SuccessResponse,
11 extract::{ApiJson, ApiQuery, Auth},
12 models::InviteToken,
13 tags::INVITE_TOKEN,
14 util::{get_owned_invite_token, get_owned_queue},
15};
16
17#[derive(Deserialize, IntoParams)]
18#[into_params(parameter_in = Query)]
19struct GetInviteTokenByIdQuery {
20 #[param(example = 1)]
21 id: i64,
22}
23
24#[derive(Deserialize, IntoParams)]
25#[into_params(parameter_in = Query)]
26struct GetInviteTokenByQueueIdQuery {
27 #[param(example = 1)]
28 queue_id: i64,
29}
30
31#[derive(Deserialize, ToSchema)]
32#[schema(description = "Body of the create invite token request")]
33struct CreateInviteTokenRequest {
34 #[schema(examples(1))]
35 queue_id: i64,
36 #[schema(examples("For classmates", "Для однокурсников"))]
37 name: String,
38 expiration_date: Option<DateTime<Utc>>,
39}
40
41#[derive(Deserialize, ToSchema)]
42#[schema(description = "Body of the expire invite token request")]
43struct ExpireInviteTokenRequest {
44 #[schema(examples(1))]
45 id: i64,
46}
47
48#[derive(Deserialize, ToSchema)]
49#[schema(description = "Body of the change invite token expiration date request")]
50struct ChangeInviteTokenExpirationDateRequest {
51 #[schema(examples(1))]
52 id: i64,
53 #[schema(examples("2000-01-01 00:00:00Z", "2000-01-01 03:00:00+03:00", json!(null)))]
54 expiration_date: Option<DateTime<Utc>>,
55}
56
57#[derive(Deserialize, ToSchema)]
58#[schema(description = "Body of the delete invite token request")]
59struct DeleteInviteTokenRequest {
60 #[schema(examples(1))]
61 id: i64,
62}
63
64#[utoipa::path(
65 get,
66 path = "/get/by_id",
67 tag = INVITE_TOKEN,
68 summary = "Get by id",
69 description = "Get the invite token by id",
70 params(GetInviteTokenByIdQuery),
71 responses(
72 (
73 status = 200, body = SuccessResponse<InviteToken>,
74 description = "Success response with the requested invite token"
75 ),
76 GlobalResponses
77 ),
78 security(("auth" = [])),
79)]
80async fn get_by_id(
81 State(state): State<AppState>,
82 Auth(user): Auth,
83 ApiQuery(req): ApiQuery<GetInviteTokenByIdQuery>,
84) -> ApiResult<InviteToken> {
85 Ok(SuccessResponse::ok(
86 get_owned_invite_token(req.id, user.id, &state.db)
87 .await?
88 .into(),
89 ))
90}
91
92#[utoipa::path(
93 get,
94 path = "/get/by_queue_id",
95 tag = INVITE_TOKEN,
96 summary = "Get by queue id",
97 description = "Get the invite token by the queue id",
98 params(GetInviteTokenByQueueIdQuery),
99 responses(
100 (
101 status = 200, body = SuccessResponse<Vec<InviteToken>>,
102 description = "Success response with the requested invite tokens"
103 ),
104 GlobalResponses
105 ),
106 security(("auth" = [])),
107)]
108async fn get_by_queue_id(
109 State(state): State<AppState>,
110 Auth(user): Auth,
111 ApiQuery(req): ApiQuery<GetInviteTokenByQueueIdQuery>,
112) -> ApiResult<Vec<InviteToken>> {
113 let queue = get_owned_queue(req.queue_id, user.id, &state.db).await?;
114
115 Ok(SuccessResponse::ok(
116 queue
117 .find_related(invite_tokens::Entity)
118 .all(&state.db)
119 .await?
120 .into_iter()
121 .map(Into::into)
122 .collect(),
123 ))
124}
125
126#[utoipa::path(
127 post,
128 path = "/create",
129 tag = INVITE_TOKEN,
130 summary = "Create",
131 description = "Create a new invite token",
132 request_body = CreateInviteTokenRequest,
133 responses(
134 (
135 status = 200, body = SuccessResponse<InviteToken>,
136 description = "Success response with the created invite token"
137 ),
138 GlobalResponses
139 ),
140 security(("auth" = [])),
141)]
142async fn create(
143 State(state): State<AppState>,
144 Auth(user): Auth,
145 ApiJson(req): ApiJson<CreateInviteTokenRequest>,
146) -> ApiResult<InviteToken> {
147 let queue = get_owned_queue(req.queue_id, user.id, &state.db).await?;
148
149 Ok(SuccessResponse::ok(
150 invite_tokens::ActiveModel {
151 token: Set(uuid::Uuid::new_v4()),
152 queue_id: Set(queue.id),
153 name: Set(req.name),
154 expiration_date: Set(req.expiration_date.as_ref().map(DateTime::naive_utc)),
155 ..Default::default()
156 }
157 .insert(&state.db)
158 .await?
159 .into(),
160 ))
161}
162
163#[utoipa::path(
164 patch,
165 path = "/expire",
166 tag = INVITE_TOKEN,
167 summary = "Expire",
168 description = "Expire the invite token",
169 request_body = ExpireInviteTokenRequest,
170 responses(
171 (
172 status = 200, body = SuccessResponse<InviteToken>,
173 description = "Success response with the changed invite token data"
174 ),
175 GlobalResponses
176 ),
177 security(("auth" = [])),
178)]
179async fn expire(
180 State(state): State<AppState>,
181 Auth(user): Auth,
182 ApiJson(req): ApiJson<ExpireInviteTokenRequest>,
183) -> ApiResult<InviteToken> {
184 let mut active_invite_token = get_owned_invite_token(req.id, user.id, &state.db)
185 .await?
186 .into_active_model();
187
188 active_invite_token.expiration_date = Set(Some(Utc::now().naive_utc()));
189
190 let invite_token = active_invite_token.update(&state.db).await?;
191 Ok(SuccessResponse::ok(invite_token.into()))
192}
193
194#[utoipa::path(
195 patch,
196 path = "/update/expiration_date",
197 tag = INVITE_TOKEN,
198 summary = "Change expiration date",
199 description = "Change invite token expiration date",
200 request_body = ChangeInviteTokenExpirationDateRequest,
201 responses(
202 (
203 status = 200, body = SuccessResponse<InviteToken>,
204 description = "Success response with the changed invite token data"
205 ),
206 GlobalResponses
207 ),
208 security(("auth" = [])),
209)]
210async fn update_expiration_date(
211 State(state): State<AppState>,
212 Auth(user): Auth,
213 ApiJson(req): ApiJson<ChangeInviteTokenExpirationDateRequest>,
214) -> ApiResult<InviteToken> {
215 let mut active_invite_token = get_owned_invite_token(req.id, user.id, &state.db)
216 .await?
217 .into_active_model();
218
219 active_invite_token.expiration_date =
220 Set(req.expiration_date.as_ref().map(DateTime::naive_utc));
221
222 let invite_token = active_invite_token.update(&state.db).await?;
223 Ok(SuccessResponse::ok(invite_token.into()))
224}
225
226#[utoipa::path(
227 delete,
228 path = "/delete",
229 tag = INVITE_TOKEN,
230 summary = "Delete",
231 description = "Delete the invite token",
232 request_body = DeleteInviteTokenRequest,
233 responses(
234 (
235 status = 200, body = SuccessResponse<InviteToken>,
236 description = "Success response with the deleted invite token data"
237 ),
238 GlobalResponses
239 ),
240 security(("auth" = [])),
241)]
242async fn delete(
243 State(state): State<AppState>,
244 Auth(user): Auth,
245 ApiJson(req): ApiJson<DeleteInviteTokenRequest>,
246) -> ApiResult<InviteToken> {
247 let invite_token = get_owned_invite_token(req.id, user.id, &state.db).await?;
248 invite_token.clone().delete(&state.db).await?;
249 Ok(SuccessResponse::ok(invite_token.into()))
250}
4 251
5pub fn router() -> OpenApiRouter<AppState> { 252pub fn router() -> OpenApiRouter<AppState> {
6 OpenApiRouter::new() 253 OpenApiRouter::new()
254 .routes(routes!(get_by_id))
255 .routes(routes!(get_by_queue_id))
256 .routes(routes!(create))
257 .routes(routes!(expire))
258 .routes(routes!(update_expiration_date))
259 .routes(routes!(delete))
7} 260}