diff --git a/migrations/00000000000003_create_mushaf/up.sql b/migrations/00000000000003_create_mushaf/up.sql index ab7201a..04aba83 100644 --- a/migrations/00000000000003_create_mushaf/up.sql +++ b/migrations/00000000000003_create_mushaf/up.sql @@ -5,7 +5,6 @@ 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 c775f90..6172691 100644 --- a/migrations/00000000000005_create_quran_surahs/up.sql +++ b/migrations/00000000000005_create_quran_surahs/up.sql @@ -5,8 +5,6 @@ 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 89e0803..3383d46 100644 --- a/migrations/2023-02-19-100955_create_quran_ayahs/up.sql +++ b/migrations/2023-02-19-100955_create_quran_ayahs/up.sql @@ -5,6 +5,8 @@ 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 78efd78..90a46b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -204,19 +204,24 @@ async fn main() -> std::io::Result<()> { .route(web::delete().to(translation_delete::translation_delete)), ) .service( - 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), - ), - ), + 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), + ), + ), ), ) .service( @@ -332,15 +337,18 @@ 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)) - .route("/{language}", web::post().to(edit_phrase::edit_phrase)) - .route( - "/{language}", - web::delete().to(delete_phrase::delete_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), + ), ), ) }) diff --git a/src/models.rs b/src/models.rs index abdaecc..08825c8 100644 --- a/src/models.rs +++ b/src/models.rs @@ -246,6 +246,8 @@ 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, @@ -260,6 +262,8 @@ 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)] @@ -303,8 +307,6 @@ 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, @@ -325,8 +327,6 @@ 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,8 +346,6 @@ pub struct QuranMushaf { pub name: Option, pub source: Option, - pub bismillah_text: Option, - #[serde(skip_serializing)] pub created_at: NaiveDateTime, #[serde(skip_serializing)] @@ -361,7 +359,6 @@ 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 63dab9c..4dc1653 100644 --- a/src/routers/quran/ayah/ayah_add.rs +++ b/src/routers/quran/ayah/ayah_add.rs @@ -15,6 +15,8 @@ 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 @@ -59,6 +61,8 @@ 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 7ab2cc4..5d827bc 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::DbPool; +use crate::{AyahBismillah, DbPool}; use actix_web::web; use diesel::prelude::*; use uuid::Uuid; @@ -13,7 +13,8 @@ pub async fn ayah_edit( pool: web::Data, ) -> Result<&'static str, RouterError> { use crate::schema::quran_ayahs::dsl::{ - ayah_number, quran_ayahs, sajdah as ayah_sajdah, uuid as ayah_uuid, + ayah_number, bismillah_text, is_bismillah, quran_ayahs, sajdah as ayah_sajdah, + uuid as ayah_uuid, }; let new_ayah = new_ayah.into_inner(); @@ -28,6 +29,21 @@ 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 2383b6c..f01d70b 100644 --- a/src/routers/quran/ayah/ayah_list.rs +++ b/src/routers/quran/ayah/ayah_list.rs @@ -2,6 +2,7 @@ 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, @@ -44,6 +45,18 @@ 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 928c075..153e087 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}, - Format, + AyahBismillah, Format, }; #[derive(Deserialize, Serialize)] @@ -64,6 +64,7 @@ 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 b24a287..f56ce83 100644 --- a/src/routers/quran/mushaf/mod.rs +++ b/src/routers/quran/mushaf/mod.rs @@ -22,7 +22,6 @@ 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 b8b146e..1c0f741 100644 --- a/src/routers/quran/mushaf/mushaf_add.rs +++ b/src/routers/quran/mushaf/mushaf_add.rs @@ -31,7 +31,6 @@ 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 834fa23..7e9fb51 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::{ - bismillah_text, quran_mushafs, name as mushaf_name, short_name as mushaf_short_name, + name as mushaf_name, quran_mushafs, short_name as mushaf_short_name, source as mushaf_source, uuid as mushaf_uuid, }; @@ -28,7 +28,6 @@ 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 1bc51b8..812d545 100644 --- a/src/routers/quran/surah/mod.rs +++ b/src/routers/quran/surah/mod.rs @@ -62,12 +62,26 @@ 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 @@ -177,10 +191,8 @@ 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 @@ -203,7 +215,5 @@ 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 58e3232..3aa3f0a 100644 --- a/src/routers/quran/surah/surah_add.rs +++ b/src/routers/quran/surah/surah_add.rs @@ -61,8 +61,6 @@ 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 d8144a8..c5b3735 100644 --- a/src/routers/quran/surah/surah_edit.rs +++ b/src/routers/quran/surah/surah_edit.rs @@ -14,9 +14,8 @@ 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::{ - 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, + 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(); @@ -37,8 +36,6 @@ 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 958d341..43c6dad 100644 --- a/src/routers/quran/surah/surah_view.rs +++ b/src/routers/quran/surah/surah_view.rs @@ -1,8 +1,10 @@ -use super::{Format, GetSurahQuery, QuranResponseData, SimpleAyah, SingleSurahResponse}; +use super::{ + AyahBismillah, Format, GetSurahQuery, QuranResponseData, SimpleAyah, SingleSurahResponse, +}; use crate::models::{QuranAyah, QuranMushaf, QuranSurah}; use crate::routers::multip; use crate::{error::RouterError, DbPool}; -use crate::{AyahTy, SingleSurahMushaf, SurahName}; +use crate::{AyahTy, SingleSurahMushaf, SurahBismillah, SurahName}; use actix_web::web; use diesel::prelude::*; use uuid::Uuid; @@ -39,6 +41,18 @@ 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 @@ -62,12 +76,6 @@ 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(); @@ -84,10 +92,28 @@ 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, @@ -97,9 +123,6 @@ 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 fc2ac9c..659f5eb 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -4,6 +4,7 @@ diesel::table! { app_accounts (id) { id -> Int4, uuid -> Uuid, + #[max_length = 30] username -> Varchar, account_type -> Text, } @@ -38,11 +39,13 @@ 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, @@ -63,7 +66,9 @@ diesel::table! { uuid -> Uuid, creator_user_id -> Int4, account_id -> Int4, + #[max_length = 300] name -> Varchar, + #[max_length = 8] language -> Varchar, } } @@ -76,6 +81,7 @@ diesel::table! { owner_account_id -> Int4, profile_image -> Nullable, established_date -> Date, + #[max_length = 11] national_id -> Varchar, created_at -> Timestamptz, updated_at -> Timestamptz, @@ -88,7 +94,9 @@ 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, @@ -101,7 +109,9 @@ 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, @@ -114,6 +124,7 @@ diesel::table! { uuid -> Uuid, phrase_id -> Int4, text -> Text, + #[max_length = 3] language -> Varchar, created_at -> Timestamptz, updated_at -> Timestamptz, @@ -134,6 +145,7 @@ diesel::table! { app_tokens (id) { id -> Int4, account_id -> Int4, + #[max_length = 64] token_hash -> Varchar, terminated -> Bool, terminated_by_id -> Int4, @@ -148,8 +160,11 @@ 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, } } @@ -160,6 +175,7 @@ diesel::table! { account_id -> Int4, birthday -> Nullable, profile_image -> Nullable, + #[max_length = 4] language -> Nullable, created_at -> Timestamptz, updated_at -> Timestamptz, @@ -184,7 +200,10 @@ 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, } @@ -195,10 +214,12 @@ 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, } @@ -209,11 +230,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, @@ -230,8 +251,10 @@ 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,