Added optional user name.
This commit is contained in:
@@ -37,7 +37,10 @@ impl AccountMemberResponse {
|
||||
pub fn from_domain(member: domain::AccountMember, user: domain::User) -> Self {
|
||||
Self {
|
||||
id: member.id,
|
||||
user: UserResponse { id: user.id },
|
||||
user: UserResponse {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -45,6 +48,7 @@ impl AccountMemberResponse {
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct UserResponse {
|
||||
pub id: Uuid,
|
||||
pub name: Option<String>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -60,12 +64,18 @@ mod tests {
|
||||
fn response(id: Uuid, user: domain::User) -> AccountMemberResponse {
|
||||
AccountMemberResponse {
|
||||
id,
|
||||
user: UserResponse { id: user.id },
|
||||
user: UserResponse {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn user() -> domain::User {
|
||||
domain::User { id: Uuid::nil() }
|
||||
domain::User {
|
||||
id: Uuid::nil(),
|
||||
name: Some("Test User".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -75,6 +85,7 @@ mod tests {
|
||||
|
||||
assert_eq!(response["id"], id.to_string());
|
||||
assert_eq!(response["user"]["id"], Uuid::nil().to_string());
|
||||
assert_eq!(response["user"]["name"], "Test User");
|
||||
assert!(response.get("account_id").is_none());
|
||||
}
|
||||
|
||||
@@ -99,5 +110,6 @@ mod tests {
|
||||
let response = response(id, user());
|
||||
assert_eq!(response.id, id);
|
||||
assert_eq!(response.user.id, Uuid::nil());
|
||||
assert_eq!(response.user.name, Some("Test User".to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,22 +4,31 @@ use uuid::Uuid;
|
||||
use crate::domain;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct UserRequest {}
|
||||
pub struct UserRequest {
|
||||
pub name: Option<String>,
|
||||
}
|
||||
|
||||
impl UserRequest {
|
||||
pub fn into_domain(self, id: Uuid) -> Result<domain::User, domain::DomainError> {
|
||||
Ok(domain::User { id })
|
||||
Ok(domain::User {
|
||||
id,
|
||||
name: self.name,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct UserResponse {
|
||||
pub id: Uuid,
|
||||
pub name: Option<String>,
|
||||
}
|
||||
|
||||
impl From<domain::User> for UserResponse {
|
||||
fn from(user: domain::User) -> Self {
|
||||
Self { id: user.id }
|
||||
Self {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +37,9 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
fn request() -> UserRequest {
|
||||
UserRequest {}
|
||||
UserRequest {
|
||||
name: Some("Test User".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -38,13 +49,16 @@ mod tests {
|
||||
let response = serde_json::to_value(UserResponse::from(user)).unwrap();
|
||||
|
||||
assert_eq!(response["id"], id.to_string());
|
||||
assert_eq!(response["name"], "Test User");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserializes_user_response_as_request() {
|
||||
let user = request().into_domain(Uuid::nil()).unwrap();
|
||||
let response = serde_json::to_value(UserResponse::from(user)).unwrap();
|
||||
let _request: UserRequest = serde_json::from_value(response).unwrap();
|
||||
let request: UserRequest = serde_json::from_value(response).unwrap();
|
||||
|
||||
assert_eq!(request.name, Some("Test User".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -52,8 +66,10 @@ mod tests {
|
||||
let id = Uuid::new_v4();
|
||||
let user = request().into_domain(id).unwrap();
|
||||
assert_eq!(user.id, id);
|
||||
assert_eq!(user.name, Some("Test User".to_string()));
|
||||
|
||||
let response = UserResponse::from(user);
|
||||
assert_eq!(response.id, id);
|
||||
assert_eq!(response.name, Some("Test User".to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user