Skip to content

Commit

Permalink
Merge pull request #191 from nyzd/main
Browse files Browse the repository at this point in the history
Bug fix
  • Loading branch information
al-abd authored Nov 14, 2024
2 parents 9db43b0 + e4c62a4 commit a117d01
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 63 deletions.
1 change: 0 additions & 1 deletion migrations/00000000000003_create_mushaf/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 0 additions & 2 deletions migrations/00000000000005_create_quran_surahs/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions migrations/2023-02-19-100955_create_quran_ayahs/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
48 changes: 28 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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),
),
),
)
})
Expand Down
11 changes: 4 additions & 7 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ pub struct QuranAyah {

pub ayah_number: i32,
pub sajdah: Option<String>,
pub is_bismillah: bool,
pub bismillah_text: Option<String>,

#[serde(skip_serializing)]
pub created_at: NaiveDateTime,
Expand All @@ -260,6 +262,8 @@ pub struct NewQuranAyah {
pub surah_id: i32,
pub ayah_number: i32,
pub sajdah: Option<String>,
pub is_bismillah: bool,
pub bismillah_text: Option<String>,
}

#[derive(Clone, Selectable, Identifiable, Associations, Queryable, PartialEq, Debug, Serialize)]
Expand Down Expand Up @@ -303,8 +307,6 @@ pub struct QuranSurah {
pub name: String,
pub period: Option<String>,
pub number: i32,
pub bismillah_status: bool,
pub bismillah_as_first_ayah: bool,
pub mushaf_id: i32,

pub name_pronunciation: Option<String>,
Expand All @@ -325,8 +327,6 @@ pub struct NewQuranSurah {
pub name: String,
pub period: Option<String>,
pub number: i32,
pub bismillah_status: bool,
pub bismillah_as_first_ayah: bool,
pub mushaf_id: i32,
pub name_pronunciation: Option<String>,
pub name_translation_phrase: Option<String>,
Expand All @@ -346,8 +346,6 @@ pub struct QuranMushaf {
pub name: Option<String>,
pub source: Option<String>,

pub bismillah_text: Option<String>,

#[serde(skip_serializing)]
pub created_at: NaiveDateTime,
#[serde(skip_serializing)]
Expand All @@ -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<String>,
}

#[derive(Deserialize, Serialize, Clone, Validate, Identifiable, Queryable, Debug, Selectable)]
Expand Down
4 changes: 4 additions & 0 deletions src/routers/quran/ayah/ayah_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub struct AyahWithText {
pub surah_uuid: String,
pub sajdah: Option<Sajdah>,
pub text: String,
pub is_bismillah: bool,
pub bismillah_text: Option<String>,
}

/// Add's a new ayah
Expand Down Expand Up @@ -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)?;
Expand Down
20 changes: 18 additions & 2 deletions src/routers/quran/ayah/ayah_edit.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,7 +13,8 @@ pub async fn ayah_edit(
pool: web::Data<DbPool>,
) -> 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();
Expand All @@ -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)?;

Expand Down
13 changes: 13 additions & 0 deletions src/routers/quran/ayah/ayah_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/routers/quran/ayah/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use uuid::Uuid;

use crate::{
filter::{Filters, Order},
Format,
AyahBismillah, Format,
};

#[derive(Deserialize, Serialize)]
Expand Down Expand Up @@ -64,6 +64,7 @@ pub struct AyahWithContent {
pub struct SimpleAyah {
pub ayah_number: i32,
pub sajdah: Option<Sajdah>,
pub bismillah: Option<AyahBismillah>,
}

#[derive(Deserialize, Clone)]
Expand Down
1 change: 0 additions & 1 deletion src/routers/quran/mushaf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub struct SimpleMushaf {
short_name: String,
name: String,
source: String,
bismillah_text: Option<String>,
}

#[derive(Deserialize)]
Expand Down
1 change: 0 additions & 1 deletion src/routers/quran/mushaf/mushaf_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
3 changes: 1 addition & 2 deletions src/routers/quran/mushaf/mushaf_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub async fn mushaf_edit(
pool: web::Data<DbPool>,
) -> 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,
};

Expand All @@ -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)?;

Expand Down
20 changes: 15 additions & 5 deletions src/routers/quran/surah/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
}

// 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<String>,
}

/// 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<String>,
pub bismillah: Option<AyahBismillah>,
}

/// it contains ayah info and the content
Expand Down Expand Up @@ -177,10 +191,8 @@ pub struct SingleSurahResponse {
pub names: Vec<SurahName>,
pub period: Option<String>,
pub number: i32,
pub bismillah_status: bool,
pub bismillah_as_first_ayah: bool,
pub bismillah_text: Option<String>,
pub number_of_ayahs: i64,
pub bismillah: Option<SurahBismillah>,
}

/// The response type for /surah
Expand All @@ -203,7 +215,5 @@ pub struct SimpleSurah {
pub name_transliteration: Option<String>,
pub period: Option<String>,
pub number: i32,
pub bismillah_status: bool,
pub bismillah_as_first_ayah: bool,
pub mushaf_uuid: Uuid,
}
2 changes: 0 additions & 2 deletions src/routers/quran/surah/surah_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 2 additions & 5 deletions src/routers/quran/surah/surah_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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),
Expand Down
Loading

0 comments on commit a117d01

Please sign in to comment.