diff --git a/migrations/00000000000003_create_mushaf/up.sql b/migrations/00000000000003_create_mushaf/up.sql index 04aba83..ab7201a 100644 --- a/migrations/00000000000003_create_mushaf/up.sql +++ b/migrations/00000000000003_create_mushaf/up.sql @@ -5,6 +5,7 @@ CREATE TABLE quran_mushafs ( short_name VARCHAR(200), name VARCHAR(400), source VARCHAR(300), + bismillah_text TEXT, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), CONSTRAINT mushaf_fk_user_id_rel FOREIGN KEY(creator_user_id) REFERENCES app_users(id), diff --git a/migrations/00000000000005_create_quran_surahs/up.sql b/migrations/00000000000005_create_quran_surahs/up.sql index 6172691..c775f90 100644 --- a/migrations/00000000000005_create_quran_surahs/up.sql +++ b/migrations/00000000000005_create_quran_surahs/up.sql @@ -5,6 +5,8 @@ CREATE TABLE quran_surahs ( name VARCHAR(50) NOT NULL, period VARCHAR(50), number serial NOT NULL, + bismillah_status BOOLEAN NOT NULL, + bismillah_as_first_ayah BOOLEAN NOT NULL, mushaf_id serial NOT NULL, name_pronunciation TEXT, name_translation_phrase TEXT, diff --git a/migrations/2023-02-19-100955_create_quran_ayahs/up.sql b/migrations/2023-02-19-100955_create_quran_ayahs/up.sql index 3383d46..89e0803 100644 --- a/migrations/2023-02-19-100955_create_quran_ayahs/up.sql +++ b/migrations/2023-02-19-100955_create_quran_ayahs/up.sql @@ -5,8 +5,6 @@ CREATE TABLE quran_ayahs ( surah_id serial NOT NULL, ayah_number serial NOT NULL, sajdah VARCHAR(20), - is_bismillah BOOLEAN NOT NULL, - bismillah_text TEXT, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), CONSTRAINT quran_ayahs_id PRIMARY KEY (id), diff --git a/src/main.rs b/src/main.rs index 90a46b4..78efd78 100644 --- a/src/main.rs +++ b/src/main.rs @@ -204,24 +204,19 @@ async fn main() -> std::io::Result<()> { .route(web::delete().to(translation_delete::translation_delete)), ) .service( - web::scope("/text") - .route( - "/{translation_uuid}", - web::get().to(translation_text_view::translation_text_view), - ) - .service( - web::resource("/{translation_uuid}") - .wrap(AuthZ::new(auth_z_controller.clone())) - .wrap(TokenAuth::new(user_id_from_token.clone(), true)) - .route( - web::post() - .to(translation_text_modify::translation_text_modify), - ) - .route( - web::delete() - .to(translation_text_delete::translation_text_delete), - ), - ), + web::scope("/text").service( + web::resource("/{translation_uuid}") + .wrap(TokenAuth::new(user_id_from_token.clone(), false)) + .route(web::get().to(translation_text_view::translation_text_view)) + .route( + web::post() + .to(translation_text_modify::translation_text_modify), + ) + .route( + web::delete() + .to(translation_text_delete::translation_text_delete), + ), + ), ), ) .service( @@ -337,18 +332,15 @@ async fn main() -> std::io::Result<()> { ) .service( web::scope("/phrase") + .wrap(AuthZ::new(auth_z_controller.clone())) + .wrap(TokenAuth::new(user_id_from_token.clone(), true)) .route("", web::get().to(phrase_list::list_phrase)) + .route("", web::post().to(add_phrase::add_phrase)) .route("/{language}", web::get().to(view_phrase::view_phrase)) - .service( - web::scope("") - .wrap(AuthZ::new(auth_z_controller.clone())) - .wrap(TokenAuth::new(user_id_from_token.clone(), true)) - .route("", web::post().to(add_phrase::add_phrase)) - .route("/{language}", web::post().to(edit_phrase::edit_phrase)) - .route( - "/{language}", - web::delete().to(delete_phrase::delete_phrase), - ), + .route("/{language}", web::post().to(edit_phrase::edit_phrase)) + .route( + "/{language}", + web::delete().to(delete_phrase::delete_phrase), ), ) }) diff --git a/src/models.rs b/src/models.rs index 08825c8..abdaecc 100644 --- a/src/models.rs +++ b/src/models.rs @@ -246,8 +246,6 @@ pub struct QuranAyah { pub ayah_number: i32, pub sajdah: Option, - pub is_bismillah: bool, - pub bismillah_text: Option, #[serde(skip_serializing)] pub created_at: NaiveDateTime, @@ -262,8 +260,6 @@ pub struct NewQuranAyah { pub surah_id: i32, pub ayah_number: i32, pub sajdah: Option, - pub is_bismillah: bool, - pub bismillah_text: Option, } #[derive(Clone, Selectable, Identifiable, Associations, Queryable, PartialEq, Debug, Serialize)] @@ -307,6 +303,8 @@ pub struct QuranSurah { pub name: String, pub period: Option, pub number: i32, + pub bismillah_status: bool, + pub bismillah_as_first_ayah: bool, pub mushaf_id: i32, pub name_pronunciation: Option, @@ -327,6 +325,8 @@ pub struct NewQuranSurah { pub name: String, pub period: Option, pub number: i32, + pub bismillah_status: bool, + pub bismillah_as_first_ayah: bool, pub mushaf_id: i32, pub name_pronunciation: Option, pub name_translation_phrase: Option, @@ -346,6 +346,8 @@ pub struct QuranMushaf { pub name: Option, pub source: Option, + pub bismillah_text: Option, + #[serde(skip_serializing)] pub created_at: NaiveDateTime, #[serde(skip_serializing)] @@ -359,6 +361,7 @@ pub struct NewQuranMushaf<'a> { pub short_name: Option<&'a str>, pub name: Option<&'a str>, pub source: Option<&'a str>, + pub bismillah_text: Option, } #[derive(Deserialize, Serialize, Clone, Validate, Identifiable, Queryable, Debug, Selectable)] diff --git a/src/routers/quran/ayah/ayah_add.rs b/src/routers/quran/ayah/ayah_add.rs index 4dc1653..63dab9c 100644 --- a/src/routers/quran/ayah/ayah_add.rs +++ b/src/routers/quran/ayah/ayah_add.rs @@ -15,8 +15,6 @@ pub struct AyahWithText { pub surah_uuid: String, pub sajdah: Option, pub text: String, - pub is_bismillah: bool, - pub bismillah_text: Option, } /// Add's a new ayah @@ -61,8 +59,6 @@ pub async fn ayah_add( sajdah: new_ayah.sajdah.map(|sajdah| sajdah.to_string()), ayah_number: (latest_ayah_number + 1) as i32, creator_user_id: user, - is_bismillah: new_ayah.is_bismillah, - bismillah_text: new_ayah.bismillah_text, } .insert_into(quran_ayahs) .get_result(&mut conn)?; diff --git a/src/routers/quran/ayah/ayah_edit.rs b/src/routers/quran/ayah/ayah_edit.rs index 5d827bc..7ab2cc4 100644 --- a/src/routers/quran/ayah/ayah_edit.rs +++ b/src/routers/quran/ayah/ayah_edit.rs @@ -1,5 +1,5 @@ use crate::error::RouterError; -use crate::{AyahBismillah, DbPool}; +use crate::DbPool; use actix_web::web; use diesel::prelude::*; use uuid::Uuid; @@ -13,8 +13,7 @@ pub async fn ayah_edit( pool: web::Data, ) -> Result<&'static str, RouterError> { use crate::schema::quran_ayahs::dsl::{ - ayah_number, bismillah_text, is_bismillah, quran_ayahs, sajdah as ayah_sajdah, - uuid as ayah_uuid, + ayah_number, quran_ayahs, sajdah as ayah_sajdah, uuid as ayah_uuid, }; let new_ayah = new_ayah.into_inner(); @@ -29,21 +28,6 @@ pub async fn ayah_edit( .set(( ayah_number.eq(new_ayah.ayah_number), ayah_sajdah.eq(new_sajdah), - is_bismillah.eq(new_ayah - .bismillah - .clone() - .unwrap_or(AyahBismillah { - is_ayah: false, - text: None, - }) - .is_ayah), - bismillah_text.eq(new_ayah - .bismillah - .unwrap_or(AyahBismillah { - is_ayah: false, - text: None, - }) - .text), )) .execute(&mut conn)?; diff --git a/src/routers/quran/ayah/ayah_list.rs b/src/routers/quran/ayah/ayah_list.rs index f01d70b..2383b6c 100644 --- a/src/routers/quran/ayah/ayah_list.rs +++ b/src/routers/quran/ayah/ayah_list.rs @@ -2,7 +2,6 @@ use crate::error::{RouterError, RouterErrorDetailBuilder}; use crate::filter::Filter; use crate::models::QuranAyah; use crate::routers::multip; -use crate::AyahBismillah; use crate::{ routers::quran::surah::{AyahTy, Format, SimpleAyah}, DbPool, @@ -45,18 +44,6 @@ pub async fn ayah_list( number: a.ayah_number as u32, uuid: a.uuid, sajdah: a.sajdah, - bismillah: match (a.is_bismillah, a.bismillah_text) { - (true, None) => Some(AyahBismillah { - is_ayah: true, - text: None, - }), - (false, Some(text)) => Some(AyahBismillah { - is_ayah: false, - text: Some(text), - }), - (false, None) => None, - (_, _) => None, - }, }); let final_ayahs = ayahs_as_map diff --git a/src/routers/quran/ayah/mod.rs b/src/routers/quran/ayah/mod.rs index 153e087..928c075 100644 --- a/src/routers/quran/ayah/mod.rs +++ b/src/routers/quran/ayah/mod.rs @@ -11,7 +11,7 @@ use uuid::Uuid; use crate::{ filter::{Filters, Order}, - AyahBismillah, Format, + Format, }; #[derive(Deserialize, Serialize)] @@ -64,7 +64,6 @@ pub struct AyahWithContent { pub struct SimpleAyah { pub ayah_number: i32, pub sajdah: Option, - pub bismillah: Option, } #[derive(Deserialize, Clone)] diff --git a/src/routers/quran/mushaf/mod.rs b/src/routers/quran/mushaf/mod.rs index f56ce83..b24a287 100644 --- a/src/routers/quran/mushaf/mod.rs +++ b/src/routers/quran/mushaf/mod.rs @@ -22,6 +22,7 @@ pub struct SimpleMushaf { short_name: String, name: String, source: String, + bismillah_text: Option, } #[derive(Deserialize)] diff --git a/src/routers/quran/mushaf/mushaf_add.rs b/src/routers/quran/mushaf/mushaf_add.rs index 1c0f741..b8b146e 100644 --- a/src/routers/quran/mushaf/mushaf_add.rs +++ b/src/routers/quran/mushaf/mushaf_add.rs @@ -31,6 +31,7 @@ pub async fn mushaf_add( creator_user_id: user, name: Some(&new_mushaf.name), source: Some(&new_mushaf.source), + bismillah_text: new_mushaf.bismillah_text, } .insert_into(quran_mushafs) .execute(&mut conn)?; diff --git a/src/routers/quran/mushaf/mushaf_edit.rs b/src/routers/quran/mushaf/mushaf_edit.rs index 7e9fb51..834fa23 100644 --- a/src/routers/quran/mushaf/mushaf_edit.rs +++ b/src/routers/quran/mushaf/mushaf_edit.rs @@ -13,7 +13,7 @@ pub async fn mushaf_edit( pool: web::Data, ) -> Result<&'static str, RouterError> { use crate::schema::quran_mushafs::dsl::{ - name as mushaf_name, quran_mushafs, short_name as mushaf_short_name, + bismillah_text, quran_mushafs, name as mushaf_name, short_name as mushaf_short_name, source as mushaf_source, uuid as mushaf_uuid, }; @@ -28,6 +28,7 @@ pub async fn mushaf_edit( mushaf_name.eq(new_mushaf.name), mushaf_short_name.eq(new_mushaf.short_name), mushaf_source.eq(new_mushaf.source), + bismillah_text.eq(new_mushaf.bismillah_text), )) .execute(&mut conn)?; diff --git a/src/routers/quran/surah/mod.rs b/src/routers/quran/surah/mod.rs index 812d545..1bc51b8 100644 --- a/src/routers/quran/surah/mod.rs +++ b/src/routers/quran/surah/mod.rs @@ -62,26 +62,12 @@ impl PartialOrd for SimpleAyahSurah { } } -#[derive(Hash, Ord, PartialOrd, PartialEq, Eq, Serialize, Clone, Debug, Deserialize)] -pub struct AyahBismillah { - pub is_ayah: bool, - pub text: Option, -} - -// This will be returned in /surah/{uuid} router -#[derive(Hash, Ord, PartialOrd, PartialEq, Eq, Serialize, Clone, Debug, Deserialize)] -pub struct SurahBismillah { - pub as_first_ayah: bool, - pub text: Option, -} - /// The Ayah type that will return in the response #[derive(Hash, Ord, PartialOrd, PartialEq, Eq, Serialize, Clone, Debug)] pub struct SimpleAyah { pub number: u32, pub uuid: Uuid, pub sajdah: Option, - pub bismillah: Option, } /// it contains ayah info and the content @@ -191,8 +177,10 @@ pub struct SingleSurahResponse { pub names: Vec, pub period: Option, pub number: i32, + pub bismillah_status: bool, + pub bismillah_as_first_ayah: bool, + pub bismillah_text: Option, pub number_of_ayahs: i64, - pub bismillah: Option, } /// The response type for /surah @@ -215,5 +203,7 @@ pub struct SimpleSurah { pub name_transliteration: Option, pub period: Option, pub number: i32, + pub bismillah_status: bool, + pub bismillah_as_first_ayah: bool, pub mushaf_uuid: Uuid, } diff --git a/src/routers/quran/surah/surah_add.rs b/src/routers/quran/surah/surah_add.rs index 3aa3f0a..58e3232 100644 --- a/src/routers/quran/surah/surah_add.rs +++ b/src/routers/quran/surah/surah_add.rs @@ -61,6 +61,8 @@ pub async fn surah_add( period: new_surah.period, number: (latest_surah_number + 1) as i32, mushaf_id: mushaf, + bismillah_status: new_surah.bismillah_status, + bismillah_as_first_ayah: new_surah.bismillah_as_first_ayah, name_pronunciation: new_surah.name_pronunciation, name_translation_phrase: new_surah.name_translation_phrase, name_transliteration: new_surah.name_transliteration, diff --git a/src/routers/quran/surah/surah_edit.rs b/src/routers/quran/surah/surah_edit.rs index c5b3735..d8144a8 100644 --- a/src/routers/quran/surah/surah_edit.rs +++ b/src/routers/quran/surah/surah_edit.rs @@ -14,8 +14,9 @@ pub async fn surah_edit( ) -> Result<&'static str, RouterError> { use crate::schema::quran_mushafs::dsl::{id as mushaf_id, quran_mushafs, uuid as mushaf_uuid}; use crate::schema::quran_surahs::dsl::{ - mushaf_id as surah_mushaf_id, name, name_pronunciation, name_translation_phrase, - name_transliteration, number, period, quran_surahs, uuid as surah_uuid, + bismillah_as_first_ayah, bismillah_status, mushaf_id as surah_mushaf_id, name, + name_pronunciation, name_translation_phrase, name_transliteration, number, period, + quran_surahs, uuid as surah_uuid, }; let new_surah = new_surah.into_inner(); @@ -36,6 +37,8 @@ pub async fn surah_edit( number.eq(new_surah.number), surah_mushaf_id.eq(mushaf), name.eq(new_surah.name), + bismillah_status.eq(new_surah.bismillah_status), + bismillah_as_first_ayah.eq(new_surah.bismillah_as_first_ayah), period.eq(new_surah.period), name_pronunciation.eq(new_surah.name_pronunciation), name_translation_phrase.eq(new_surah.name_translation_phrase), diff --git a/src/routers/quran/surah/surah_view.rs b/src/routers/quran/surah/surah_view.rs index 43c6dad..958d341 100644 --- a/src/routers/quran/surah/surah_view.rs +++ b/src/routers/quran/surah/surah_view.rs @@ -1,10 +1,8 @@ -use super::{ - AyahBismillah, Format, GetSurahQuery, QuranResponseData, SimpleAyah, SingleSurahResponse, -}; +use super::{Format, GetSurahQuery, QuranResponseData, SimpleAyah, SingleSurahResponse}; use crate::models::{QuranAyah, QuranMushaf, QuranSurah}; use crate::routers::multip; use crate::{error::RouterError, DbPool}; -use crate::{AyahTy, SingleSurahMushaf, SurahBismillah, SurahName}; +use crate::{AyahTy, SingleSurahMushaf, SurahName}; use actix_web::web; use diesel::prelude::*; use uuid::Uuid; @@ -41,18 +39,6 @@ pub async fn surah_view( number: ayah.ayah_number as u32, uuid: ayah.uuid, sajdah: ayah.sajdah, - bismillah: match (ayah.is_bismillah, ayah.bismillah_text) { - (true, None) => Some(AyahBismillah { - is_ayah: true, - text: None, - }), - (false, Some(text)) => Some(AyahBismillah { - is_ayah: false, - text: Some(text), - }), - (false, None) => None, - (_, _) => None, - }, }); let final_ayahs = ayahs_as_map @@ -76,6 +62,12 @@ pub async fn surah_view( .filter(mushaf_id.eq(surah.mushaf_id)) .get_result::(&mut conn)?; + let mushaf_bismillah_text = if surah.bismillah_as_first_ayah { + None + } else { + mushaf.bismillah_text.clone() // this is Option + }; + let translation = if let Some(ref phrase) = surah.name_translation_phrase { let mut p = app_phrases.left_join(app_phrase_translations).into_boxed(); @@ -92,28 +84,10 @@ pub async fn surah_view( None }; - // TODO: remove unwrap. - let (first_ayah_text, first_ayah_bismillah) = match final_ayahs.first().unwrap() { - AyahTy::Text(a) => (a.text.clone(), a.ayah.bismillah.clone()), - AyahTy::Words(a) => (a.words.join(" "), a.ayah.bismillah.clone()), - }; - - let surah_bismillah = first_ayah_bismillah.and_then(|bismillah| { - Some(SurahBismillah { - as_first_ayah: bismillah.is_ayah, - text: if bismillah.is_ayah { - Some(first_ayah_text) - } else { - bismillah.text - }, - }) - }); - Ok(web::Json(QuranResponseData { surah: SingleSurahResponse { uuid: surah.uuid, mushaf: SingleSurahMushaf::from(mushaf), - bismillah: surah_bismillah, names: vec![SurahName { arabic: surah.name, translation, @@ -123,6 +97,9 @@ pub async fn surah_view( }], period: surah.period, number: surah.number, + bismillah_status: surah.bismillah_status, + bismillah_as_first_ayah: surah.bismillah_as_first_ayah, + bismillah_text: mushaf_bismillah_text, number_of_ayahs: final_ayahs.len() as i64, }, ayahs: final_ayahs, diff --git a/src/schema.rs b/src/schema.rs index 659f5eb..fc2ac9c 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -4,7 +4,6 @@ diesel::table! { app_accounts (id) { id -> Int4, uuid -> Uuid, - #[max_length = 30] username -> Varchar, account_type -> Text, } @@ -39,13 +38,11 @@ diesel::table! { app_error_logs (id) { id -> Int4, uuid -> Uuid, - #[max_length = 256] error_name -> Varchar, status_code -> Int4, message -> Text, detail -> Nullable, account_id -> Nullable, - #[max_length = 64] request_token -> Nullable, request_user_agent -> Nullable, request_ipv4 -> Cidr, @@ -66,9 +63,7 @@ diesel::table! { uuid -> Uuid, creator_user_id -> Int4, account_id -> Int4, - #[max_length = 300] name -> Varchar, - #[max_length = 8] language -> Varchar, } } @@ -81,7 +76,6 @@ diesel::table! { owner_account_id -> Int4, profile_image -> Nullable, established_date -> Date, - #[max_length = 11] national_id -> Varchar, created_at -> Timestamptz, updated_at -> Timestamptz, @@ -94,9 +88,7 @@ diesel::table! { uuid -> Uuid, creator_user_id -> Int4, permission_id -> Int4, - #[max_length = 450] name -> Varchar, - #[max_length = 255] value -> Varchar, created_at -> Timestamptz, updated_at -> Timestamptz, @@ -109,9 +101,7 @@ diesel::table! { uuid -> Uuid, creator_user_id -> Int4, account_id -> Int4, - #[max_length = 255] object -> Varchar, - #[max_length = 255] action -> Varchar, created_at -> Timestamptz, updated_at -> Timestamptz, @@ -124,7 +114,6 @@ diesel::table! { uuid -> Uuid, phrase_id -> Int4, text -> Text, - #[max_length = 3] language -> Varchar, created_at -> Timestamptz, updated_at -> Timestamptz, @@ -145,7 +134,6 @@ diesel::table! { app_tokens (id) { id -> Int4, account_id -> Int4, - #[max_length = 64] token_hash -> Varchar, terminated -> Bool, terminated_by_id -> Int4, @@ -160,11 +148,8 @@ diesel::table! { account_id -> Int4, creator_user_id -> Int4, primary_name -> Bool, - #[max_length = 100] first_name -> Varchar, - #[max_length = 200] last_name -> Varchar, - #[max_length = 4] language -> Varchar, } } @@ -175,7 +160,6 @@ diesel::table! { account_id -> Int4, birthday -> Nullable, profile_image -> Nullable, - #[max_length = 4] language -> Nullable, created_at -> Timestamptz, updated_at -> Timestamptz, @@ -200,10 +184,7 @@ diesel::table! { creator_user_id -> Int4, surah_id -> Int4, ayah_number -> Int4, - #[max_length = 20] sajdah -> Nullable, - is_bismillah -> Bool, - bismillah_text -> Nullable, created_at -> Timestamptz, updated_at -> Timestamptz, } @@ -214,12 +195,10 @@ diesel::table! { id -> Int4, uuid -> Uuid, creator_user_id -> Int4, - #[max_length = 200] short_name -> Nullable, - #[max_length = 400] name -> Nullable, - #[max_length = 300] source -> Nullable, + bismillah_text -> Nullable, created_at -> Timestamptz, updated_at -> Timestamptz, } @@ -230,11 +209,11 @@ diesel::table! { id -> Int4, uuid -> Uuid, creator_user_id -> Int4, - #[max_length = 50] name -> Varchar, - #[max_length = 50] period -> Nullable, number -> Int4, + bismillah_status -> Bool, + bismillah_as_first_ayah -> Bool, mushaf_id -> Int4, name_pronunciation -> Nullable, name_translation_phrase -> Nullable, @@ -251,10 +230,8 @@ diesel::table! { mushaf_id -> Int4, creator_user_id -> Int4, translator_account_id -> Int4, - #[max_length = 5] language -> Varchar, release_date -> Nullable, - #[max_length = 300] source -> Nullable, approved -> Bool, bismillah -> Text,