Skip to content

Commit

Permalink
return empty array if the length of search string is less than 3
Browse files Browse the repository at this point in the history
  • Loading branch information
berinaniesh committed Jul 15, 2024
1 parent d6866e2 commit 95032bd
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 71 deletions.
16 changes: 14 additions & 2 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,23 @@ pub const BOOKS: [&'static str; 66] = [
"Revelation",
];

pub const ABBREVIATIONS: [&'static str; 66] = ["GEN","EXO","LEV","NUM","DEU","JOS","JDG","RUT","1SA","2SA","1KI","2KI","1CH","2CH","EZR","NEH","EST","JOB","PSA","PRO","ECC","SNG","ISA","JER","LAM","EZK","DAN","HOS","JOL","AMO","OBA","JON","MIC","NAM","HAB","ZEP","HAG","ZEC","MAL","MAT","MRK","LUK","JHN","ACT","ROM","1CO","2CO","GAL","EPH","PHP","COL","1TH","2TH","1TI","2TI","TIT","PHM","HEB","JAM","1PE","2PE","1JN","2JN","3JN","JUD","REV"];
pub const ABBREVIATIONS: [&'static str; 66] = [
"GEN", "EXO", "LEV", "NUM", "DEU", "JOS", "JDG", "RUT", "1SA", "2SA", "1KI", "2KI", "1CH",
"2CH", "EZR", "NEH", "EST", "JOB", "PSA", "PRO", "ECC", "SNG", "ISA", "JER", "LAM", "EZK",
"DAN", "HOS", "JOL", "AMO", "OBA", "JON", "MIC", "NAM", "HAB", "ZEP", "HAG", "ZEC", "MAL",
"MAT", "MRK", "LUK", "JHN", "ACT", "ROM", "1CO", "2CO", "GAL", "EPH", "PHP", "COL", "1TH",
"2TH", "1TI", "2TI", "TIT", "PHM", "HEB", "JAM", "1PE", "2PE", "1JN", "2JN", "3JN", "JUD",
"REV",
];

pub const CHAPTERCOUNT: [u8; 66] = [
50, 40, 27, 36, 34, 24, 21, 4, 31, 24, 22, 25, 29, 36, 10, 13, 10, 42, 150, 31, 12, 8, 66, 52,
5, 48, 12, 14, 3, 9, 1, 4, 7, 3, 3, 3, 2, 14, 4, 28, 16, 24, 21, 28, 16, 16, 13, 6, 6, 4, 4, 5,
3, 6, 4, 3, 1, 13, 5, 5, 3, 5, 1, 1, 1, 22,
];
pub const VERSECOUNT: [u16; 66] = [1533, 1213, 859, 1288, 959, 658, 618, 85, 810, 695, 816, 719, 942, 822, 280, 406, 167, 1070, 2461, 915, 222, 117, 1292, 1364, 154, 1273, 357, 197, 73, 146, 21, 48, 105, 47, 56, 53, 38, 211, 55, 1071, 678, 1151, 879, 1007, 433, 437, 257, 149, 155, 104, 95, 89, 47, 113, 83, 46, 25, 303, 108, 105, 61, 105, 13, 14, 25, 404];
pub const VERSECOUNT: [u16; 66] = [
1533, 1213, 859, 1288, 959, 658, 618, 85, 810, 695, 816, 719, 942, 822, 280, 406, 167, 1070,
2461, 915, 222, 117, 1292, 1364, 154, 1273, 357, 197, 73, 146, 21, 48, 105, 47, 56, 53, 38,
211, 55, 1071, 678, 1151, 879, 1007, 433, 437, 257, 149, 155, 104, 95, 89, 47, 113, 83, 46, 25,
303, 108, 105, 61, 105, 13, 14, 25, 404,
];
36 changes: 12 additions & 24 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use thiserror::Error;
use actix_web::error::ResponseError;
use actix_web::http::StatusCode;
use actix_web::HttpResponse;
use sqlx::Error as SqlxError;
use serde_json::json;
use sqlx::Error as SqlxError;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum AppError {
Expand All @@ -17,39 +17,27 @@ pub enum AppError {
impl ResponseError for AppError {
fn error_response(&self) -> HttpResponse {
match self {
AppError::NotFound => {
HttpResponse::NotFound().json(json!({
"message": self.to_string()
}))
}
AppError::InternalServerError => {
HttpResponse::InternalServerError().json(json!({
"message": self.to_string()
}))
}
AppError::NotFound => HttpResponse::NotFound().json(json!({
"message": self.to_string()
})),
AppError::InternalServerError => HttpResponse::InternalServerError().json(json!({
"message": self.to_string()
})),
}
}
fn status_code(&self) -> StatusCode {
match *self {
AppError::NotFound => {
StatusCode::NOT_FOUND
}
AppError::InternalServerError => {
StatusCode::INTERNAL_SERVER_ERROR
}
AppError::NotFound => StatusCode::NOT_FOUND,
AppError::InternalServerError => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}

impl From<SqlxError> for AppError {
fn from(err: SqlxError) -> Self {
match err {
SqlxError::RowNotFound | SqlxError::ColumnNotFound(_) => {
AppError::NotFound
}
_ => {
AppError::InternalServerError
}
SqlxError::RowNotFound | SqlxError::ColumnNotFound(_) => AppError::NotFound,
_ => AppError::InternalServerError,
}
}
}
19 changes: 16 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod constants;
mod error;
mod models;
mod routes;
mod error;
mod constants;

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -37,7 +37,20 @@ pub struct AppData {
search,
get_next_page,
),
components(schemas(Hello, TranslationInfo, Verse, VerseFilter, TranslationSelector, Book, Count, SearchParameters, PageIn, PageOut, PrevNext, BooksChapterCount))
components(schemas(
Hello,
TranslationInfo,
Verse,
VerseFilter,
TranslationSelector,
Book,
Count,
SearchParameters,
PageIn,
PageOut,
PrevNext,
BooksChapterCount
))
)]
struct ApiDoc;

Expand Down
19 changes: 11 additions & 8 deletions src/models.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use serde::{Serialize, Deserialize};
use crate::constants::{ABBREVIATIONS, BOOKS, CHAPTERCOUNT, TOTAL, VERSECOUNT};
use serde::{Deserialize, Serialize};
use sqlx::{FromRow, Type};
use std::fmt;
use std::env;
use utoipa::{ToSchema, IntoParams};
use crate::constants::{TOTAL, BOOKS, ABBREVIATIONS, CHAPTERCOUNT, VERSECOUNT};
use std::fmt;
use utoipa::{IntoParams, ToSchema};

#[derive(Debug, Deserialize, Serialize, Clone, Copy, Type)]
#[sqlx(type_name = "testament")]
Expand All @@ -18,7 +18,7 @@ impl fmt::Display for Testament {
Self::OldTestament => {
//fmt::Debug::fmt("Old", f)
write!(f, "Old Testament")
},
}
Self::NewTestament => {
//fmt::Debug::fmt("New", f)
write!(f, "New Testament")
Expand Down Expand Up @@ -171,8 +171,6 @@ pub struct PrevNext {
pub next: Option<PageOut>,
}



#[derive(Debug, Serialize, ToSchema)]
pub struct BooksChapterCount {
pub book: String,
Expand All @@ -185,7 +183,12 @@ impl BooksChapterCount {
pub fn default() -> Vec<BooksChapterCount> {
let mut ans: Vec<BooksChapterCount> = Vec::with_capacity(TOTAL as usize);
for i in 0..TOTAL as usize {
let temp = BooksChapterCount {book: String::from(BOOKS[i]), abbreviation: String::from(ABBREVIATIONS[i]), chapters: CHAPTERCOUNT[i], verses: VERSECOUNT[i]};
let temp = BooksChapterCount {
book: String::from(BOOKS[i]),
abbreviation: String::from(ABBREVIATIONS[i]),
chapters: CHAPTERCOUNT[i],
verses: VERSECOUNT[i],
};
ans.push(temp)
}
return ans;
Expand Down
Loading

0 comments on commit 95032bd

Please sign in to comment.