various todo fixes

This commit is contained in:
jellywx 2021-11-15 08:09:48 +00:00
parent 610779a293
commit c953bc0cd3
2 changed files with 77 additions and 27 deletions

View File

@ -88,15 +88,37 @@ async fn todo(ctx: &Context, invoke: &mut CommandInvoke, args: CommandOptions) {
.await; .await;
} }
None => { None => {
let values = sqlx::query!( let values = if let Some(uid) = keys.0 {
// fucking braindead mysql use <=> instead of = for null comparison sqlx::query!(
"SELECT todos.id, value FROM todos "SELECT todos.id, value FROM todos
INNER JOIN users ON todos.user_id = users.id INNER JOIN users ON todos.user_id = users.id
WHERE users.user = ?",
uid,
)
.fetch_all(&pool)
.await
.unwrap()
.iter()
.map(|row| (row.id as usize, row.value.clone()))
.collect::<Vec<(usize, String)>>()
} else if let Some(cid) = keys.1 {
sqlx::query!(
"SELECT todos.id, value FROM todos
INNER JOIN channels ON todos.channel_id = channels.id INNER JOIN channels ON todos.channel_id = channels.id
WHERE channels.channel = ?",
cid,
)
.fetch_all(&pool)
.await
.unwrap()
.iter()
.map(|row| (row.id as usize, row.value.clone()))
.collect::<Vec<(usize, String)>>()
} else {
sqlx::query!(
"SELECT todos.id, value FROM todos
INNER JOIN guilds ON todos.guild_id = guilds.id INNER JOIN guilds ON todos.guild_id = guilds.id
WHERE users.user <=> ? AND channels.channel <=> ? AND guilds.guild <=> ?", WHERE guilds.guild = ?",
keys.0,
keys.1,
keys.2, keys.2,
) )
.fetch_all(&pool) .fetch_all(&pool)
@ -104,7 +126,8 @@ WHERE users.user <=> ? AND channels.channel <=> ? AND guilds.guild <=> ?",
.unwrap() .unwrap()
.iter() .iter()
.map(|row| (row.id as usize, row.value.clone())) .map(|row| (row.id as usize, row.value.clone()))
.collect::<Vec<(usize, String)>>(); .collect::<Vec<(usize, String)>>()
};
let resp = show_todo_page(&values, 0, keys.0, keys.1, keys.2); let resp = show_todo_page(&values, 0, keys.0, keys.1, keys.2);

View File

@ -164,11 +164,37 @@ impl ComponentDataModel {
if Some(component.user.id.0) == pager.user_id || pager.user_id.is_none() { if Some(component.user.id.0) == pager.user_id || pager.user_id.is_none() {
let pool = ctx.data.read().await.get::<SQLPool>().cloned().unwrap(); let pool = ctx.data.read().await.get::<SQLPool>().cloned().unwrap();
let values = sqlx::query!( let values = if let Some(uid) = pager.user_id {
// fucking braindead mysql use <=> instead of = for null comparison sqlx::query!(
"SELECT id, value FROM todos WHERE user_id <=> ? AND channel_id <=> ? AND guild_id <=> ?", "SELECT todos.id, value FROM todos
pager.user_id, INNER JOIN users ON todos.user_id = users.id
pager.channel_id, WHERE users.user = ?",
uid,
)
.fetch_all(&pool)
.await
.unwrap()
.iter()
.map(|row| (row.id as usize, row.value.clone()))
.collect::<Vec<(usize, String)>>()
} else if let Some(cid) = pager.channel_id {
sqlx::query!(
"SELECT todos.id, value FROM todos
INNER JOIN channels ON todos.channel_id = channels.id
WHERE channels.channel = ?",
cid,
)
.fetch_all(&pool)
.await
.unwrap()
.iter()
.map(|row| (row.id as usize, row.value.clone()))
.collect::<Vec<(usize, String)>>()
} else {
sqlx::query!(
"SELECT todos.id, value FROM todos
INNER JOIN guilds ON todos.guild_id = guilds.id
WHERE guilds.guild = ?",
pager.guild_id, pager.guild_id,
) )
.fetch_all(&pool) .fetch_all(&pool)
@ -176,7 +202,8 @@ impl ComponentDataModel {
.unwrap() .unwrap()
.iter() .iter()
.map(|row| (row.id as usize, row.value.clone())) .map(|row| (row.id as usize, row.value.clone()))
.collect::<Vec<(usize, String)>>(); .collect::<Vec<(usize, String)>>()
};
let max_pages = max_todo_page(&values); let max_pages = max_todo_page(&values);