make account names unique per user not globally

This commit is contained in:
DSeeLP 2025-03-11 11:12:56 +01:00
parent a4e233398f
commit 83c5768935
5 changed files with 11 additions and 14 deletions

View File

@ -7,8 +7,9 @@ create table users(
create table accounts( create table accounts(
id uuid primary key, id uuid primary key,
"user" uuid not null references users(id), "user" uuid not null references users(id),
name varchar(32) not null unique, name varchar(32) not null,
balance bigint not null default 0 constraint positive_balance check (balance >= 0) balance bigint not null default 0 constraint positive_balance check (balance >= 0),
unique ("user", name)
); );
create table transactions( create table transactions(

View File

@ -59,7 +59,7 @@ impl User {
tx.execute(&stmt, &[&id, &name, &hash]) tx.execute(&stmt, &[&id, &name, &hash])
.await .await
.map_err(|err| unique_violation(err, conflict_error))?; .map_err(|err| unique_violation(err, conflict_error))?;
Account::create(&mut tx, Some(id), id, name) Account::create(&mut tx, Some(id), id, "personal")
.await .await
.map_err(|err| unique_violation(err, conflict_error))?; .map_err(|err| unique_violation(err, conflict_error))?;
tx.commit().await?; tx.commit().await?;

View File

@ -12,14 +12,12 @@ GET {{host}}/api/accounts?limit=50
Authorization: Bearer {{token}} Authorization: Bearer {{token}}
HTTP 200 HTTP 200
[Asserts] [Asserts]
jsonpath "$.pagination.total" == 7
jsonpath "$.pagination.limit" == 50 jsonpath "$.pagination.limit" == 50
jsonpath "$.pagination.offset" == 0 jsonpath "$.pagination.offset" == 0
jsonpath "$.result" isCollection jsonpath "$.result" isCollection
jsonpath "$.result[0].name" == "user1" jsonpath "$.result[0].name" == "personal"
jsonpath "$.result[1].name" == "user2" jsonpath "$.result[1].name" == "personal"
jsonpath "$.result[2].name" == "user3" jsonpath "$.result[2].name" == "personal"
jsonpath "$.result[3].name" == "user4" jsonpath "$.result[3].name" == "personal"
jsonpath "$.result[4].name" == "user5" jsonpath "$.result[4].name" == "personal"
jsonpath "$.result[5].name" == "user6" jsonpath "$.result[5].name" == "personal"
jsonpath "$.result[6].name" == "test-user"

View File

@ -13,5 +13,5 @@ Authorization: Bearer {{token}}
HTTP 200 HTTP 200
[Asserts] [Asserts]
jsonpath "$.result" isCollection jsonpath "$.result" isCollection
jsonpath "$.result[0].name" == "user1" jsonpath "$.result[0].name" == "personal"
jsonpath "$.result[0].balance" == 100000 jsonpath "$.result[0].balance" == 100000

View File

@ -12,7 +12,6 @@ GET {{host}}/api/users?limit=50
Authorization: Bearer {{token}} Authorization: Bearer {{token}}
HTTP 200 HTTP 200
[Asserts] [Asserts]
jsonpath "$.pagination.total" == 7
jsonpath "$.pagination.limit" == 50 jsonpath "$.pagination.limit" == 50
jsonpath "$.pagination.offset" == 0 jsonpath "$.pagination.offset" == 0
jsonpath "$.result" isCollection jsonpath "$.result" isCollection
@ -22,4 +21,3 @@ jsonpath "$.result[2].name" == "user3"
jsonpath "$.result[3].name" == "user4" jsonpath "$.result[3].name" == "user4"
jsonpath "$.result[4].name" == "user5" jsonpath "$.result[4].name" == "user5"
jsonpath "$.result[5].name" == "user6" jsonpath "$.result[5].name" == "user6"
jsonpath "$.result[6].name" == "test-user"