Added optional user name.
This commit is contained in:
+7
-7
@@ -1,11 +1,11 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n\t\tINSERT INTO users (id)\n\t\tVALUES ($1)\n\t\tON CONFLICT (id) DO UPDATE SET\n id = EXCLUDED.id\n\t\tRETURNING WITH (OLD AS old_row, NEW AS new_row)\n\t\t\told_row.id AS \"old_id?\",\n\t\t\tnew_row.id AS \"id!\"\n\t\t",
|
||||
"query": "\n\t\tSELECT id, name\n\t\tFROM users\n\t\tWHERE id = $1\n\t\t",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "old_id?",
|
||||
"name": "id",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
"Table": {
|
||||
@@ -16,12 +16,12 @@
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "id!",
|
||||
"type_info": "Uuid",
|
||||
"name": "name",
|
||||
"type_info": "Text",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "users",
|
||||
"name": "id"
|
||||
"name": "name"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -33,8 +33,8 @@
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
false
|
||||
true
|
||||
]
|
||||
},
|
||||
"hash": "ee3602e86d747b71e9d7c41965c4c7f58e16790af1303adcfe9686bb35fc741a"
|
||||
"hash": "04663fc2c4ebe3733677026e8bd1841a0114827b8d8770c7aff2e27c99f8514b"
|
||||
}
|
||||
-28
@@ -1,28 +0,0 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n\t\tSELECT id\n\t\tFROM users\n\t\tWHERE id = $1\n\t\t",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "id",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "users",
|
||||
"name": "id"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Uuid"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "22d5813be73a45003abd94152c36756917d705c694b36988b1514067d78fa154"
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n\t\tSELECT id, name\n\t\tFROM users\n\t\tWHERE id = ANY($1)\n\t\t",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "id",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "users",
|
||||
"name": "id"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "name",
|
||||
"type_info": "Text",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "users",
|
||||
"name": "name"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"UuidArray"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
true
|
||||
]
|
||||
},
|
||||
"hash": "443bee847f228636f13fc85b764166a05053329fca285195c109dbfc34784a78"
|
||||
}
|
||||
-28
@@ -1,28 +0,0 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n\t\tSELECT id\n\t\tFROM users\n\t\tWHERE id = ANY($1)\n\t\t",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "id",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "users",
|
||||
"name": "id"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"UuidArray"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "47a30da9fcc527a98f828648ef9948e494d0d25dd2d8b329664e399d8982a3bd"
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n\t\tSELECT id, name\n\t\tFROM users\n\t\tORDER BY id DESC\n\t\tLIMIT $1\n\t\tOFFSET $2\n\t\t",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "id",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "users",
|
||||
"name": "id"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "name",
|
||||
"type_info": "Text",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "users",
|
||||
"name": "name"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Int8",
|
||||
"Int8"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
true
|
||||
]
|
||||
},
|
||||
"hash": "56081e68b4709f6a2bb4b54c8a173c235e744ccb30e2eb1b9fa3b8cd47386268"
|
||||
}
|
||||
+15
-3
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n DELETE FROM users\n WHERE id = $1\n RETURNING id\n ",
|
||||
"query": "\n DELETE FROM users\n WHERE id = $1\n RETURNING id, name\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
@@ -13,6 +13,17 @@
|
||||
"name": "id"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "name",
|
||||
"type_info": "Text",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "users",
|
||||
"name": "name"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
@@ -21,8 +32,9 @@
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
false,
|
||||
true
|
||||
]
|
||||
},
|
||||
"hash": "979e0f8da81ddcc7a89b270f927504defeee5490f63061270b5deee3254c842e"
|
||||
"hash": "7c3bb75adeb8e2eb29970d21bf8a2b6a5d5440a758503ef1bcdd74fcf6315d99"
|
||||
}
|
||||
-29
@@ -1,29 +0,0 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n\t\tSELECT id\n\t\tFROM users\n\t\tORDER BY id DESC\n\t\tLIMIT $1\n\t\tOFFSET $2\n\t\t",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "id",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "users",
|
||||
"name": "id"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Int8",
|
||||
"Int8"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "87aa6dcbad3e6911c33c93ae02e001ec762ed8877a4a0bc66e195bdb1c113135"
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n\t\tINSERT INTO users (id, name)\n\t\tVALUES ($1, $2)\n\t\tON CONFLICT (id) DO UPDATE SET\n name = EXCLUDED.name\n\t\tRETURNING WITH (OLD AS old_row, NEW AS new_row)\n\t\t\told_row.id AS \"old_id?\",\n\t\t\told_row.name AS \"old_name?\",\n\t\t\tnew_row.id AS \"id!\",\n\t\t\tnew_row.name AS \"name?\"\n\t\t",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "old_id?",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "users",
|
||||
"name": "id"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "old_name?",
|
||||
"type_info": "Text",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "users",
|
||||
"name": "name"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 2,
|
||||
"name": "id!",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "users",
|
||||
"name": "id"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 3,
|
||||
"name": "name?",
|
||||
"type_info": "Text",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "users",
|
||||
"name": "name"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Uuid",
|
||||
"Text"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
true,
|
||||
false,
|
||||
true
|
||||
]
|
||||
},
|
||||
"hash": "a185e03615a65324b1fbf9c97213a1e949353589679fd5689f88af14b3dda682"
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
CREATE TABLE users (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid ()
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
|
||||
name TEXT
|
||||
);
|
||||
@@ -2,4 +2,5 @@ use uuid::Uuid;
|
||||
|
||||
pub struct UserRow {
|
||||
pub id: Uuid,
|
||||
pub name: Option<String>,
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ where
|
||||
sqlx::query_as!(
|
||||
UserRow,
|
||||
r#"
|
||||
SELECT id
|
||||
SELECT id, name
|
||||
FROM users
|
||||
ORDER BY id DESC
|
||||
LIMIT $1
|
||||
@@ -38,7 +38,7 @@ where
|
||||
sqlx::query_as!(
|
||||
UserRow,
|
||||
r#"
|
||||
SELECT id
|
||||
SELECT id, name
|
||||
FROM users
|
||||
WHERE id = $1
|
||||
"#,
|
||||
@@ -56,7 +56,7 @@ where
|
||||
sqlx::query_as!(
|
||||
UserRow,
|
||||
r#"
|
||||
SELECT id
|
||||
SELECT id, name
|
||||
FROM users
|
||||
WHERE id = ANY($1)
|
||||
"#,
|
||||
@@ -73,23 +73,32 @@ where
|
||||
{
|
||||
let row = sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO users (id)
|
||||
VALUES ($1)
|
||||
INSERT INTO users (id, name)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
id = EXCLUDED.id
|
||||
name = EXCLUDED.name
|
||||
RETURNING WITH (OLD AS old_row, NEW AS new_row)
|
||||
old_row.id AS "old_id?",
|
||||
new_row.id AS "id!"
|
||||
old_row.name AS "old_name?",
|
||||
new_row.id AS "id!",
|
||||
new_row.name AS "name?"
|
||||
"#,
|
||||
user.id,
|
||||
user.name,
|
||||
)
|
||||
.fetch_one(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
Ok(Upsert {
|
||||
previous: row.old_id.map(|id| UserRow { id }),
|
||||
current: UserRow { id: row.id },
|
||||
previous: row.old_id.map(|id| UserRow {
|
||||
id,
|
||||
name: row.old_name,
|
||||
}),
|
||||
current: UserRow {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -102,7 +111,7 @@ where
|
||||
r#"
|
||||
DELETE FROM users
|
||||
WHERE id = $1
|
||||
RETURNING id
|
||||
RETURNING id, name
|
||||
"#,
|
||||
id
|
||||
)
|
||||
|
||||
@@ -3,4 +3,5 @@ use uuid::Uuid;
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct User {
|
||||
pub id: Uuid,
|
||||
pub name: Option<String>,
|
||||
}
|
||||
|
||||
@@ -111,11 +111,17 @@ fn classify_delete_error(error: DatabaseError) -> RepositoryError {
|
||||
}
|
||||
|
||||
fn decode(row: UserRow) -> Result<User, DecodeError> {
|
||||
Ok(User { id: row.id })
|
||||
Ok(User {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
})
|
||||
}
|
||||
|
||||
fn encode(user: User) -> UserRow {
|
||||
UserRow { id: user.id }
|
||||
UserRow {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -153,7 +159,10 @@ mod tests {
|
||||
}
|
||||
|
||||
fn row() -> UserRow {
|
||||
UserRow { id: Uuid::new_v4() }
|
||||
UserRow {
|
||||
id: Uuid::new_v4(),
|
||||
name: Some("Test User".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -163,6 +172,7 @@ mod tests {
|
||||
let user = decode(row).unwrap();
|
||||
|
||||
assert_eq!(user.id, id);
|
||||
assert_eq!(user.name, Some("Test User".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -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