Skip to content

Commit

Permalink
Add rival and song endpoints to API docs
Browse files Browse the repository at this point in the history
  • Loading branch information
RubberDuckShobe committed Nov 8, 2024
1 parent b84e96d commit dd4af47
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 26 deletions.
17 changes: 2 additions & 15 deletions src/api/players.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,7 @@ struct PlayerResponse {
),
responses(
(status = OK, description = "Success",
body = PlayerPublic, content_type = "application/json",
example = json!(r#"{
"id":1,
"username":"m1nt_",
"accountType":2,
"joinedAt":"+002023-05-23T18:56:24.726000000Z",
"avatarUrl":"https://avatars.steamstatic.com/d72c8ef0f183faf564b9407572d51751794acd15_full.jpg"}"#)),
body = PlayerPublic, content_type = "application/json"),
(status = NOT_FOUND, description = "Player not found", body = SimpleRouteErrorOutput, content_type = "application/json")
)
)]
Expand Down Expand Up @@ -73,14 +67,7 @@ async fn get_player(
method(get),
path = "/self",
responses(
(status = OK, description = "Success",
body = PlayerPublic, content_type = "application/json",
example = json!(r#"{
"id":1,
"username":"m1nt_",
"accountType":2,
"joinedAt":"+002023-05-23T18:56:24.726000000Z",
"avatarUrl":"https://avatars.steamstatic.com/d72c8ef0f183faf564b9407572d51751794acd15_full.jpg"}"#)),
(status = OK, description = "Success", body = PlayerPublic, content_type = "application/json"),
(status = UNAUTHORIZED, description = "Not logged in or invalid token", body = SimpleRouteErrorOutput, content_type = "application/json")
),
security(
Expand Down
20 changes: 16 additions & 4 deletions src/api/rivals.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use axum::{extract::State, routing::get, Json};
use axum::{extract::State, Json};
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use serde::Serialize;
use utoipa_axum::router::OpenApiRouter;
use utoipa::ToSchema;
use utoipa_axum::{router::OpenApiRouter, routes};

use crate::{
models::{players::Player, rivalries::RivalryView},
Expand All @@ -11,15 +12,26 @@ use crate::{
};

pub fn routes() -> OpenApiRouter<AppState> {
OpenApiRouter::new().route("/own", get(get_own_rivals))
OpenApiRouter::new().routes(routes!(get_own_rivals))
}

#[derive(Serialize)]
#[derive(Serialize, ToSchema)]
#[serde(rename_all = "camelCase")]
struct RivalryResponse {
rivalries: Vec<RivalryView>,
}

/// Get own rivals
#[utoipa::path(
method(get),
path = "/self",
responses(
(status = OK, description = "Success", body = RivalryResponse, content_type = "application/json")
),
security(
("token_jwt" = [])
))
]
async fn get_own_rivals(
State(state): State<AppState>,
claims: Claims,
Expand Down
23 changes: 18 additions & 5 deletions src/api/songs.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
use axum::{
extract::{Path, Query, State},
routing::get,
Json,
};
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use serde::{Deserialize, Serialize};
use utoipa_axum::router::OpenApiRouter;
use utoipa::ToSchema;
use utoipa_axum::{router::OpenApiRouter, routes};

use crate::{
models::{extra_song_info::ExtraSongInfo, songs::Song},
util::errors::RouteError,
util::errors::{RouteError, SimpleRouteErrorOutput},
AppState,
};

pub fn routes() -> OpenApiRouter<AppState> {
OpenApiRouter::new().route("/:id", get(get_song))
OpenApiRouter::new().routes(routes!(get_song))
}

#[derive(Serialize)]
#[derive(Serialize, ToSchema)]
#[serde(rename_all = "camelCase")]
struct SongResponse {
#[serde(flatten)]
Expand All @@ -34,6 +34,19 @@ struct GetSongParams {
with_extra_info: bool,
}

/// Get a song by ID
#[utoipa::path(
method(get),
path = "/{id}",
params(
("id" = i32, Path, description = "ID of song to get"),
("with_extra_info" = bool, Query, description = "Include extra info")
),
responses(
(status = OK, description = "Success", body = SongResponse, content_type = "application/json"),
(status = NOT_FOUND, description = "Song not found", body = SimpleRouteErrorOutput, content_type = "application/json")
)
)]
async fn get_song(
State(state): State<AppState>,
Path(id): Path<i32>,
Expand Down
2 changes: 2 additions & 0 deletions src/models/extra_song_info.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use diesel::prelude::*;
use diesel_async::{AsyncPgConnection, RunQueryDsl};
use serde::Serialize;
use utoipa::ToSchema;

use crate::schema::extra_song_info;

Expand All @@ -17,6 +18,7 @@ use crate::schema::extra_song_info;
Serialize,
Default,
AsChangeset,
ToSchema,
)]
#[diesel(belongs_to(super::songs::Song))]
#[diesel(table_name = extra_song_info, check_for_backend(diesel::pg::Pg))]
Expand Down
3 changes: 2 additions & 1 deletion src/models/rivalries.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use diesel::prelude::*;
use diesel_async::{AsyncPgConnection, RunQueryDsl};
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

use super::players::PlayerPublic;
use crate::{models::players::Player, schema::rivalries};
Expand Down Expand Up @@ -69,7 +70,7 @@ impl NewRivalry {
}
}

#[derive(Queryable, Deserialize, Serialize)]
#[derive(Queryable, Deserialize, Serialize, ToSchema)]
#[diesel(table_name = rivalries, check_for_backend(diesel::pg::Pg))]
#[serde(rename_all = "camelCase")]
pub struct RivalryView {
Expand Down
3 changes: 2 additions & 1 deletion src/models/songs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use diesel_async::{AsyncPgConnection, RunQueryDsl, SaveChangesDsl};
use fred::prelude::*;
use serde::Serialize;
use tracing::debug;
use utoipa::ToSchema;

use crate::{
models::{
Expand All @@ -13,7 +14,7 @@ use crate::{
schema::{extra_song_info, songs},
};

#[derive(Identifiable, Selectable, Queryable, Debug, Serialize)]
#[derive(Identifiable, Selectable, Queryable, Debug, Serialize, ToSchema)]
#[diesel(table_name = songs, check_for_backend(diesel::pg::Pg))]
#[diesel(primary_key(id))]
#[serde(rename_all = "camelCase")]
Expand Down

0 comments on commit dd4af47

Please sign in to comment.