Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
berinaniesh committed Oct 10, 2023
1 parent 16c6ea3 commit 08cd49f
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 39 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
name = "bible-api"
authors = ["Berin Aniesh <berinaniesh@gmail.com>"]
repository = "https://github.com/berinaniesh/bible-api"
version = "0.1.0"
edition = "2021"

Expand Down
1 change: 1 addition & 0 deletions format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
find src -type f -name \*.rs -exec ucf "{}" \;
22 changes: 16 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod models;
mod routes;
mod query_params;
mod routes;

use actix_cors::Cors;
use actix_web::middleware::Logger;
Expand All @@ -9,19 +9,30 @@ 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 {
pub pool: PgPool,
}

#[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();
Expand All @@ -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/"))
})
Expand Down
34 changes: 32 additions & 2 deletions src/models.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -12,7 +43,6 @@ pub struct Verse {
pub verse: String,
}


#[derive(Debug, Serialize, ToSchema)]
pub struct TranslationInfo {
pub name: String,
Expand Down Expand Up @@ -49,4 +79,4 @@ pub struct Book {
pub book_id: i32,
pub book_name: String,
pub testament: String,
}
}
2 changes: 1 addition & 1 deletion src/query_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ pub struct VerseFilter {
pub struct TranslationSelector {
pub translation: Option<String>,
pub tr: Option<String>,
}
}
46 changes: 16 additions & 30 deletions src/routes.rs
Original file line number Diff line number Diff line change
@@ -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<VerseFilter>, app_data: web::Data<AppData>) -> Vec<Verse> {
Expand Down Expand Up @@ -112,33 +112,13 @@ pub async fn query_verses(qp: web::Query<VerseFilter>, app_data: web::Data<AppDa
get,
path = "/",
responses(
(status = 200, description = "API healthy")
(status = 200, description = "API healthy", body = Hello)
)
)]
#[get("/")]
pub async fn home(app_data: web::Data<AppData>) -> 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<String> = 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(
Expand All @@ -155,7 +135,10 @@ pub async fn get_translations(app_data: web::Data<AppData>) -> HttpResponse {
}

#[get("/books")]
pub async fn get_books(qp: web::Query<TranslationSelector>, app_data: web::Data<AppData>) -> HttpResponse {
pub async fn get_books(
qp: web::Query<TranslationSelector>,
app_data: web::Data<AppData>,
) -> HttpResponse {
let mut ot = Vec::new();
let mut nt = Vec::new();
let mut translation_name = String::new();
Expand Down Expand Up @@ -271,7 +254,10 @@ pub async fn get_random_verse(
),
)]
#[get("/chaptercount/{book}")]
pub async fn get_chaptercount(app_data: web::Data<AppData>, path: web::Path<String>) -> HttpResponse {
pub async fn get_chaptercount(
app_data: web::Data<AppData>,
path: web::Path<String>,
) -> HttpResponse {
let book = path.into_inner();
let mut query_builder: QueryBuilder<Postgres> = QueryBuilder::new(
r#"SELECT COUNT(*) AS count FROM "Chapter" WHERE book_id=(SELECT id FROM "Book" WHERE name="#,
Expand Down Expand Up @@ -327,4 +313,4 @@ pub async fn get_translation_books(
)));
}
return HttpResponse::Ok().json(q);
}
}

0 comments on commit 08cd49f

Please sign in to comment.