Skip to content

Commit

Permalink
Update API Structure
Browse files Browse the repository at this point in the history
  • Loading branch information
isiko committed May 14, 2024
1 parent 81eb538 commit 3b3ae3e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
DATABASE_URL="postgres://dennis@localhost/food_calc_testing"
API_PORT=8080
API_PORT=8090
API_INTERFACE=0.0.0.0
54 changes: 53 additions & 1 deletion api/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ use foodlib::Event;
use serde::{Deserialize, Serialize};
use sqlx::{
postgres::types::PgMoney,
types::{chrono::NaiveDateTime, BigDecimal},
types::{
chrono::{NaiveDate, NaiveDateTime},
BigDecimal,
},
Encode,
};

use crate::ApiState;
Expand Down Expand Up @@ -177,9 +181,57 @@ async fn meal_update(
return StatusCode::OK;
}

#[derive(Serialize, Deserialize)]
struct IdAndName {
id: i32,
name: String,
}

#[derive(Serialize, Deserialize)]
struct APIDate {
start: i64,
end: i64,
}
#[derive(Serialize, Deserialize)]
struct MealReturn {
event_id: i32,
recipe: IdAndName,
place: IdAndName,
date: APIDate,
weight: BigDecimal,
energy: BigDecimal,
price: BigDecimal,
servings: i32,
comment: Option<String>,
}
async fn meal_list(State(state): State<ApiState>, Path(event_id): Path<i32>) -> impl IntoResponse {
let query = state.food_base.get_event_meals(event_id).await;
if let Ok(meals) = query {
let meals: Vec<MealReturn> = meals
.into_iter()
.map(|meal| {
return MealReturn {
event_id: event_id,
recipe: IdAndName {
id: meal.recipe_id,
name: meal.name,
},
place: IdAndName {
id: meal.place_id,
name: meal.place,
},
date: APIDate {
start: meal.start_time.timestamp_millis(),
end: meal.start_time.timestamp_millis(),
},
weight: meal.weight,
energy: meal.energy,
price: meal.price.to_bigdecimal(2),
servings: meal.servings,
comment: meal.comment,
};
})
.collect();
return (StatusCode::OK, Json(meals));
} else {
todo!()
Expand Down

0 comments on commit 3b3ae3e

Please sign in to comment.