Skip to content

Commit

Permalink
impl: getting my words
Browse files Browse the repository at this point in the history
  • Loading branch information
Pugma committed Oct 3, 2024
1 parent 0452623 commit 239f36a
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 22 deletions.

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

56 changes: 34 additions & 22 deletions server/app/src/repo/words.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use anyhow::{Ok, Result};
use sqlx::{query, query_as, Execute, MySql, QueryBuilder};

use openapi::models::{ExcludedUsers, MyWords, Word, Words};
use uuid::Uuid;
use openapi::models::{ExcludedUser, ExcludedUsers, MyWord, MyWords, Word, Words};
use sqlx::{query, query_as, types::uuid::Uuid, Execute, MySql, QueryBuilder};
use std::collections::HashMap;

use super::{constant::BIND_LIMIT, Repository};

Expand Down Expand Up @@ -55,24 +54,37 @@ impl Repository {
Ok(result.into())
}

pub async fn get_my_word(&self, _trap_id: String) -> Result<MyWords> {
// let result = query_as!(
// MyWord,
// "SELECT
// `word`,
// `word_uuid` AS `id`,
// `register_time` AS `time`,
// `excluded_users`.`trap_id` AS `excluded_users`
// FROM `words` JOIN `excluded_users` ON `words`.`word_id` = `excluded_users`.`word_id`
// WHERE `words`.`trap_id`=?",
// trap_id
// )
// .fetch_all(&self.pool)
// .await?;

// Ok(result.into())

unimplemented!()
pub async fn get_my_word(&self, trap_id: String) -> Result<MyWords> {
let rows = query!(
"SELECT
`word`,
`word_uuid` AS `id`,
`register_time` AS `time`,
`excluded_users`.`trap_id` AS `excluded_users`
FROM `words` JOIN `excluded_users` ON `words`.`word_id` = `excluded_users`.`word_id`
WHERE `words`.`trap_id`=?",
trap_id
)
.fetch_all(&self.pool)
.await?;

let mut a: HashMap<String, MyWord> = HashMap::new();
for row in rows {
let entry = a.entry(row.word.clone()).or_insert(MyWord {
word: row.word,
id: Uuid::from_slice(&row.id)?,
time: row.time.unwrap().and_utc(),
excluded_users: Vec::<ExcludedUser>::new().into(),
});

entry.excluded_users.push(ExcludedUser {
trap_id: row.excluded_users,
});
}

let my_words: Vec<MyWord> = a.into_values().collect();

Ok(my_words.into())
}

pub async fn get_by_user(&self, trap_id: String) -> Result<Words> {
Expand Down

0 comments on commit 239f36a

Please sign in to comment.