-
Notifications
You must be signed in to change notification settings - Fork 10
/
application.rs
162 lines (155 loc) · 4.68 KB
/
application.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use actix_web::{delete, get, http::StatusCode, post, web, Responder, Scope};
use sea_orm::{prelude::*, Condition};
use crate::{
database::entity::applications,
internal::auth::{auth_role, Auth},
models::application::*,
services::{
application::ApplicationService, prelude::DataService, ToMessageResponse, ToPageResponse,
ToResponse,
},
};
pub fn get_routes() -> Scope {
web::scope("/application")
.service(list)
.service(info)
.service(create)
.service(delete)
.service(token)
}
/// Get token by application ID
/// - Minimum required role: `user`
/// - Allow unverified users: `false`
/// - Application token allowed: `false`
#[utoipa::path(
context_path = "/api/application",
tag = "application",
responses(
(status = 200, body = TokenResponse),
(status = 404, body = MessageResponse, description = "Application not found")
),
params(
("application_id" = str, Path, description = "Application ID to get token for"),
),
security(("apiKey" = [])),
)]
#[get("/{application_id}/token")]
async fn token(
service: web::Data<ApplicationService>,
application_id: web::Path<String>,
user: Auth<auth_role::User>,
) -> impl Responder {
service
.generate_token(&application_id, Some(&user.id))
.await
.to_response::<TokenResponse>(StatusCode::OK)
}
/// Get all applications
/// - Minimum required role: `user`
/// - Allow unverified users: `false`
/// - Application token allowed: `false`
#[utoipa::path(
context_path = "/api/application",
tag = "application",
responses((status = 200, body = ApplicationPage)),
params(
("page_number" = u64, Path, description = "Page to get applications by (starts at 1)"),
),
security(("apiKey" = [])),
)]
#[get("/list/{page_number}")]
async fn list(
service: web::Data<ApplicationService>,
page_number: web::Path<usize>,
user: Auth<auth_role::User>,
) -> impl Responder {
service
.get_page(
*page_number,
5,
Some(Condition::any().add(applications::Column::UserId.eq(user.id.to_owned()))),
)
.await
.to_page_response::<ApplicationData>(StatusCode::OK)
}
/// Get token info
/// - Minimum required role: `user`
/// - Allow unverified users: `false`
/// - Application token allowed: `false`
#[utoipa::path(
context_path = "/api/application",
tag = "application",
responses(
(status = 200, body = ApplicationData),
(status = 401, body = MessageResponse)
),
security(("apiKey" = [])),
)]
#[get("/{application_id}")]
async fn info(
service: web::Data<ApplicationService>,
user: Auth<auth_role::User>,
application_id: web::Path<String>,
) -> impl Responder {
service
.by_condition(
Condition::all()
.add(applications::Column::UserId.eq(user.id.to_owned()))
.add(applications::Column::Id.eq(application_id.to_owned())),
)
.await
.to_response::<ApplicationData>(StatusCode::OK)
}
/// Create an application
/// - Minimum required role: `user`
/// - Allow unverified users: `false`
/// - Application token allowed: `false`
#[utoipa::path(
context_path = "/api/application",
tag = "application",
responses(
(status = 200, body = ApplicationData),
(status = 400, body = MessageResponse, description = "Token limit reached or invalid name"),
),
request_body = ApplicationCreate,
security(("apiKey" = [])),
)]
#[post("")]
async fn create(
service: web::Data<ApplicationService>,
user: Auth<auth_role::User>,
form: web::Json<ApplicationCreate>,
) -> impl Responder {
service
.create_application(&user.id, &form.name)
.await
.to_response::<ApplicationData>(StatusCode::OK)
}
/// Delete an application
/// - Minimum required role: `user`
/// - Allow unverified users: `false`
/// - Application token allowed: `false`
#[utoipa::path(
context_path = "/api/application",
tag = "application",
responses(
(status = 200, body = MessageResponse, description = "Application was deleted"),
(status = 401, body = MessageResponse, description = "Unauthorized or token does not exist"),
),
security(("apiKey" = [])),
)]
#[delete("/{application_id}")]
async fn delete(
service: web::Data<ApplicationService>,
user: Auth<auth_role::User>,
application_id: web::Path<String>,
) -> impl Responder {
service
.delete(
application_id.to_string(),
false,
Some(Condition::all().add(applications::Column::UserId.eq(user.id.to_owned()))),
)
.await
.to_message_response(StatusCode::OK)
}