diff options
author | niliara-edu <nil.jimeno@estudiant.fjaverianas.com> | 2025-01-26 00:27:45 +0100 |
---|---|---|
committer | niliara-edu <nil.jimeno@estudiant.fjaverianas.com> | 2025-01-26 00:27:45 +0100 |
commit | e3f0f700bf2de3b8e7d7c72893a4d6cb128f000d (patch) | |
tree | a7262e9c07ebb0541b0a948cb215fe33ef0246c5 /src | |
parent | 57a2df34f4986f1f6062f22804021925afec0419 (diff) |
update
Diffstat (limited to 'src')
-rw-r--r-- | src/api/search_results.rs | 18 | ||||
-rw-r--r-- | src/database/artist.rs | 5 | ||||
-rw-r--r-- | src/database/search_results.rs | 15 | ||||
-rw-r--r-- | src/database/song.rs | 3 | ||||
-rw-r--r-- | src/main.rs | 44 |
5 files changed, 50 insertions, 35 deletions
diff --git a/src/api/search_results.rs b/src/api/search_results.rs index 8f01e56..9ccb013 100644 --- a/src/api/search_results.rs +++ b/src/api/search_results.rs @@ -7,7 +7,6 @@ use serde::Deserialize; #[derive(Deserialize)] struct SearchQueryOptions { - id: Option<String>, name: Option<String>, } @@ -16,23 +15,6 @@ pub async fn search_results( app_state: web::Data<AppState>, get_args: web::Query<SearchQueryOptions>, ) -> HttpResponse { - let default: String = String::default(); - - if get_args.id.is_some() { - let id: &str = get_args.id.as_ref().unwrap_or(&default); - let search_attempt: ( - sqlx::Result<Option<Artist>, sqlx::Error>, - sqlx::Result<Option<Album>, sqlx::Error>, - sqlx::Result<Option<Song>, sqlx::Error>, - ) = { app_state.database.search_results_by_id(id).await }; - - return HttpResponse::Ok().json(( - search_attempt.0.unwrap_or(None), - search_attempt.1.unwrap_or(None), - search_attempt.2.unwrap_or(None), - )); - }; - let search_attempt: ( sqlx::Result<Vec<Artist>, sqlx::Error>, sqlx::Result<Vec<Album>, sqlx::Error>, diff --git a/src/database/artist.rs b/src/database/artist.rs index 018bc17..5dda7a8 100644 --- a/src/database/artist.rs +++ b/src/database/artist.rs @@ -1,10 +1,13 @@ use crate::database::DatabaseWrapper; use serde::{Deserialize, Serialize}; use sqlx::mysql::MySqlQueryResult; +use utoipa::ToSchema; -#[derive(Serialize)] +#[derive(Serialize, ToSchema)] pub struct Artist { + #[schema(example = "Attempt", required = true)] name: Option<String>, + #[schema(example = 3, required = true)] id: Option<i32>, } diff --git a/src/database/search_results.rs b/src/database/search_results.rs index 1468111..027d160 100644 --- a/src/database/search_results.rs +++ b/src/database/search_results.rs @@ -4,21 +4,6 @@ use crate::database::artist::Artist; use crate::database::album::Album; impl DatabaseWrapper { - pub async fn search_results_by_id( - &self, - id: &str, - ) -> ( - Result<Option<Artist>, sqlx::Error>, - Result<Option<Album>, sqlx::Error>, - Result<Option<Song>, sqlx::Error>, - ) { - ( - self.select_artist_by_id(id).await, - self.select_album_by_id(id).await, - self.select_song_by_id(id).await, - ) - } - pub async fn search_results_by_name( &self, name: &str, diff --git a/src/database/song.rs b/src/database/song.rs index 2b45065..45c217b 100644 --- a/src/database/song.rs +++ b/src/database/song.rs @@ -1,8 +1,9 @@ use crate::database::DatabaseWrapper; use serde::{Deserialize, Serialize}; use sqlx::mysql::MySqlQueryResult; +use utoipa::ToSchema; -#[derive(Serialize)] +#[derive(Serialize, ToSchema)] pub struct Song { name: Option<String>, id: Option<i32>, diff --git a/src/main.rs b/src/main.rs index 0151c47..595723a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,14 @@ use dotenv::dotenv; use std::env; use std::sync::Arc; +use utoipa::{ + openapi::security::{HttpAuthScheme, HttpBuilder, SecurityScheme}, + Modify, OpenApi, ToSchema, +}; + +use utoipa_swagger_ui::SwaggerUi; +use database::artist::Artist; + #[derive(Clone)] struct AppState { database: Arc<database::DatabaseWrapper>, @@ -40,11 +48,47 @@ async fn main() -> std::io::Result<()> { secret: jwt_secret, }; + /* utoipa setup */ + #[derive(OpenApi)] + #[openapi( + paths( + ), + components( + schemas( + Artist + ) + ), + modifiers(&SecurityAddon) + )] + struct ApiDoc; + + struct SecurityAddon; + impl Modify for SecurityAddon { + fn modify(&self, openapi : &mut utoipa::openapi::OpenApi) { + let components = openapi.components.as_mut().unwrap(); + components.add_security_scheme( + "bearer_auth", + SecurityScheme::Http( + HttpBuilder::new() + .scheme(HttpAuthScheme::Bearer) + .bearer_format("JWT") + .build() + ), + ); + } + } + + let openapi = ApiDoc::openapi(); + /* main server setup */ HttpServer::new(move || { App::new() .app_data(web::Data::new(app_state.clone())) .route("/", web::get().to(root)) + .service(SwaggerUi::new("/docs/{_:.*}").url( + "/docs/openapi.json", + openapi.clone(), + )) .service(api::api_scope()) .service(auth::auth_scope()) }) |