Skip to content

Commit

Permalink
Merge pull request #4 from samudradev/lemma-mod
Browse files Browse the repository at this point in the history
⚙ FIX: Add Delete Konsep logic
  • Loading branch information
Thaza-Kun committed Jan 31, 2024
2 parents b869b57 + bdd7c90 commit 9d02de7
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 13 deletions.
103 changes: 98 additions & 5 deletions src-tauri/database/src/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ where
}
Ok(())
}

pub fn empty() -> AttachmentMod<A> {
AttachmentMod {
attached: vec![],
detached: vec![],
modified: vec![],
}
}
}

impl<A: ItemMod, I: Item<IntoMod = A>> From<Vec<I>> for AttachmentMod<A> {
Expand Down Expand Up @@ -151,14 +159,14 @@ impl CompareAttachable<KonsepItem, KonsepItemMod> for LemmaItem {
.collect_vec()
}
fn find_modified(&self, other: &Vec<KonsepItem>) -> Vec<KonsepItemMod> {
let detached = self
let detached_id = self
.find_detached(other)
.iter()
.map(KonsepItem::partial_from_mod)
.map(|item| item.id)
.collect_vec();
let old = Vec::clone(&self.items())
.into_iter()
.filter(|item| !detached.contains(item))
.filter(|item| !detached_id.contains(&item.id))
.sorted_by(|a, b| a.id.partial_cmp(&b.id).unwrap_or(std::cmp::Ordering::Equal));
let new = Vec::clone(other)
.into_iter()
Expand All @@ -180,18 +188,18 @@ impl CompareAttachable<KonsepItem, KonsepItemMod> for LemmaItem {
#[cfg(test)]
mod test {
use super::*;
use crate::data::LemmaItemMod;
use crate::io::interface::{FromView, SubmitMod};
use crate::views::LemmaWithKonsepView;
use tracing_test::traced_test;
use crate::data::LemmaItemMod;

#[sqlx::test]
#[traced_test]
fn test_new_lemma(pool: sqlx::Pool<sqlx::Sqlite>) -> Result<(), sqlx::Error> {
let new = LemmaItem {
id: AutoGen::Unknown,
lemma: "cakera tokokan".into(),
konseps: vec![]
konseps: vec![],
};
LemmaItemMod::from_item(&new).submit_mod(&pool).await?;
let views = LemmaWithKonsepView::query_lemma("cakera tokokan".into(), &pool).await?;
Expand Down Expand Up @@ -239,6 +247,91 @@ mod test {
assert_eq!(data, vec![new]);
Ok(())
}

#[test]
#[traced_test]
fn test_diff_handling_detach_konsep() -> Result<(), Box<dyn std::error::Error>> {
let lemma_1 = LemmaItem {
id: AutoGen::Known(1),
lemma: "cubaan".into(),
konseps: vec![
KonsepItem {
id: AutoGen::Known(1),
keterangan: "cubaan simpan 1/2".into(),
golongan_kata: "kata nama".into(),
cakupans: vec![],
kata_asing: vec![],
},
KonsepItem {
id: AutoGen::Known(2),
keterangan: "cubaan padam".into(),
golongan_kata: "kata nama".into(),
cakupans: vec![],
kata_asing: vec![],
},
KonsepItem {
id: AutoGen::Known(3),
keterangan: "cubaan simpan 2/2".into(),
golongan_kata: "kata nama".into(),
cakupans: vec![],
kata_asing: vec![],
},
],
};
let lemma_2 = LemmaItem {
id: AutoGen::Known(1),
lemma: "cubaan".into(),
konseps: vec![
KonsepItem {
id: AutoGen::Known(1),
keterangan: "cubaan simpan 1/2".into(),
golongan_kata: "kata nama".into(),
cakupans: vec![],
kata_asing: vec![],
},
KonsepItem {
id: AutoGen::Known(3),
keterangan: "cubaan simpan 2/2".into(),
golongan_kata: "kata nama".into(),
cakupans: vec![],
kata_asing: vec![],
},
],
};
let attachment_mod = lemma_1.compare_attachment(lemma_2.konseps);
assert_eq!(
attachment_mod.detached,
vec![KonsepItemMod {
id: AutoGen::Known(2),
keterangan: FieldMod::Fixed("cubaan padam".into()),
golongan_kata: FieldMod::Fixed("kata nama".into()),
cakupans: AttachmentMod::empty(),
kata_asing: AttachmentMod::empty()
}]
);
assert_eq!(
attachment_mod.modified,
vec![
KonsepItemMod {
id: AutoGen::Known(1),
keterangan: FieldMod::Fixed("cubaan simpan 1/2".into()),
golongan_kata: FieldMod::Fixed("kata nama".into()),
cakupans: AttachmentMod::empty(),
kata_asing: AttachmentMod::empty(),
},
KonsepItemMod {
id: AutoGen::Known(3),
keterangan: FieldMod::Fixed("cubaan simpan 2/2".into()),
golongan_kata: FieldMod::Fixed("kata nama".into()),
cakupans: AttachmentMod::empty(),
kata_asing: AttachmentMod::empty(),
},
]
);
assert!(false);
Ok(())
}

#[sqlx::test(fixtures("lemma"))]
fn test_diff_handling_detach_cakupan(
pool: sqlx::Pool<sqlx::Sqlite>,
Expand Down
19 changes: 13 additions & 6 deletions src-tauri/database/src/data/items/konsep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,24 @@ impl AttachmentItemMod<LemmaItem, sqlx::Sqlite> for KonsepItemMod {
}
async fn submit_detachment_from(
&self,
_parent: &LemmaItem,
_pool: &sqlx::Pool<sqlx::Sqlite>,
parent: &LemmaItem,
pool: &sqlx::Pool<sqlx::Sqlite>,
) -> sqlx::Result<()> {
tracing::trace!(
"Detaching <{}:{}> from <{}:{}>",
self.id,
self.keterangan.value(),
_parent.id,
_parent.lemma
parent.id,
parent.lemma
);
todo!()
sqlx::query! {
r#" DELETE FROM konsep WHERE (id = ? AND lemma_id = ?)"#,
self.id,
parent.id
}
.execute(pool)
.await?;
Ok(())
}

async fn submit_modification_with(
Expand Down Expand Up @@ -181,7 +188,7 @@ impl Item for KonsepItem {
fn modify_into(&self, other: &Self) -> Result<Self::IntoMod> {
if self.id != other.id {
return Err(BackendError {
message: String::from("ID Assertion error"),
message: format!("ID Assertion error, {} != {}", self.id, other.id),
});
}
Ok(KonsepItemMod {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/DataCardEdit.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
data = data;
new_konsep = {} as KonsepItem;
}
async function delete_lemma() {
await invoke("delete_lemma", { item: data });
LemmaStore.update((value) => {
Expand Down
10 changes: 10 additions & 0 deletions src/lib/components/DisplayKonseps.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
konseps = konseps;
edit_at = -1;
}
function remove_konsep_from_list(index: number) {
konseps.splice(index, 1);
konseps = konseps;
edit_at = -1;
}
</script>

{#each konseps as konsep, i}
Expand All @@ -36,6 +43,9 @@
onSubmit={() => {
update_value_at(konsep, i);
}}
onDelete={() => {
remove_konsep_from_list(i);
}}
/>
{:else}
<div class="text-left">
Expand Down
8 changes: 7 additions & 1 deletion src/lib/components/FormAppendKonsep.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// Initialize values
export let konsep: KonsepItem;
export let onSubmit: VoidFunction = () => {};
export let onDelete: VoidFunction = () => {};
export let index: number = 0;
$: cakupan_list = [] as string[];
$: kata_asing_list = [] as KataAsingItem[];
Expand Down Expand Up @@ -57,9 +58,14 @@
>
{index + 1}.
<SelectGolonganKata bind:golongan_kata={golongan_kata_item} />
<button type="submit" class="text-right indicator-item btn-primary"
<div class="indicator-item">
<button type="submit" class="text-right btn-primary"
>+</button
>
<button on:click|preventDefault={onDelete} class="text-right btn-secondary"
>-</button
>
</div>
<textarea
class="textarea w-full"
placeholder="konsep"
Expand Down

0 comments on commit 9d02de7

Please sign in to comment.