Use transactions for certain routes
This commit is contained in:
@ -10,12 +10,15 @@ use serenity::{
|
||||
};
|
||||
use sqlx::{MySql, Pool};
|
||||
|
||||
use crate::routes::{
|
||||
dashboard::{
|
||||
create_reminder, generate_uid, ImportBody, Reminder, ReminderCsv, ReminderTemplateCsv,
|
||||
TodoCsv,
|
||||
use crate::{
|
||||
guards::transaction::Transaction,
|
||||
routes::{
|
||||
dashboard::{
|
||||
create_reminder, generate_uid, ImportBody, Reminder, ReminderCsv, ReminderTemplateCsv,
|
||||
TodoCsv,
|
||||
},
|
||||
JsonResult,
|
||||
},
|
||||
JsonResult,
|
||||
};
|
||||
|
||||
#[get("/api/guild/<id>/export/reminders")]
|
||||
@ -118,12 +121,12 @@ pub async fn export_reminders(
|
||||
}
|
||||
|
||||
#[put("/api/guild/<id>/export/reminders", data = "<body>")]
|
||||
pub async fn import_reminders(
|
||||
pub(crate) async fn import_reminders(
|
||||
id: u64,
|
||||
cookies: &CookieJar<'_>,
|
||||
body: Json<ImportBody>,
|
||||
ctx: &State<Context>,
|
||||
pool: &State<Pool<MySql>>,
|
||||
mut transaction: Transaction<'_>,
|
||||
) -> JsonResult {
|
||||
check_authorization!(cookies, ctx.inner(), id);
|
||||
|
||||
@ -175,7 +178,7 @@ pub async fn import_reminders(
|
||||
|
||||
create_reminder(
|
||||
ctx.inner(),
|
||||
pool.inner(),
|
||||
&mut transaction,
|
||||
GuildId(id),
|
||||
UserId(user_id),
|
||||
reminder,
|
||||
@ -200,7 +203,14 @@ pub async fn import_reminders(
|
||||
}
|
||||
}
|
||||
|
||||
Ok(json!({}))
|
||||
match transaction.commit().await {
|
||||
Ok(_) => Ok(json!({})),
|
||||
|
||||
Err(e) => {
|
||||
warn!("Failed to commit transaction: {:?}", e);
|
||||
json_err!("Couldn't commit transaction")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Err(_) => {
|
||||
|
Reference in New Issue
Block a user