More work on todo list support
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
use log::warn;
|
||||
use rocket::{
|
||||
delete, get,
|
||||
http::CookieJar,
|
||||
@ -5,13 +6,23 @@ use rocket::{
|
||||
serde::json::{json, Json},
|
||||
State,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serenity::prelude::Context;
|
||||
|
||||
use crate::web::{check_authorization, guards::transaction::Transaction, routes::JsonResult};
|
||||
use crate::web::{
|
||||
check_authorization, guards::transaction::Transaction, routes::JsonResult, string_opt,
|
||||
};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct CreateTodo {
|
||||
pub struct CreateTodo {
|
||||
channel_id: Option<u64>,
|
||||
value: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct GetTodo {
|
||||
id: u32,
|
||||
#[serde(with = "string_opt")]
|
||||
channel_id: Option<u64>,
|
||||
value: String,
|
||||
}
|
||||
@ -38,7 +49,30 @@ pub async fn get_todo(
|
||||
) -> JsonResult {
|
||||
check_authorization(cookies, ctx.inner(), id).await?;
|
||||
|
||||
Ok(json!([]))
|
||||
let todos = sqlx::query_as!(
|
||||
GetTodo,
|
||||
"
|
||||
SELECT
|
||||
todos.id,
|
||||
channels.channel AS channel_id,
|
||||
value
|
||||
FROM todos
|
||||
INNER JOIN guilds
|
||||
ON guilds.id = todos.guild_id
|
||||
LEFT JOIN channels
|
||||
ON channels.id = todos.channel_id
|
||||
WHERE guilds.guild = ?
|
||||
",
|
||||
id
|
||||
)
|
||||
.fetch_all(transaction.executor())
|
||||
.await
|
||||
.map_err(|e| {
|
||||
warn!("Error fetching todos: {:?}", e);
|
||||
json!({ "errors": vec!["Unknown error"] })
|
||||
})?;
|
||||
|
||||
Ok(json!(todos))
|
||||
}
|
||||
|
||||
#[patch("/api/guild/<id>/todos")]
|
||||
|
Reference in New Issue
Block a user