api for meal plan deletion

This commit is contained in:
Jeremy Wall 2023-01-18 07:47:25 -05:00
parent 2ffb3be114
commit ad62c5f31f
2 changed files with 60 additions and 0 deletions

View File

@ -258,6 +258,22 @@ async fn api_all_plans(
}
}
async fn api_delete_plan_for_date(
Extension(app_store): Extension<Arc<storage::SqliteStore>>,
session: storage::UserIdFromSession,
Path(date): Path<chrono::NaiveDate>,
) -> api::EmptyResponse {
use storage::{UserId, UserIdFromSession::FoundUserId};
if let FoundUserId(UserId(id)) = session {
app_store
.delete_meal_plan_for_date(id.as_str(), date)
.await
.into()
} else {
api::EmptyResponse::Unauthorized
}
}
async fn api_save_plan_for_date(
Extension(app_store): Extension<Arc<storage::SqliteStore>>,
session: storage::UserIdFromSession,

View File

@ -138,6 +138,12 @@ pub trait APIStore {
user_id: S,
) -> Result<Option<Vec<NaiveDate>>>;
async fn delete_meal_plan_for_date<S: AsRef<str> + Send>(
&self,
user_id: S,
date: NaiveDate,
) -> Result<()>;
async fn save_meal_plan<S: AsRef<str> + Send>(
&self,
user_id: S,
@ -615,6 +621,44 @@ impl APIStore for SqliteStore {
Ok(Some(result))
}
async fn delete_meal_plan_for_date<S: AsRef<str> + Send>(
&self,
user_id: S,
date: NaiveDate,
) -> Result<()> {
let user_id = user_id.as_ref();
let mut transaction = self.pool.as_ref().begin().await?;
sqlx::query!(
"delete from plan_recipes where user_id = ? and plan_date = ?",
user_id,
date
)
.execute(&mut transaction)
.await?;
sqlx::query!(
"delete from filtered_ingredients where user_id = ? and plan_date = ?",
user_id,
date
)
.execute(&mut transaction)
.await?;
sqlx::query!(
"delete from modified_amts where user_id = ? and plan_date = ?",
user_id,
date
)
.execute(&mut transaction)
.await?;
sqlx::query!(
"delete from extra_items where user_id = ? and plan_date = ?",
user_id,
date
)
.execute(&mut transaction)
.await?;
Ok(())
}
async fn fetch_meal_plan_for_date<S: AsRef<str> + Send>(
&self,
user_id: S,