Skip to content

Commit

Permalink
🚑 Allow features to work
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaza-Kun committed Jul 14, 2024
1 parent a2f4b36 commit af577db
Show file tree
Hide file tree
Showing 13 changed files with 541 additions and 144 deletions.
2 changes: 1 addition & 1 deletion backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/database/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "database"
version = "0.5.0"
version = "0.5.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
24 changes: 14 additions & 10 deletions backend/database/src/changes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Contains general modification structs and enums.

use std::fmt::Display;

use itertools::Itertools;
use tracing::instrument;

Expand Down Expand Up @@ -51,13 +53,13 @@ where
A: ItemMod,
{
#[instrument(skip_all)]
pub async fn submit_changes_with<P, DB: sqlx::Database>(
pub async fn submit_changes_with<P>(
&self,
parent: &P,
pool: &sqlx::Pool<DB>,
pool: &sqlx::Pool<<A as AttachmentItemMod<P>>::Engine>,
) -> sqlx::Result<()>
where
A: ItemMod + AttachmentItemMod<P, DB>,
A: ItemMod + AttachmentItemMod<P>,
P: Item,
{
for attached in self.attached.iter() {
Expand Down Expand Up @@ -124,29 +126,31 @@ pub trait CompareAttachable<I: PartialEq + Clone + Item<IntoMod = A>, A: ItemMod
}
}

impl CompareAttachable<CakupanItem, CakupanItem> for KonsepItem {
impl<I> CompareAttachable<CakupanItem, CakupanItem> for KonsepItem<I> {
fn items(&self) -> Vec<CakupanItem> {
Vec::clone(&self.cakupans)
}
}
impl CompareAttachable<KataAsingItem, KataAsingItem> for KonsepItem {
impl<I> CompareAttachable<KataAsingItem, KataAsingItem> for KonsepItem<I> {
fn items(&self) -> Vec<KataAsingItem> {
Vec::clone(&self.kata_asing)
}
}

impl CompareAttachable<KonsepItem, KonsepItemMod> for LemmaItem {
fn items(&self) -> Vec<KonsepItem> {
impl<I: Copy + Clone + PartialEq + PartialOrd + Display>
CompareAttachable<KonsepItem<I>, KonsepItemMod<I>> for LemmaItem<I>
{
fn items(&self) -> Vec<KonsepItem<I>> {
Vec::clone(&self.konseps)
}
fn find_attached(&self, other: &Vec<KonsepItem>) -> Vec<KonsepItemMod> {
fn find_attached(&self, other: &Vec<KonsepItem<I>>) -> Vec<KonsepItemMod<I>> {
Vec::clone(other)
.into_iter()
.filter(|item| item.id == AutoGen::Unknown)
.map(|item| KonsepItemMod::from_item(&item))
.collect_vec()
}
fn find_detached(&self, other: &Vec<KonsepItem>) -> Vec<KonsepItemMod> {
fn find_detached(&self, other: &Vec<KonsepItem<I>>) -> Vec<KonsepItemMod<I>> {
let other_ids = Vec::clone(other)
.into_iter()
.filter(|item| item.id != AutoGen::Unknown)
Expand All @@ -158,7 +162,7 @@ impl CompareAttachable<KonsepItem, KonsepItemMod> for LemmaItem {
.map(|item| KonsepItemMod::from_item(&item))
.collect_vec()
}
fn find_modified(&self, other: &Vec<KonsepItem>) -> Vec<KonsepItemMod> {
fn find_modified(&self, other: &Vec<KonsepItem<I>>) -> Vec<KonsepItemMod<I>> {
let detached_id = self
.find_detached(other)
.iter()
Expand Down
101 changes: 94 additions & 7 deletions backend/database/src/data/items/cakupan.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Contains the [CakupanItem].

use std::fmt::{Debug, Display};

use crate::io::interface::{AttachmentItemMod, FromView, Item, ItemMod, SubmitItem};
use crate::prelude::*;
use tracing::instrument;
Expand Down Expand Up @@ -91,12 +93,15 @@ impl From<String> for CakupanItem {

#[cfg(feature = "sqlite")]
#[async_trait::async_trait]
impl AttachmentItemMod<KonsepItem, sqlx::Sqlite> for CakupanItem {
impl<I: Display + Sync + PartialEq + Copy + Clone> AttachmentItemMod<KonsepItem<I>>
for CakupanItem
{
type Engine = sqlx::Sqlite;
#[instrument(skip_all)]
async fn submit_attachment_to(
&self,
parent: &KonsepItem,
pool: &sqlx::Pool<sqlx::Sqlite>,
parent: &KonsepItem<I>,
pool: &sqlx::Pool<Self::Engine>,
) -> sqlx::Result<()> {
tracing::trace!(
"Attaching <Cakupan={}> to <{}:{}>",
Expand All @@ -123,8 +128,8 @@ impl AttachmentItemMod<KonsepItem, sqlx::Sqlite> for CakupanItem {
}
async fn submit_detachment_from(
&self,
parent: &KonsepItem,
pool: &sqlx::Pool<sqlx::Sqlite>,
parent: &KonsepItem<I>,
pool: &sqlx::Pool<Self::Engine>,
) -> sqlx::Result<()> {
tracing::trace!(
"Detaching <Cakupan={}> from <{}:{}>",
Expand All @@ -150,8 +155,90 @@ impl AttachmentItemMod<KonsepItem, sqlx::Sqlite> for CakupanItem {

async fn submit_modification_with(
&self,
parent: &KonsepItem,
_pool: &sqlx::Pool<sqlx::Sqlite>,
parent: &KonsepItem<I>,
_pool: &sqlx::Pool<Self::Engine>,
) -> sqlx::Result<()> {
tracing::trace!(
"Modifying <Cakupan={}> with <{}:{}>",
self.0,
parent.id,
parent.keterangan
);
todo!()
}
}

#[cfg(feature = "postgres")]
#[async_trait::async_trait]
impl<I: Display + Sync + PartialEq + Copy + Clone> AttachmentItemMod<KonsepItem<I>>
for CakupanItem
{
type Engine = sqlx::Postgres;
#[instrument(skip_all)]
async fn submit_attachment_to(
&self,
parent: &KonsepItem<I>,
pool: &sqlx::Pool<Self::Engine>,
) -> sqlx::Result<()> {
tracing::trace!(
"Attaching <Cakupan={}> to <{}:{}>",
self.0,
parent.id,
parent.keterangan
);
sqlx::query! {
r#"INSERT INTO cakupan (nama) VALUES ($1) ON CONFLICT (nama) DO NOTHING;"#,
self.0
}
.execute(pool)
.await
.expect("Error attaching cakupan to konsep");

sqlx::query! {
r#"INSERT INTO cakupan_x_konsep (cakupan_id, konsep_id)
VALUES (
(SELECT id FROM cakupan WHERE cakupan.nama = $1),
(SELECT id FROM konsep WHERE konsep.keterangan = $2)
) ON CONFLICT (cakupan_id, konsep_id) DO NOTHING;"#,
self.0,
parent.keterangan
}
.execute(pool)
.await
.expect("Error attaching cakupan to konsep");
Ok(())
}
async fn submit_detachment_from(
&self,
parent: &KonsepItem<I>,
pool: &sqlx::Pool<Self::Engine>,
) -> sqlx::Result<()> {
tracing::trace!(
"Detaching <Cakupan={}> from <{}:{}>",
self.0,
parent.id,
parent.keterangan
);
sqlx::query! {
r#" DELETE FROM cakupan_x_konsep AS cxk
WHERE (
cxk.cakupan_id = (SELECT id FROM cakupan WHERE cakupan.nama = $1)
AND
cxk.konsep_id = (SELECT id FROM konsep WHERE konsep.keterangan = $2)
);"#,
self.0,
parent.keterangan
}
.execute(pool)
.await
.expect("Error detaching cakupan from konsep");
Ok(())
}

async fn submit_modification_with(
&self,
parent: &KonsepItem<I>,
_pool: &sqlx::Pool<Self::Engine>,
) -> sqlx::Result<()> {
tracing::trace!(
"Modifying <Cakupan={}> with <{}:{}>",
Expand Down
102 changes: 95 additions & 7 deletions backend/database/src/data/items/kata_asing.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Contains struct [KataAsingItem] which map a foreign word with its language of origin.

use std::fmt::Display;

use crate::io::interface::{AttachmentItemMod, FromView, Item, ItemMod, SubmitItem};
use crate::prelude::*;

Expand Down Expand Up @@ -97,11 +99,15 @@ impl SubmitItem for KataAsingItem {

#[cfg(feature = "sqlite")]
#[async_trait::async_trait]
impl AttachmentItemMod<KonsepItem, sqlx::Sqlite> for KataAsingItem {
impl<I: Display + Sync + PartialEq + Copy + Clone> AttachmentItemMod<KonsepItem<I>>
for KataAsingItem
{
type Engine = sqlx::Sqlite;

async fn submit_attachment_to(
&self,
parent: &KonsepItem,
pool: &sqlx::Pool<sqlx::Sqlite>,
parent: &KonsepItem<I>,
pool: &sqlx::Pool<Self::Engine>,
) -> sqlx::Result<()> {
tracing::trace!(
"Attaching <KataAsing {}:{}> to <{}:{}>",
Expand Down Expand Up @@ -130,8 +136,8 @@ impl AttachmentItemMod<KonsepItem, sqlx::Sqlite> for KataAsingItem {
}
async fn submit_detachment_from(
&self,
parent: &KonsepItem,
pool: &sqlx::Pool<sqlx::Sqlite>,
parent: &KonsepItem<I>,
pool: &sqlx::Pool<Self::Engine>,
) -> sqlx::Result<()> {
tracing::trace!(
"Detaching <KataAsing {}:{}> from <{}:{}>",
Expand Down Expand Up @@ -159,8 +165,90 @@ impl AttachmentItemMod<KonsepItem, sqlx::Sqlite> for KataAsingItem {

async fn submit_modification_with(
&self,
parent: &KonsepItem,
_pool: &sqlx::Pool<sqlx::Sqlite>,
parent: &KonsepItem<I>,
_pool: &sqlx::Pool<Self::Engine>,
) -> sqlx::Result<()> {
tracing::trace!(
"Modifying <KataAsing {}:{}> with <{}:{}>",
self.bahasa,
self.nama,
parent.id,
parent.keterangan
);
todo!()
}
}

#[cfg(feature = "postgres")]
#[async_trait::async_trait]
impl<I: Display + Sync + PartialEq + Copy + Clone> AttachmentItemMod<KonsepItem<I>>
for KataAsingItem
{
type Engine = sqlx::Postgres;

async fn submit_attachment_to(
&self,
parent: &KonsepItem<I>,
pool: &sqlx::Pool<Self::Engine>,
) -> sqlx::Result<()> {
tracing::trace!(
"Attaching <KataAsing {}:{}> to <{}:{}>",
self.bahasa,
self.nama,
parent.id,
parent.keterangan
);
sqlx::query! {
r#"INSERT INTO kata_asing (nama, bahasa) VALUES ($1,$2) ON CONFLICT (nama, bahasa) DO NOTHING;"#, self.nama, self.bahasa
}.execute(pool).await.expect("Error attaching kata_asing to konsep");
sqlx::query! {
r#"INSERT INTO kata_asing_x_konsep (kata_asing_id, konsep_id)
VALUES (
(SELECT id FROM kata_asing WHERE kata_asing.nama = $1 AND kata_asing.bahasa = $2),
(SELECT id FROM konsep WHERE konsep.keterangan = $3)
) ON CONFLICT (kata_asing_id, konsep_id) DO NOTHING;"#,
self.nama,
self.bahasa,
parent.keterangan
}
.execute(pool)
.await
.expect("Error attaching kata_asing to konsep");
Ok(())
}
async fn submit_detachment_from(
&self,
parent: &KonsepItem<I>,
pool: &sqlx::Pool<Self::Engine>,
) -> sqlx::Result<()> {
tracing::trace!(
"Detaching <KataAsing {}:{}> from <{}:{}>",
self.bahasa,
self.nama,
parent.id,
parent.keterangan
);
sqlx::query! {
r#"DELETE FROM kata_asing_x_konsep AS kaxk
WHERE (
kaxk.kata_asing_id = (SELECT id FROM kata_asing WHERE kata_asing.nama = $1 AND kata_asing.bahasa = $2)
AND
kaxk.konsep_id = (SELECT id FROM konsep WHERE konsep.keterangan = $3)
);"#,
self.nama,
self.bahasa,
parent.keterangan
}
.execute(pool)
.await
.expect("Error detaching cakupan from konsep");
Ok(())
}

async fn submit_modification_with(
&self,
parent: &KonsepItem<I>,
_pool: &sqlx::Pool<Self::Engine>,
) -> sqlx::Result<()> {
tracing::trace!(
"Modifying <KataAsing {}:{}> with <{}:{}>",
Expand Down
Loading

0 comments on commit af577db

Please sign in to comment.