diff --git a/src/main.rs b/src/main.rs index 06458e9..3c4dac8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,16 @@ struct RfidCard { cardComment: String } +// Implement `.into()` to auto map values from the database model to the local struct +impl From<::Model> for RfidCard { + fn from(rfid_card: ::Model) -> RfidCard { + RfidCard { + cardId: rfid_card.card_id, + cardComment: rfid_card.card_comment, + } + } +} + #[derive(Serialize, Deserialize)] struct Member { id: i32, @@ -106,8 +116,23 @@ async fn get_members(db: &State) -> Result = members.into_iter().map(Member::from).collect(); - // TODO: Add the member's RFID card + let mut members: Vec = members.into_iter().map(Member::from).collect(); + + // Fetch the member's RFID card + for member in &mut members { + // Look up all RFID cards associated with this member + let rfid_cards = RfidCards::find() + .filter(rfid_cards::Column::MemberId.eq(member.id)) + .all(db) + .await?; + + // Convert the RFID cards vector from the database model to the local struct model + let rfid_cards: Vec = rfid_cards.into_iter().map(RfidCard::from).collect(); + + // Add these cards to the member's rfidCards field + member.rfidCards = rfid_cards; + } + // Put this into the `MultipleMembersStruct` so that the aquired JSON will look as the API demands let output = MultipleMembersStruct { members }; diff --git a/test.db b/test.db index 421baaf..c34eab2 100644 Binary files a/test.db and b/test.db differ