fix refreshing canteens when menu is empty

This commit is contained in:
Moritz Hölting 2026-01-03 00:48:17 +01:00
parent c56fe5a8a8
commit 3ff708540a
2 changed files with 56 additions and 45 deletions

View File

@ -119,7 +119,9 @@ pub async fn check_refresh(db: &sqlx::PgPool, date: NaiveDate, canteens: &[Cante
.difference(&db_dishes)
.collect::<HashSet<_>>();
if let Err(err) = update_stale_dishes(db, date, &stale_dishes, &new_dishes).await {
if let Err(err) =
update_stale_dishes(db, date, &stale_dishes, &new_dishes, canteens).await
{
tracing::error!("Error updating stale dishes in db: {}", err);
false
} else {
@ -151,9 +153,11 @@ async fn update_stale_dishes(
date: NaiveDate,
stale_dishes: &HashSet<&(Canteen, Dish)>,
new_dishes: &HashSet<&(Canteen, Dish)>,
canteens: &[Canteen],
) -> Result<(), sqlx::Error> {
let mut tx = db.begin().await?;
if !stale_dishes.is_empty() {
QueryBuilder::new("UPDATE meals SET is_latest = FALSE WHERE date = ")
.push_bind(date)
.push(r#" AND ("name", canteen) IN "#)
@ -165,18 +169,27 @@ async fn update_stale_dishes(
.build()
.execute(&mut *tx)
.await?;
}
let chunks = new_dishes
.iter()
.sorted_by_key(|(c, _)| c)
.chunk_by(|(c, _)| c);
let new_dishes_iter = chunks.into_iter().map(|(canteen, g)| {
let new_dishes_iter = chunks
.into_iter()
.map(|(canteen, g)| {
(
*canteen,
g.map(|(_, dish)| dish).cloned().collect::<Vec<_>>(),
)
});
})
.chain(
canteens
.iter()
.map(|canteen| (*canteen, Vec::new()))
.unique_by(|(c, _)| *c),
);
for (canteen, menu) in new_dishes_iter {
add_menu_to_db(&mut tx, &date, canteen, menu).await?;

View File

@ -85,10 +85,7 @@ pub async fn add_menu_to_db(
canteen: Canteen,
menu: Vec<Dish>,
) -> Result<(), sqlx::Error> {
if menu.is_empty() {
return Ok(());
}
if !menu.is_empty() {
let mut query = sqlx::QueryBuilder::new("INSERT INTO meals (date,canteen,name,dish_type,image_src,price_students,price_employees,price_guests,vegan,vegetarian,kjoules,proteins,carbohydrates,fats) ");
query
@ -113,6 +110,7 @@ pub async fn add_menu_to_db(
.build()
.execute(&mut **db)
.await?;
}
sqlx::query!(
"INSERT INTO canteens_scraped (scraped_for, canteen) VALUES ($1, $2)",