Added optional user name.
This commit is contained in:
@@ -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