-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmod.rs
More file actions
196 lines (178 loc) · 5.8 KB
/
Copy pathmod.rs
File metadata and controls
196 lines (178 loc) · 5.8 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
mod id;
use actix_session::Session;
use actix_web::{get, put, web, HttpResponse, Responder};
use serde::Serialize;
use sqlx::PgPool;
use utoipa::{OpenApi, ToSchema};
use crate::{
api::{ApiResult, LanguageParams},
database, Difficulty,
};
use crate::Language;
#[derive(OpenApi)]
#[openapi(
tags((name = "achievements")),
paths(get_achievements, put_achievements),
components(schemas(
Language,
Achievement,
UpdateAchievement,
))
)]
struct ApiDoc;
#[derive(Serialize, ToSchema)]
struct Achievement {
id: i32,
series: i32,
series_name: String,
name: String,
description: String,
currency: i32,
hidden: bool,
#[serde(skip_serializing_if = "Option::is_none")]
version: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
comment: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
reference: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
difficulty: Option<Difficulty>,
#[serde(skip_serializing_if = "Option::is_none")]
video: Option<String>,
gacha: bool,
timegated: Option<String>,
missable: bool,
impossible: bool,
#[serde(skip_serializing_if = "Option::is_none")]
set: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
related: Option<Vec<i32>>,
percent: f64,
}
impl From<database::achievements::DbAchievement> for Achievement {
fn from(db_achievement: database::achievements::DbAchievement) -> Self {
Achievement {
id: db_achievement.id,
series: db_achievement.series,
series_name: db_achievement.series_name.clone(),
name: db_achievement.name.clone(),
description: db_achievement.description.clone(),
currency: db_achievement.jades,
hidden: db_achievement.hidden,
version: db_achievement.version.clone(),
comment: db_achievement.comment.clone(),
reference: db_achievement.reference.clone(),
difficulty: db_achievement
.difficulty
.as_ref()
.map(|d| d.parse().unwrap()),
video: db_achievement.video.clone(),
gacha: db_achievement.gacha,
timegated: db_achievement.timegated,
missable: db_achievement.missable,
impossible: db_achievement.impossible,
set: db_achievement.set,
related: None,
percent: (!db_achievement.impossible)
.then_some(db_achievement.percent.unwrap_or_default())
.unwrap_or_default(),
}
}
}
pub fn openapi() -> utoipa::openapi::OpenApi {
let mut openapi = ApiDoc::openapi();
openapi.merge(id::openapi());
openapi
}
pub fn configure(cfg: &mut web::ServiceConfig) {
cfg.service(get_achievements)
.service(put_achievements)
.configure(id::configure);
}
#[utoipa::path(
tag = "achievements",
get,
path = "/api/achievements",
params(LanguageParams),
responses(
(status = 200, description = "[Achievement]", body = Vec<Achievement>),
)
)]
#[get("/api/achievements")]
async fn get_achievements(
language_params: web::Query<LanguageParams>,
pool: web::Data<PgPool>,
) -> ApiResult<impl Responder> {
let db_achievements = database::achievements::get_all(language_params.lang, &pool).await?;
let mut achievements = db_achievements
.into_iter()
.map(Achievement::from)
.collect::<Vec<_>>();
for achievement in &mut achievements {
if let Some(set) = achievement.set {
achievement.related = Some(
database::achievements::get_all_related_ids(achievement.id, set, &pool).await?,
);
}
}
Ok(HttpResponse::Ok().json(achievements))
}
#[derive(serde::Deserialize, ToSchema)]
struct UpdateAchievement {
id: i32,
#[serde(skip_serializing_if = "Option::is_none")]
version: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
comment: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
reference: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
difficulty: Option<Difficulty>,
#[serde(skip_serializing_if = "Option::is_none")]
video: Option<String>,
gacha: Option<bool>,
timegated: Option<String>,
missable: Option<bool>,
impossible: Option<bool>,
set: Option<i32>,
}
#[utoipa::path(
tag = "achievements",
put,
path = "/api/achievements",
responses(
(status = 200, description = "Updated Achievement", body = Vec<UpdateAchievement>),
)
)]
#[put("/api/achievements")]
async fn put_achievements(
session: Session,
achievements: web::Json<Vec<UpdateAchievement>>,
pool: web::Data<PgPool>,
) -> ApiResult<impl Responder> {
let admin = if let Ok(Some(username)) = session.get::<String>("username") {
database::admins::exists(&username, &pool).await?
} else {
false
};
if !admin {
return Ok(HttpResponse::Forbidden().finish());
}
for achievement in achievements.iter() {
let update_achievement = database::achievements::DbUpdateAchievement {
id: achievement.id,
version: achievement.version.clone(),
comment: achievement.comment.clone(),
reference: achievement.reference.clone(),
difficulty: achievement.difficulty.map(|d| d.to_string()),
video: achievement.video.clone(),
gacha: achievement.gacha,
timegated: achievement.timegated.clone(),
missable: achievement.missable,
impossible: achievement.impossible,
set: achievement.set,
};
database::achievements::update_achievement_by_id(&update_achievement, &pool).await?;
}
Ok(HttpResponse::Ok().finish())
}