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,32 +153,43 @@ 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?;
|
||||||
|
|
||||||
QueryBuilder::new("UPDATE meals SET is_latest = FALSE WHERE date = ")
|
if !stale_dishes.is_empty() {
|
||||||
.push_bind(date)
|
QueryBuilder::new("UPDATE meals SET is_latest = FALSE WHERE date = ")
|
||||||
.push(r#" AND ("name", canteen) IN "#)
|
.push_bind(date)
|
||||||
.push_tuples(stale_dishes, |mut sep, (canteen, dish)| {
|
.push(r#" AND ("name", canteen) IN "#)
|
||||||
sep.push_bind(&dish.name)
|
.push_tuples(stale_dishes, |mut sep, (canteen, dish)| {
|
||||||
.push_bind(canteen.get_identifier());
|
sep.push_bind(&dish.name)
|
||||||
})
|
.push_bind(canteen.get_identifier());
|
||||||
.push(";")
|
})
|
||||||
.build()
|
.push(";")
|
||||||
.execute(&mut *tx)
|
.build()
|
||||||
.await?;
|
.execute(&mut *tx)
|
||||||
|
.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()
|
||||||
*canteen,
|
.map(|(canteen, g)| {
|
||||||
g.map(|(_, dish)| dish).cloned().collect::<Vec<_>>(),
|
(
|
||||||
)
|
*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 {
|
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,35 +85,33 @@ 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) ");
|
||||||
|
|
||||||
|
query
|
||||||
|
.push_values(menu, |mut sep, item| {
|
||||||
|
let vegan = item.is_vegan();
|
||||||
|
|
||||||
|
sep.push_bind(date)
|
||||||
|
.push_bind(canteen.get_identifier())
|
||||||
|
.push_bind(item.get_name().to_string())
|
||||||
|
.push_bind(item.get_type() as DishType)
|
||||||
|
.push_bind(item.get_image_src().map(str::to_string))
|
||||||
|
.push_bind(item.get_price_students().to_owned())
|
||||||
|
.push_bind(item.get_price_employees().to_owned())
|
||||||
|
.push_bind(item.get_price_guests().to_owned())
|
||||||
|
.push_bind(vegan)
|
||||||
|
.push_bind(vegan || item.is_vegetarian())
|
||||||
|
.push_bind(item.nutrition_values.kjoule)
|
||||||
|
.push_bind(item.nutrition_values.protein.to_owned())
|
||||||
|
.push_bind(item.nutrition_values.carbs.to_owned())
|
||||||
|
.push_bind(item.nutrition_values.fat.to_owned());
|
||||||
|
})
|
||||||
|
.build()
|
||||||
|
.execute(&mut **db)
|
||||||
|
.await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
.push_values(menu, |mut sep, item| {
|
|
||||||
let vegan = item.is_vegan();
|
|
||||||
|
|
||||||
sep.push_bind(date)
|
|
||||||
.push_bind(canteen.get_identifier())
|
|
||||||
.push_bind(item.get_name().to_string())
|
|
||||||
.push_bind(item.get_type() as DishType)
|
|
||||||
.push_bind(item.get_image_src().map(str::to_string))
|
|
||||||
.push_bind(item.get_price_students().to_owned())
|
|
||||||
.push_bind(item.get_price_employees().to_owned())
|
|
||||||
.push_bind(item.get_price_guests().to_owned())
|
|
||||||
.push_bind(vegan)
|
|
||||||
.push_bind(vegan || item.is_vegetarian())
|
|
||||||
.push_bind(item.nutrition_values.kjoule)
|
|
||||||
.push_bind(item.nutrition_values.protein.to_owned())
|
|
||||||
.push_bind(item.nutrition_values.carbs.to_owned())
|
|
||||||
.push_bind(item.nutrition_values.fat.to_owned());
|
|
||||||
})
|
|
||||||
.build()
|
|
||||||
.execute(&mut **db)
|
|
||||||
.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)",
|
||||||
date,
|
date,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue