Skip to content

Commit ed7eb6f

Browse files
authored
Merge pull request #76 from debatecore/75-fix-tournaments-list
[75] fix tournament list response being empty octet stream; docs, indentation
2 parents fe03997 + 4293e69 commit ed7eb6f

File tree

1 file changed

+27
-31
lines changed

1 file changed

+27
-31
lines changed

src/routes/tournament_routes.rs

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
use crate::{omni_error::OmniError, setup::AppState, tournament::{Tournament, TournamentPatch}, users::{permissions::Permission, TournamentUser, User}};
1+
use crate::{
2+
omni_error::OmniError,
3+
setup::AppState,
4+
tournament::{Tournament, TournamentPatch},
5+
users::{permissions::Permission, TournamentUser, User},
6+
};
27
use axum::{
38
extract::{Path, State},
49
http::{HeaderMap, StatusCode},
@@ -10,7 +15,6 @@ use tower_cookies::Cookies;
1015
use tracing::error;
1116
use uuid::Uuid;
1217

13-
1418
pub fn route() -> Router<AppState> {
1519
Router::new()
1620
.route("/tournament", get(get_tournaments).post(create_tournament))
@@ -23,22 +27,19 @@ pub fn route() -> Router<AppState> {
2327
}
2428

2529
/// Get a list of all tournaments
26-
///
30+
///
2731
/// This request only returns the tournaments the user is permitted to see.
2832
/// The user must be given any role within a tournament to see it.
2933
/// The infrastructure admin can see all tournaments
30-
#[utoipa::path(get, path = "/tournament",
34+
#[utoipa::path(get, path = "/tournament",
3135
responses(
3236
(
3337
status=200, description = "Ok",
3438
body=Vec<Tournament>,
3539
example=json!(get_tournaments_list_example())
3640
),
3741
(status=400, description = "Bad request"),
38-
(
39-
status=401,
40-
description = "The user is not permitted to list any tournaments, meaning they do not have any roles within any tournament."
41-
),
42+
(status=401, description = "Unauthorized; user auth not present or invalid"),
4243
(status=500, description = "Internal server error")
4344
),
4445
tag="tournament"
@@ -58,20 +59,17 @@ async fn get_tournaments(
5859
let roles = user.get_roles(tournament_id, pool).await?;
5960
let tournament_user = TournamentUser {
6061
user: user.clone(),
61-
roles
62+
roles,
6263
};
6364
if tournament_user.has_permission(Permission::ReadTournament) {
6465
visible_tournaments.push(tournament);
6566
}
6667
}
67-
if visible_tournaments.is_empty() {
68-
return Ok(vec![].into_response());
69-
}
7068
Ok(Json(visible_tournaments).into_response())
7169
}
7270

7371
/// Create a new tournament
74-
///
72+
///
7573
/// Available only to the infrastructure admin.
7674
#[utoipa::path(
7775
post,
@@ -80,14 +78,14 @@ async fn get_tournaments(
8078
responses
8179
(
8280
(
83-
status=200,
81+
status=200,
8482
description = "Tournament created successfully",
8583
body=Tournament,
8684
example=json!(get_tournament_example_with_id())
8785
),
8886
(status=400, description = "Bad request"),
8987
(
90-
status=401,
88+
status=401,
9189
description = "The user is not permitted to modify this tournament"
9290
),
9391
(status=404, description = "Tournament not found"),
@@ -112,9 +110,9 @@ async fn create_tournament(
112110
}
113111

114112
/// Get details of an existing tournament
115-
///
113+
///
116114
/// The user must be given any role within the tournament to use this endpoint.
117-
#[utoipa::path(get, path = "/tournament/{id}",
115+
#[utoipa::path(get, path = "/tournament/{id}",
118116
responses
119117
(
120118
(
@@ -124,7 +122,7 @@ async fn create_tournament(
124122
),
125123
(status=400, description = "Bad request"),
126124
(
127-
status=401,
125+
status=401,
128126
description = "The user is not permitted to read this tournament"
129127
),
130128
(status=404, description = "Tournament not found"),
@@ -154,9 +152,9 @@ async fn get_tournament_by_id(
154152
}
155153

156154
/// Patch an existing tournament
157-
///
155+
///
158156
/// Requires either the Organizer or Admin role.
159-
#[utoipa::path(patch, path = "/tournament/{id}",
157+
#[utoipa::path(patch, path = "/tournament/{id}",
160158
request_body=TournamentPatch,
161159
responses(
162160
(
@@ -166,7 +164,7 @@ async fn get_tournament_by_id(
166164
),
167165
(status=400, description = "Bad request"),
168166
(
169-
status=401,
167+
status=401,
170168
description = "The user is not permitted to modify this tournament"
171169
),
172170
(status=404, description = "Tournament not found"),
@@ -202,13 +200,12 @@ async fn patch_tournament_by_id(
202200
}
203201
}
204202

205-
206203
/// Delete an existing tournament.
207-
///
204+
///
208205
/// Available only to the tournament Organizers.
209206
/// This operation is only allowed when there are no resources
210207
/// referencing this tournament.
211-
#[utoipa::path(delete, path = "/tournament/{id}",
208+
#[utoipa::path(delete, path = "/tournament/{id}",
212209
responses(
213210
(status=204, description = "Tournament deleted successfully"),
214211
(status=400, description = "Bad request"),
@@ -237,16 +234,14 @@ async fn delete_tournament_by_id(
237234
let tournament = Tournament::get_by_id(id, pool).await?;
238235
match tournament.delete(pool).await {
239236
Ok(_) => Ok(StatusCode::NO_CONTENT.into_response()),
240-
Err(e) =>
241-
{
237+
Err(e) => {
242238
if e.is_sqlx_foreign_key_violation() {
243-
return Err(OmniError::DependentResourcesError)
244-
}
245-
else {
239+
return Err(OmniError::DependentResourcesError);
240+
} else {
246241
error!("Error deleting a tournament with id {id}: {e}");
247242
return Err(e)?;
248243
}
249-
},
244+
}
250245
}
251246
}
252247

@@ -275,5 +270,6 @@ fn get_tournaments_list_example() -> String {
275270
"shortened_name": "PND"
276271
}
277272
]
278-
"#.to_owned()
273+
"#
274+
.to_owned()
279275
}

0 commit comments

Comments
 (0)