add endpoint for earliest saved meal
This commit is contained in:
parent
6f4700e021
commit
8a1df4946d
|
|
@ -0,0 +1,20 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "SELECT MIN(date) AS \"date!\" FROM meals WHERE is_latest = TRUE;",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "date!",
|
||||||
|
"type_info": "Date"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": []
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
null
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "66fcef790324f9eedc03ad052acc67111c50d82f56471df1c9adba498be0d543"
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
|
use actix_web::{
|
||||||
|
get,
|
||||||
|
web::{self, ServiceConfig},
|
||||||
|
HttpResponse, Responder,
|
||||||
|
};
|
||||||
|
use chrono::NaiveDate;
|
||||||
|
use serde_json::json;
|
||||||
|
use sqlx::PgPool;
|
||||||
|
|
||||||
|
pub fn configure(cfg: &mut ServiceConfig) {
|
||||||
|
cfg.service(web::scope("/metadata").service(earliest_meal_date));
|
||||||
|
}
|
||||||
|
|
||||||
|
static EARLIEST_MEAL_DATE: OnceLock<NaiveDate> = OnceLock::new();
|
||||||
|
|
||||||
|
#[get("/earliest-meal-date")]
|
||||||
|
async fn earliest_meal_date(db: web::Data<PgPool>) -> impl Responder {
|
||||||
|
if let Some(earliest_date) = EARLIEST_MEAL_DATE.get() {
|
||||||
|
earliest_meal_date_ok_response(*earliest_date)
|
||||||
|
} else {
|
||||||
|
match sqlx::query_scalar!(
|
||||||
|
r#"SELECT MIN(date) AS "date!" FROM meals WHERE is_latest = TRUE;"#
|
||||||
|
)
|
||||||
|
.fetch_one(db.as_ref())
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(date) => {
|
||||||
|
EARLIEST_MEAL_DATE.set(date).ok();
|
||||||
|
earliest_meal_date_ok_response(date)
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
tracing::error!("Failed to query datebase: {err}");
|
||||||
|
HttpResponse::InternalServerError().json(json!({
|
||||||
|
"error": "Failed to query database"
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn earliest_meal_date_ok_response(date: NaiveDate) -> HttpResponse {
|
||||||
|
HttpResponse::Ok().json(json!({
|
||||||
|
"date": date,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
@ -5,11 +5,13 @@ use shared::Canteen;
|
||||||
use strum::IntoEnumIterator as _;
|
use strum::IntoEnumIterator as _;
|
||||||
|
|
||||||
mod menu;
|
mod menu;
|
||||||
|
mod metadata;
|
||||||
mod nutrition;
|
mod nutrition;
|
||||||
mod price_history;
|
mod price_history;
|
||||||
|
|
||||||
pub fn configure(cfg: &mut ServiceConfig) {
|
pub fn configure(cfg: &mut ServiceConfig) {
|
||||||
cfg.service(index)
|
cfg.service(index)
|
||||||
|
.configure(metadata::configure)
|
||||||
.configure(menu::configure)
|
.configure(menu::configure)
|
||||||
.configure(nutrition::configure)
|
.configure(nutrition::configure)
|
||||||
.configure(price_history::configure);
|
.configure(price_history::configure);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue