diff --git a/Cargo.toml b/Cargo.toml index 750e945..aa274f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [package] name = "bible-api" authors = ["Berin Aniesh "] +repository = "https://github.com/berinaniesh/bible-api" version = "0.1.0" edition = "2021" diff --git a/format.sh b/format.sh new file mode 100755 index 0000000..f06b54e --- /dev/null +++ b/format.sh @@ -0,0 +1 @@ +find src -type f -name \*.rs -exec ucf "{}" \; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index abc68cb..026809c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ mod models; -mod routes; mod query_params; +mod routes; use actix_cors::Cors; use actix_web::middleware::Logger; @@ -9,8 +9,8 @@ use sqlx::postgres::{PgPool, PgPoolOptions}; use utoipa::OpenApi; use utoipa_swagger_ui::SwaggerUi; -use crate::routes::*; use crate::models::*; +use crate::routes::*; #[derive(Debug, Clone)] pub struct AppData { @@ -18,10 +18,21 @@ pub struct AppData { } #[derive(OpenApi)] -#[openapi(paths(home, get_translations, get_verses, get_random_verse, get_abbreviations, get_translation_info, get_translation_books, get_chaptercount), components(schemas(TranslationInfo, Verse, Book, Count)))] +#[openapi( + paths( + home, + get_translations, + get_verses, + get_random_verse, + get_abbreviations, + get_translation_info, + get_translation_books, + get_chaptercount + ), + components(schemas(Hello, TranslationInfo, Verse, Book, Count)) +)] struct ApiDoc; - #[tokio::main] async fn main() -> std::io::Result<()> { let db_url = dotenvy::var("DATABASE_URL").unwrap(); @@ -46,8 +57,7 @@ async fn main() -> std::io::Result<()> { .service(routes::get_chaptercount) .service(routes::get_random_verse) .service( - SwaggerUi::new("/docs/{_:.*}") - .url("/api-docs/openapi.json", ApiDoc::openapi()), + SwaggerUi::new("/docs/{_:.*}").url("/api-docs/openapi.json", ApiDoc::openapi()), ) .service(web::redirect("/docs", "/docs/")) }) diff --git a/src/models.rs b/src/models.rs index d6c1b6e..2deb2ca 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,7 +1,38 @@ use serde::Serialize; use sqlx::FromRow; +use std::env; use utoipa::ToSchema; +#[derive(Debug, Serialize, ToSchema)] +pub struct Hello { + pub greeting: String, + pub name: String, + pub about: String, + pub docs: String, + pub repository: String, + pub author: String, +} + +impl Hello { + pub fn default() -> Self { + let greeting = String::from("Hello there"); + let name = String::from("Bible API"); + let about = String::from("REST API to serve bible verses"); + let docs = String::from("/docs"); + let repository = String::from(env!("CARGO_PKG_REPOSITORY")); + let author = String::from(env!("CARGO_PKG_AUTHORS")); + + return Hello { + greeting: greeting, + name: name, + about: about, + docs: docs, + repository: repository, + author: author, + }; + } +} + #[derive(Debug, Clone, Serialize, FromRow, ToSchema)] pub struct Verse { pub translation: String, @@ -12,7 +43,6 @@ pub struct Verse { pub verse: String, } - #[derive(Debug, Serialize, ToSchema)] pub struct TranslationInfo { pub name: String, @@ -49,4 +79,4 @@ pub struct Book { pub book_id: i32, pub book_name: String, pub testament: String, -} \ No newline at end of file +} diff --git a/src/query_params.rs b/src/query_params.rs index efdf0a8..4c0d539 100644 --- a/src/query_params.rs +++ b/src/query_params.rs @@ -27,4 +27,4 @@ pub struct VerseFilter { pub struct TranslationSelector { pub translation: Option, pub tr: Option, -} \ No newline at end of file +} diff --git a/src/routes.rs b/src/routes.rs index f2ddf2c..f51a4e8 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,11 +1,11 @@ -use serde_json::json; +use actix_web::{get, web, HttpResponse}; use rand::{thread_rng, Rng}; +use serde_json::json; use sqlx::{Postgres, QueryBuilder}; -use actix_web::{get, web, HttpResponse}; use crate::models::*; -use crate::AppData; use crate::query_params::*; +use crate::AppData; #[allow(unused_assignments)] pub async fn query_verses(qp: web::Query, app_data: web::Data) -> Vec { @@ -112,33 +112,13 @@ pub async fn query_verses(qp: web::Query, app_data: web::Data) -> HttpResponse { - let t = sqlx::query_as!( - TranslationName, - r#"SELECT name from "Translation" order by id"# - ) - .fetch_all(&app_data.pool) - .await - .unwrap(); - let mut translations: Vec = Vec::new(); - for i in t.iter() { - translations.push(i.name.clone()); - } - - HttpResponse::Ok().json( - json!({ - "About": "REST API to serve bible verses", - "Repository": "https://github.com/berinaniesh/bible-api", - "Endpoints": ["/translations", "/verses", "/abbreviations", "/books"], - "ParametersForVerses": ["translation or tr", "book or b", "abbreviation or ab", "chapter or ch", "startchapter or sch", "endchapter or ech", "verse or v", "startverse or sv", "endverse or ev"], - "ParametersForBooks": ["translation"], - "Examples": ["/verses?translation=tovbsi&book=1+Samuel&abbreviation=1SA&chapter=1&verse=10", "/verses?tr=kjv&ab=jhn&ch=1&v=1"] - }), - ) +pub async fn home() -> HttpResponse { + let hello = Hello::default(); + return HttpResponse::Ok().json(hello); } #[utoipa::path( @@ -155,7 +135,10 @@ pub async fn get_translations(app_data: web::Data) -> HttpResponse { } #[get("/books")] -pub async fn get_books(qp: web::Query, app_data: web::Data) -> HttpResponse { +pub async fn get_books( + qp: web::Query, + app_data: web::Data, +) -> HttpResponse { let mut ot = Vec::new(); let mut nt = Vec::new(); let mut translation_name = String::new(); @@ -271,7 +254,10 @@ pub async fn get_random_verse( ), )] #[get("/chaptercount/{book}")] -pub async fn get_chaptercount(app_data: web::Data, path: web::Path) -> HttpResponse { +pub async fn get_chaptercount( + app_data: web::Data, + path: web::Path, +) -> HttpResponse { let book = path.into_inner(); let mut query_builder: QueryBuilder = QueryBuilder::new( r#"SELECT COUNT(*) AS count FROM "Chapter" WHERE book_id=(SELECT id FROM "Book" WHERE name="#, @@ -327,4 +313,4 @@ pub async fn get_translation_books( ))); } return HttpResponse::Ok().json(q); -} \ No newline at end of file +}