From 74701aa0c5bd96c4efc323d3031825be879a6988 Mon Sep 17 00:00:00 2001 From: Orion Kindel Date: Sun, 16 Jul 2023 15:58:27 -0400 Subject: [PATCH] feat: include links on post /users --- src/rep_payload.rs | 1 + src/route/users.rs | 15 ++++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/rep_payload.rs b/src/rep_payload.rs index 85fedf8..70b6f6f 100644 --- a/src/rep_payload.rs +++ b/src/rep_payload.rs @@ -4,5 +4,6 @@ use std::collections::HashMap; pub struct RepPayload { #[serde(flatten)] pub t: T, + #[serde(skip_serializing_if = "HashMap::is_empty")] pub links: HashMap, } diff --git a/src/route/users.rs b/src/route/users.rs index 9c5a6ef..fb26b24 100644 --- a/src/route/users.rs +++ b/src/route/users.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + use naan::prelude::*; use toad::net::Addrd; use toad::resp::code::{self, BAD_REQUEST, CHANGED, CONTENT, CREATED}; @@ -7,6 +9,7 @@ use toad_msg::{Code, ContentFormat, MessageOptions, Type}; use crate::app::App; use crate::err::{ToE, E}; use crate::model::{Actor, UserId, UserInsert, UserPatch, UserRepoError}; +use crate::rep_payload::RepPayload; use crate::repo::{Del, Insert, Page, Patch, ReadMany, ReadOne}; use crate::req_payload::ReqPayload; @@ -34,11 +37,13 @@ pub fn get_user(app: &A, where A: App { let user = app.user().get(&actor, &id).map_err(UserRepoError::into_e)?; - Ok(Some( - Message::builder(Type::Ack, CONTENT).token(req.data().token) - .content_format(ContentFormat::Json) - .payload(serde_json::to_vec(&user).map_err(ToE::to_e)?) - .build())) + let rep = RepPayload { t: user, + links: HashMap::from([("login".into(), "user_sessions/".into())]) }; + + Ok(Some(Message::builder(Type::Ack, CONTENT).token(req.data().token) + .content_format(ContentFormat::Json) + .payload(serde_json::to_vec(&rep).map_err(ToE::to_e)?) + .build())) } pub fn get_users(app: &A, actor: &Actor, req: Addrd<&Message>) -> Result, E>