fix refreshing canteens when menu is empty
This commit is contained in:
parent
c56fe5a8a8
commit
3ff708540a
|
|
@ -119,7 +119,9 @@ pub async fn check_refresh(db: &sqlx::PgPool, date: NaiveDate, canteens: &[Cante
|
||||||
.difference(&db_dishes)
|
.difference(&db_dishes)
|
||||||
.collect::<HashSet<_>>();
|
.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);
|
tracing::error!("Error updating stale dishes in db: {}", err);
|
||||||
false
|
false
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -151,9 +153,11 @@ async fn update_stale_dishes(
|
||||||
date: NaiveDate,
|
date: NaiveDate,
|
||||||
stale_dishes: &HashSet<&(Canteen, Dish)>,
|
stale_dishes: &HashSet<&(Canteen, Dish)>,
|
||||||
new_dishes: &HashSet<&(Canteen, Dish)>,
|
new_dishes: &HashSet<&(Canteen, Dish)>,
|
||||||
|
canteens: &[Canteen],
|
||||||
) -> Result<(), sqlx::Error> {
|
) -> Result<(), sqlx::Error> {
|
||||||
let mut tx = db.begin().await?;
|
let mut tx = db.begin().await?;
|
||||||
|
|
||||||
|
if !stale_dishes.is_empty() {
|
||||||
QueryBuilder::new("UPDATE meals SET is_latest = FALSE WHERE date = ")
|
QueryBuilder::new("UPDATE meals SET is_latest = FALSE WHERE date = ")
|
||||||
.push_bind(date)
|
.push_bind(date)
|
||||||
.push(r#" AND ("name", canteen) IN "#)
|
.push(r#" AND ("name", canteen) IN "#)
|
||||||
|
|
@ -165,18 +169,27 @@ async fn update_stale_dishes(
|
||||||
.build()
|
.build()
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await?;
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
let chunks = new_dishes
|
let chunks = new_dishes
|
||||||
.iter()
|
.iter()
|
||||||
.sorted_by_key(|(c, _)| c)
|
.sorted_by_key(|(c, _)| c)
|
||||||
.chunk_by(|(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,
|
*canteen,
|
||||||
g.map(|(_, dish)| dish).cloned().collect::<Vec<_>>(),
|
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 {
|
for (canteen, menu) in new_dishes_iter {
|
||||||
add_menu_to_db(&mut tx, &date, canteen, menu).await?;
|
add_menu_to_db(&mut tx, &date, canteen, menu).await?;
|
||||||
|
|
|
||||||
|
|
@ -85,10 +85,7 @@ pub async fn add_menu_to_db(
|
||||||
canteen: Canteen,
|
canteen: Canteen,
|
||||||
menu: Vec<Dish>,
|
menu: Vec<Dish>,
|
||||||
) -> Result<(), sqlx::Error> {
|
) -> Result<(), sqlx::Error> {
|
||||||
if menu.is_empty() {
|
if !menu.is_empty() {
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
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) ");
|
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
|
query
|
||||||
|
|
@ -113,6 +110,7 @@ pub async fn add_menu_to_db(
|
||||||
.build()
|
.build()
|
||||||
.execute(&mut **db)
|
.execute(&mut **db)
|
||||||
.await?;
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
"INSERT INTO canteens_scraped (scraped_for, canteen) VALUES ($1, $2)",
|
"INSERT INTO canteens_scraped (scraped_for, canteen) VALUES ($1, $2)",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue