Standardized the upsert and delete pattern across the repo.
This commit is contained in:
+88
@@ -0,0 +1,88 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n DELETE FROM transactions\n WHERE id = $1\n RETURNING id, account_id, amount_cents, direction, date, description\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "id",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "transactions",
|
||||
"name": "id"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "account_id",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "transactions",
|
||||
"name": "account_id"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 2,
|
||||
"name": "amount_cents",
|
||||
"type_info": "Int8",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "transactions",
|
||||
"name": "amount_cents"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 3,
|
||||
"name": "direction",
|
||||
"type_info": "Text",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "transactions",
|
||||
"name": "direction"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 4,
|
||||
"name": "date",
|
||||
"type_info": "Timestamptz",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "transactions",
|
||||
"name": "date"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 5,
|
||||
"name": "description",
|
||||
"type_info": "Text",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "transactions",
|
||||
"name": "description"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Uuid"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "0c75096fe78c303e4676880840fc768b4150dfb9a0654229bf6581bd67494eb3"
|
||||
}
|
||||
+34
-6
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n INSERT INTO user_app_secrets (id, app_id, secret_hash, expires_at)\n VALUES ($1, $2, $3, $4)\n ON CONFLICT (id, app_id) DO UPDATE SET\n secret_hash = EXCLUDED.secret_hash,\n expires_at = EXCLUDED.expires_at\n RETURNING WITH (OLD AS old_row, NEW AS new_row)\n old_row.id AS \"old_id?\",\n new_row.id AS \"id!\",\n new_row.app_id AS \"app_id!\",\n new_row.secret_hash AS \"secret_hash!\",\n new_row.expires_at AS \"expires_at!\",\n new_row.created_at AS \"created_at!\"\n ",
|
||||
"query": "\n INSERT INTO user_app_secrets (id, app_id, secret_hash, expires_at)\n VALUES ($1, $2, $3, $4)\n ON CONFLICT (id, app_id) DO UPDATE SET\n secret_hash = EXCLUDED.secret_hash,\n expires_at = EXCLUDED.expires_at\n RETURNING WITH (OLD AS old_row, NEW AS new_row)\n old_row.id AS \"old_id?\",\n COALESCE(old_row.app_id, new_row.app_id) AS \"old_app_id!\",\n COALESCE(old_row.secret_hash, new_row.secret_hash) AS \"old_secret_hash!\",\n COALESCE(old_row.expires_at, new_row.expires_at) AS \"old_expires_at!\",\n COALESCE(old_row.created_at, new_row.created_at) AS \"old_created_at!\",\n new_row.id AS \"id!\",\n new_row.app_id AS \"app_id!\",\n new_row.secret_hash AS \"secret_hash!\",\n new_row.expires_at AS \"expires_at!\",\n new_row.created_at AS \"created_at!\"\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
@@ -16,6 +16,30 @@
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "old_app_id!",
|
||||
"type_info": "Uuid",
|
||||
"origin": "Expression"
|
||||
},
|
||||
{
|
||||
"ordinal": 2,
|
||||
"name": "old_secret_hash!",
|
||||
"type_info": "Bytea",
|
||||
"origin": "Expression"
|
||||
},
|
||||
{
|
||||
"ordinal": 3,
|
||||
"name": "old_expires_at!",
|
||||
"type_info": "Timestamptz",
|
||||
"origin": "Expression"
|
||||
},
|
||||
{
|
||||
"ordinal": 4,
|
||||
"name": "old_created_at!",
|
||||
"type_info": "Timestamptz",
|
||||
"origin": "Expression"
|
||||
},
|
||||
{
|
||||
"ordinal": 5,
|
||||
"name": "id!",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
@@ -26,7 +50,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 2,
|
||||
"ordinal": 6,
|
||||
"name": "app_id!",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
@@ -37,7 +61,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 3,
|
||||
"ordinal": 7,
|
||||
"name": "secret_hash!",
|
||||
"type_info": "Bytea",
|
||||
"origin": {
|
||||
@@ -48,7 +72,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 4,
|
||||
"ordinal": 8,
|
||||
"name": "expires_at!",
|
||||
"type_info": "Timestamptz",
|
||||
"origin": {
|
||||
@@ -59,7 +83,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 5,
|
||||
"ordinal": 9,
|
||||
"name": "created_at!",
|
||||
"type_info": "Timestamptz",
|
||||
"origin": {
|
||||
@@ -80,6 +104,10 @@
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
@@ -87,5 +115,5 @@
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "594090231079a7874dbe6d8b9696c92e30ca688f496cc02c79f265f870540153"
|
||||
"hash": "18f30fc05dd48c2c65659cf16f167207303707d544f8bede6407790d97c8b927"
|
||||
}
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n DELETE FROM user_app_secrets\n WHERE app_id = $1 AND id = $2\n RETURNING id, app_id, secret_hash, expires_at, created_at\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "id",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "user_app_secrets",
|
||||
"name": "id"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "app_id",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "user_app_secrets",
|
||||
"name": "app_id"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 2,
|
||||
"name": "secret_hash",
|
||||
"type_info": "Bytea",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "user_app_secrets",
|
||||
"name": "secret_hash"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 3,
|
||||
"name": "expires_at",
|
||||
"type_info": "Timestamptz",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "user_app_secrets",
|
||||
"name": "expires_at"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 4,
|
||||
"name": "created_at",
|
||||
"type_info": "Timestamptz",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "user_app_secrets",
|
||||
"name": "created_at"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Uuid",
|
||||
"Uuid"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "209942ca0122a766d9170ceb6e53171788ff889717615205cbc6f8bde9a2fe63"
|
||||
}
|
||||
-28
@@ -1,28 +0,0 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "DELETE FROM recurring_transactions WHERE id = $1 RETURNING id",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "id",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "recurring_transactions",
|
||||
"name": "id"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Uuid"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "20f1027f06ea416d4f0d0a5b231ec8dde2291c3c49a937a8fa4612646e191420"
|
||||
}
|
||||
+42
-7
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n INSERT INTO transactions (id, account_id, amount_cents, direction, date, description)\n VALUES ($1, $2, $3, $4, $5, $6)\n\t\tON CONFLICT (id) DO UPDATE SET\n account_id = EXCLUDED.account_id,\n\t\t\tamount_cents = EXCLUDED.amount_cents,\n\t\t\tdirection = EXCLUDED.direction,\n\t\t\tdate = EXCLUDED.date,\n\t\t\tdescription = EXCLUDED.description\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 new_row.account_id AS \"account_id!\",\n\t\t\tnew_row.amount_cents AS \"amount_cents!\",\n\t\t\tnew_row.direction AS \"direction!\",\n\t\t\tnew_row.date AS \"date!\",\n\t\t\tnew_row.description AS \"description!\"\n\t\t",
|
||||
"query": "\n INSERT INTO transactions (id, account_id, amount_cents, direction, date, description)\n VALUES ($1, $2, $3, $4, $5, $6)\n\t\tON CONFLICT (id) DO UPDATE SET\n account_id = EXCLUDED.account_id,\n\t\t\tamount_cents = EXCLUDED.amount_cents,\n\t\t\tdirection = EXCLUDED.direction,\n\t\t\tdate = EXCLUDED.date,\n\t\t\tdescription = EXCLUDED.description\n\t\tRETURNING WITH (OLD AS old_row, NEW AS new_row)\n\t\t\told_row.id AS \"old_id?\",\n\t\t\tCOALESCE(old_row.account_id, new_row.account_id) AS \"old_account_id!\",\n\t\t\tCOALESCE(old_row.amount_cents, new_row.amount_cents) AS \"old_amount_cents!\",\n\t\t\tCOALESCE(old_row.direction, new_row.direction) AS \"old_direction!\",\n\t\t\tCOALESCE(old_row.date, new_row.date) AS \"old_date!\",\n\t\t\tCOALESCE(old_row.description, new_row.description) AS \"old_description!\",\n\t\t\tnew_row.id AS \"id!\",\n new_row.account_id AS \"account_id!\",\n\t\t\tnew_row.amount_cents AS \"amount_cents!\",\n\t\t\tnew_row.direction AS \"direction!\",\n\t\t\tnew_row.date AS \"date!\",\n\t\t\tnew_row.description AS \"description!\"\n\t\t",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
@@ -16,6 +16,36 @@
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "old_account_id!",
|
||||
"type_info": "Uuid",
|
||||
"origin": "Expression"
|
||||
},
|
||||
{
|
||||
"ordinal": 2,
|
||||
"name": "old_amount_cents!",
|
||||
"type_info": "Int8",
|
||||
"origin": "Expression"
|
||||
},
|
||||
{
|
||||
"ordinal": 3,
|
||||
"name": "old_direction!",
|
||||
"type_info": "Text",
|
||||
"origin": "Expression"
|
||||
},
|
||||
{
|
||||
"ordinal": 4,
|
||||
"name": "old_date!",
|
||||
"type_info": "Timestamptz",
|
||||
"origin": "Expression"
|
||||
},
|
||||
{
|
||||
"ordinal": 5,
|
||||
"name": "old_description!",
|
||||
"type_info": "Text",
|
||||
"origin": "Expression"
|
||||
},
|
||||
{
|
||||
"ordinal": 6,
|
||||
"name": "id!",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
@@ -26,7 +56,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 2,
|
||||
"ordinal": 7,
|
||||
"name": "account_id!",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
@@ -37,7 +67,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 3,
|
||||
"ordinal": 8,
|
||||
"name": "amount_cents!",
|
||||
"type_info": "Int8",
|
||||
"origin": {
|
||||
@@ -48,7 +78,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 4,
|
||||
"ordinal": 9,
|
||||
"name": "direction!",
|
||||
"type_info": "Text",
|
||||
"origin": {
|
||||
@@ -59,7 +89,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 5,
|
||||
"ordinal": 10,
|
||||
"name": "date!",
|
||||
"type_info": "Timestamptz",
|
||||
"origin": {
|
||||
@@ -70,7 +100,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 6,
|
||||
"ordinal": 11,
|
||||
"name": "description!",
|
||||
"type_info": "Text",
|
||||
"origin": {
|
||||
@@ -93,6 +123,11 @@
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
@@ -101,5 +136,5 @@
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "8999f47eebb460120538c7d2bdbb40dadf634e98977ae59d2398579f9be82d6f"
|
||||
"hash": "26a5b764d38ca78508087d1e793ae81530064a3ae661ff3c73c7cd686097aa81"
|
||||
}
|
||||
-29
@@ -1,29 +0,0 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "DELETE FROM user_app_secrets WHERE app_id = $1 AND id = $2 RETURNING id",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "id",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "user_app_secrets",
|
||||
"name": "id"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Uuid",
|
||||
"Uuid"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "28650c14def0b3089a1c6f10d9947581bbc02bdac145f58d3c97d1d11671f949"
|
||||
}
|
||||
-28
@@ -1,28 +0,0 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "DELETE FROM transactions WHERE id = $1 RETURNING id",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "id",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "transactions",
|
||||
"name": "id"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Uuid"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "3093183e1ab856c510b7a6a32c05210f18f845cb70fc3e5882dd38db4f23ec14"
|
||||
}
|
||||
+10
-3
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n\t\tINSERT INTO accounts (id, name)\n\t\tVALUES ($1, $2)\n\t\tON CONFLICT (id) DO UPDATE SET\n\t\t\tname = 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\tnew_row.id AS \"id!\",\n\t\t\tnew_row.name AS \"name!\"\n\t\t",
|
||||
"query": "\n\t\tINSERT INTO accounts (id, name)\n\t\tVALUES ($1, $2)\n\t\tON CONFLICT (id) DO UPDATE SET\n\t\t\tname = 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\tCOALESCE(old_row.name, new_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": [
|
||||
{
|
||||
@@ -16,6 +16,12 @@
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "old_name!",
|
||||
"type_info": "Text",
|
||||
"origin": "Expression"
|
||||
},
|
||||
{
|
||||
"ordinal": 2,
|
||||
"name": "id!",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
@@ -26,7 +32,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 2,
|
||||
"ordinal": 3,
|
||||
"name": "name!",
|
||||
"type_info": "Text",
|
||||
"origin": {
|
||||
@@ -45,9 +51,10 @@
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
null,
|
||||
false,
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "ab6330c86c647fc7b8085152087066269b95eab3d60ad9d97219e93686a85a55"
|
||||
"hash": "3b4b5bac598a8ef53be0c31ae751fb7e69f2e0b2ee18d628035fc198dadb4b9c"
|
||||
}
|
||||
+84
-11
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n INSERT INTO recurring_transactions (\n id,\n name,\n account_id,\n amount_cents,\n direction,\n starts_at,\n ends_at,\n recurrence_every,\n recurrence_unit,\n weekend_adjustment\n )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)\n ON CONFLICT (id) DO UPDATE SET\n name = EXCLUDED.name,\n account_id = EXCLUDED.account_id,\n amount_cents = EXCLUDED.amount_cents,\n direction = EXCLUDED.direction,\n starts_at = EXCLUDED.starts_at,\n ends_at = EXCLUDED.ends_at,\n recurrence_every = EXCLUDED.recurrence_every,\n recurrence_unit = EXCLUDED.recurrence_unit,\n weekend_adjustment = EXCLUDED.weekend_adjustment\n RETURNING WITH (OLD AS old_row, NEW AS new_row)\n old_row.id AS \"old_id?\",\n new_row.id AS \"id!\",\n new_row.name AS \"name!\",\n new_row.account_id AS \"account_id!\",\n new_row.amount_cents AS \"amount_cents!\",\n new_row.direction AS \"direction!\",\n new_row.starts_at AS \"starts_at!\",\n new_row.ends_at AS \"ends_at?\",\n new_row.recurrence_every AS \"recurrence_every!\",\n new_row.recurrence_unit AS \"recurrence_unit!\",\n new_row.weekend_adjustment AS \"weekend_adjustment?\"\n ",
|
||||
"query": "\n INSERT INTO recurring_transactions (\n id,\n name,\n account_id,\n amount_cents,\n direction,\n starts_at,\n ends_at,\n recurrence_every,\n recurrence_unit,\n weekend_adjustment\n )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)\n ON CONFLICT (id) DO UPDATE SET\n name = EXCLUDED.name,\n account_id = EXCLUDED.account_id,\n amount_cents = EXCLUDED.amount_cents,\n direction = EXCLUDED.direction,\n starts_at = EXCLUDED.starts_at,\n ends_at = EXCLUDED.ends_at,\n recurrence_every = EXCLUDED.recurrence_every,\n recurrence_unit = EXCLUDED.recurrence_unit,\n weekend_adjustment = EXCLUDED.weekend_adjustment\n RETURNING WITH (OLD AS old_row, NEW AS new_row)\n old_row.id AS \"old_id?\",\n COALESCE(old_row.name, new_row.name) AS \"old_name!\",\n COALESCE(old_row.account_id, new_row.account_id) AS \"old_account_id!\",\n COALESCE(old_row.amount_cents, new_row.amount_cents) AS \"old_amount_cents!\",\n COALESCE(old_row.direction, new_row.direction) AS \"old_direction!\",\n COALESCE(old_row.starts_at, new_row.starts_at) AS \"old_starts_at!\",\n old_row.ends_at AS \"old_ends_at?\",\n COALESCE(old_row.recurrence_every, new_row.recurrence_every) AS \"old_recurrence_every!\",\n COALESCE(old_row.recurrence_unit, new_row.recurrence_unit) AS \"old_recurrence_unit!\",\n old_row.weekend_adjustment AS \"old_weekend_adjustment?\",\n new_row.id AS \"id!\",\n new_row.name AS \"name!\",\n new_row.account_id AS \"account_id!\",\n new_row.amount_cents AS \"amount_cents!\",\n new_row.direction AS \"direction!\",\n new_row.starts_at AS \"starts_at!\",\n new_row.ends_at AS \"ends_at?\",\n new_row.recurrence_every AS \"recurrence_every!\",\n new_row.recurrence_unit AS \"recurrence_unit!\",\n new_row.weekend_adjustment AS \"weekend_adjustment?\"\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
@@ -16,6 +16,70 @@
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "old_name!",
|
||||
"type_info": "Text",
|
||||
"origin": "Expression"
|
||||
},
|
||||
{
|
||||
"ordinal": 2,
|
||||
"name": "old_account_id!",
|
||||
"type_info": "Uuid",
|
||||
"origin": "Expression"
|
||||
},
|
||||
{
|
||||
"ordinal": 3,
|
||||
"name": "old_amount_cents!",
|
||||
"type_info": "Int8",
|
||||
"origin": "Expression"
|
||||
},
|
||||
{
|
||||
"ordinal": 4,
|
||||
"name": "old_direction!",
|
||||
"type_info": "Text",
|
||||
"origin": "Expression"
|
||||
},
|
||||
{
|
||||
"ordinal": 5,
|
||||
"name": "old_starts_at!",
|
||||
"type_info": "Timestamptz",
|
||||
"origin": "Expression"
|
||||
},
|
||||
{
|
||||
"ordinal": 6,
|
||||
"name": "old_ends_at?",
|
||||
"type_info": "Timestamptz",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "recurring_transactions",
|
||||
"name": "ends_at"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 7,
|
||||
"name": "old_recurrence_every!",
|
||||
"type_info": "Int4",
|
||||
"origin": "Expression"
|
||||
},
|
||||
{
|
||||
"ordinal": 8,
|
||||
"name": "old_recurrence_unit!",
|
||||
"type_info": "Text",
|
||||
"origin": "Expression"
|
||||
},
|
||||
{
|
||||
"ordinal": 9,
|
||||
"name": "old_weekend_adjustment?",
|
||||
"type_info": "Text",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "recurring_transactions",
|
||||
"name": "weekend_adjustment"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 10,
|
||||
"name": "id!",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
@@ -26,7 +90,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 2,
|
||||
"ordinal": 11,
|
||||
"name": "name!",
|
||||
"type_info": "Text",
|
||||
"origin": {
|
||||
@@ -37,7 +101,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 3,
|
||||
"ordinal": 12,
|
||||
"name": "account_id!",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
@@ -48,7 +112,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 4,
|
||||
"ordinal": 13,
|
||||
"name": "amount_cents!",
|
||||
"type_info": "Int8",
|
||||
"origin": {
|
||||
@@ -59,7 +123,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 5,
|
||||
"ordinal": 14,
|
||||
"name": "direction!",
|
||||
"type_info": "Text",
|
||||
"origin": {
|
||||
@@ -70,7 +134,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 6,
|
||||
"ordinal": 15,
|
||||
"name": "starts_at!",
|
||||
"type_info": "Timestamptz",
|
||||
"origin": {
|
||||
@@ -81,7 +145,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 7,
|
||||
"ordinal": 16,
|
||||
"name": "ends_at?",
|
||||
"type_info": "Timestamptz",
|
||||
"origin": {
|
||||
@@ -92,7 +156,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 8,
|
||||
"ordinal": 17,
|
||||
"name": "recurrence_every!",
|
||||
"type_info": "Int4",
|
||||
"origin": {
|
||||
@@ -103,7 +167,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 9,
|
||||
"ordinal": 18,
|
||||
"name": "recurrence_unit!",
|
||||
"type_info": "Text",
|
||||
"origin": {
|
||||
@@ -114,7 +178,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 10,
|
||||
"ordinal": 19,
|
||||
"name": "weekend_adjustment?",
|
||||
"type_info": "Text",
|
||||
"origin": {
|
||||
@@ -141,6 +205,15 @@
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
true,
|
||||
null,
|
||||
null,
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
@@ -153,5 +226,5 @@
|
||||
true
|
||||
]
|
||||
},
|
||||
"hash": "7d7363a1746170ceec82763cadcf2e0803753901e73a2a8e04c87eb061bb724e"
|
||||
"hash": "8bc3dbca6ff74e9775321d64337db88dfab2bf30f8128fcb81a8c980337da483"
|
||||
}
|
||||
+14
-2
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "DELETE FROM accounts WHERE id = $1 RETURNING id",
|
||||
"query": "DELETE FROM accounts WHERE id = $1 RETURNING id, name",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
@@ -13,6 +13,17 @@
|
||||
"name": "id"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "name",
|
||||
"type_info": "Text",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "accounts",
|
||||
"name": "name"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
@@ -21,8 +32,9 @@
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "b755bf49d60419830ddaf5855268dd31e1f82bef6b4fd0046a4c5cbffb3e877a"
|
||||
"hash": "910c19f1022dcf5923c64f43cd3c55b6cf7a41a4907fc70d09b0b4770209acc3"
|
||||
}
|
||||
+136
@@ -0,0 +1,136 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n DELETE FROM recurring_transactions\n WHERE id = $1\n RETURNING\n id,\n name,\n account_id,\n amount_cents,\n direction,\n starts_at,\n ends_at,\n recurrence_every,\n recurrence_unit,\n weekend_adjustment\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "id",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "recurring_transactions",
|
||||
"name": "id"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 1,
|
||||
"name": "name",
|
||||
"type_info": "Text",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "recurring_transactions",
|
||||
"name": "name"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 2,
|
||||
"name": "account_id",
|
||||
"type_info": "Uuid",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "recurring_transactions",
|
||||
"name": "account_id"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 3,
|
||||
"name": "amount_cents",
|
||||
"type_info": "Int8",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "recurring_transactions",
|
||||
"name": "amount_cents"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 4,
|
||||
"name": "direction",
|
||||
"type_info": "Text",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "recurring_transactions",
|
||||
"name": "direction"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 5,
|
||||
"name": "starts_at",
|
||||
"type_info": "Timestamptz",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "recurring_transactions",
|
||||
"name": "starts_at"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 6,
|
||||
"name": "ends_at",
|
||||
"type_info": "Timestamptz",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "recurring_transactions",
|
||||
"name": "ends_at"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 7,
|
||||
"name": "recurrence_every",
|
||||
"type_info": "Int4",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "recurring_transactions",
|
||||
"name": "recurrence_every"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 8,
|
||||
"name": "recurrence_unit",
|
||||
"type_info": "Text",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "recurring_transactions",
|
||||
"name": "recurrence_unit"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"ordinal": 9,
|
||||
"name": "weekend_adjustment",
|
||||
"type_info": "Text",
|
||||
"origin": {
|
||||
"Table": {
|
||||
"table": "recurring_transactions",
|
||||
"name": "weekend_adjustment"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Uuid"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
true
|
||||
]
|
||||
},
|
||||
"hash": "be72d1cc51b27ae37afd44d85fe29e9226bcf428701dd931f9d03673729f11a2"
|
||||
}
|
||||
@@ -2,7 +2,7 @@ use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DatabaseError,
|
||||
dtos::{AccountRow, Delete, Upsert},
|
||||
dtos::{AccountRow, Upsert},
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
@@ -85,6 +85,7 @@ where
|
||||
name = EXCLUDED.name
|
||||
RETURNING WITH (OLD AS old_row, NEW AS new_row)
|
||||
old_row.id AS "old_id?",
|
||||
COALESCE(old_row.name, new_row.name) AS "old_name!",
|
||||
new_row.id AS "id!",
|
||||
new_row.name AS "name!"
|
||||
"#,
|
||||
@@ -95,30 +96,33 @@ where
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
let account = AccountRow {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
};
|
||||
// `old_id` indicates whether there was a previous row before the upsert, other old fields are
|
||||
// coalesced with the new row's values in case there was no previous row. This enables static
|
||||
// typing on the previous row instead of having to check nullability for each old field in code.
|
||||
let previous = row.old_id.map(|id| AccountRow {
|
||||
id,
|
||||
name: row.old_name,
|
||||
});
|
||||
|
||||
if row.old_id.is_some() {
|
||||
Ok(Upsert::Updated(account))
|
||||
} else {
|
||||
Ok(Upsert::Created(account))
|
||||
}
|
||||
Ok(Upsert {
|
||||
previous,
|
||||
current: AccountRow {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, DatabaseError>
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<AccountRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let deleted = sqlx::query!("DELETE FROM accounts WHERE id = $1 RETURNING id", id)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
Ok(if deleted.is_some() {
|
||||
Delete::Deleted
|
||||
} else {
|
||||
Delete::NotFound
|
||||
})
|
||||
sqlx::query_as!(
|
||||
AccountRow,
|
||||
"DELETE FROM accounts WHERE id = $1 RETURNING id, name",
|
||||
id,
|
||||
)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
|
||||
@@ -1,25 +1,11 @@
|
||||
pub enum Upsert<T> {
|
||||
Created(T),
|
||||
Updated(T),
|
||||
}
|
||||
|
||||
impl<T> Upsert<T> {
|
||||
pub fn try_map<U, E>(self, f: impl FnOnce(T) -> Result<U, E>) -> Result<Upsert<U>, E> {
|
||||
Ok(match self {
|
||||
Self::Created(value) => Upsert::Created(f(value)?),
|
||||
Self::Updated(value) => Upsert::Updated(f(value)?),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Upserted<T> {
|
||||
pub struct Upsert<T> {
|
||||
pub current: T,
|
||||
pub previous: Option<T>,
|
||||
}
|
||||
|
||||
impl<T> Upserted<T> {
|
||||
pub fn try_map<U, E>(self, mut f: impl FnMut(T) -> Result<U, E>) -> Result<Upserted<U>, E> {
|
||||
Ok(Upserted {
|
||||
impl<T> Upsert<T> {
|
||||
pub fn try_map<U, E>(self, mut f: impl FnMut(T) -> Result<U, E>) -> Result<Upsert<U>, E> {
|
||||
Ok(Upsert {
|
||||
current: f(self.current)?,
|
||||
previous: match self.previous {
|
||||
Some(value) => Some(f(value)?),
|
||||
@@ -28,8 +14,3 @@ impl<T> Upserted<T> {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Delete {
|
||||
Deleted,
|
||||
NotFound,
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DatabaseError,
|
||||
dtos::{Delete, RecurringTransactionRow, Upsert},
|
||||
dtos::{RecurringTransactionRow, Upsert},
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
@@ -108,6 +108,15 @@ where
|
||||
weekend_adjustment = EXCLUDED.weekend_adjustment
|
||||
RETURNING WITH (OLD AS old_row, NEW AS new_row)
|
||||
old_row.id AS "old_id?",
|
||||
COALESCE(old_row.name, new_row.name) AS "old_name!",
|
||||
COALESCE(old_row.account_id, new_row.account_id) AS "old_account_id!",
|
||||
COALESCE(old_row.amount_cents, new_row.amount_cents) AS "old_amount_cents!",
|
||||
COALESCE(old_row.direction, new_row.direction) AS "old_direction!",
|
||||
COALESCE(old_row.starts_at, new_row.starts_at) AS "old_starts_at!",
|
||||
old_row.ends_at AS "old_ends_at?",
|
||||
COALESCE(old_row.recurrence_every, new_row.recurrence_every) AS "old_recurrence_every!",
|
||||
COALESCE(old_row.recurrence_unit, new_row.recurrence_unit) AS "old_recurrence_unit!",
|
||||
old_row.weekend_adjustment AS "old_weekend_adjustment?",
|
||||
new_row.id AS "id!",
|
||||
new_row.name AS "name!",
|
||||
new_row.account_id AS "account_id!",
|
||||
@@ -134,41 +143,66 @@ where
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
let transaction = RecurringTransactionRow {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
account_id: row.account_id,
|
||||
amount_cents: row.amount_cents,
|
||||
direction: row.direction,
|
||||
starts_at: row.starts_at,
|
||||
ends_at: row.ends_at,
|
||||
recurrence_every: row.recurrence_every,
|
||||
recurrence_unit: row.recurrence_unit,
|
||||
weekend_adjustment: row.weekend_adjustment,
|
||||
};
|
||||
// `old_id` indicates whether there was a previous row before the upsert, other old fields are
|
||||
// coalesced with the new row's values in case there was no previous row. This enables static
|
||||
// typing on the previous row instead of having to check nullability for each old field in code.
|
||||
let previous = row.old_id.map(|id| RecurringTransactionRow {
|
||||
id,
|
||||
name: row.old_name,
|
||||
account_id: row.old_account_id,
|
||||
amount_cents: row.old_amount_cents,
|
||||
direction: row.old_direction,
|
||||
starts_at: row.old_starts_at,
|
||||
ends_at: row.old_ends_at,
|
||||
recurrence_every: row.old_recurrence_every,
|
||||
recurrence_unit: row.old_recurrence_unit,
|
||||
weekend_adjustment: row.old_weekend_adjustment,
|
||||
});
|
||||
|
||||
if row.old_id.is_some() {
|
||||
Ok(Upsert::Updated(transaction))
|
||||
} else {
|
||||
Ok(Upsert::Created(transaction))
|
||||
}
|
||||
Ok(Upsert {
|
||||
previous,
|
||||
current: RecurringTransactionRow {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
account_id: row.account_id,
|
||||
amount_cents: row.amount_cents,
|
||||
direction: row.direction,
|
||||
starts_at: row.starts_at,
|
||||
ends_at: row.ends_at,
|
||||
recurrence_every: row.recurrence_every,
|
||||
recurrence_unit: row.recurrence_unit,
|
||||
weekend_adjustment: row.weekend_adjustment,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, DatabaseError>
|
||||
pub async fn delete_by_id<'e, E>(
|
||||
executor: E,
|
||||
id: Uuid,
|
||||
) -> Result<Option<RecurringTransactionRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let deleted = sqlx::query!(
|
||||
"DELETE FROM recurring_transactions WHERE id = $1 RETURNING id",
|
||||
sqlx::query_as!(
|
||||
RecurringTransactionRow,
|
||||
r#"
|
||||
DELETE FROM recurring_transactions
|
||||
WHERE id = $1
|
||||
RETURNING
|
||||
id,
|
||||
name,
|
||||
account_id,
|
||||
amount_cents,
|
||||
direction,
|
||||
starts_at,
|
||||
ends_at,
|
||||
recurrence_every,
|
||||
recurrence_unit,
|
||||
weekend_adjustment
|
||||
"#,
|
||||
id,
|
||||
)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
Ok(if deleted.is_some() {
|
||||
Delete::Deleted
|
||||
} else {
|
||||
Delete::NotFound
|
||||
})
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DatabaseError,
|
||||
dtos::{Delete, TransactionRow, Upsert},
|
||||
dtos::{TransactionRow, Upsert},
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
@@ -71,6 +71,11 @@ where
|
||||
description = EXCLUDED.description
|
||||
RETURNING WITH (OLD AS old_row, NEW AS new_row)
|
||||
old_row.id AS "old_id?",
|
||||
COALESCE(old_row.account_id, new_row.account_id) AS "old_account_id!",
|
||||
COALESCE(old_row.amount_cents, new_row.amount_cents) AS "old_amount_cents!",
|
||||
COALESCE(old_row.direction, new_row.direction) AS "old_direction!",
|
||||
COALESCE(old_row.date, new_row.date) AS "old_date!",
|
||||
COALESCE(old_row.description, new_row.description) AS "old_description!",
|
||||
new_row.id AS "id!",
|
||||
new_row.account_id AS "account_id!",
|
||||
new_row.amount_cents AS "amount_cents!",
|
||||
@@ -89,34 +94,48 @@ where
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
let transaction = TransactionRow {
|
||||
id: row.id,
|
||||
account_id: row.account_id,
|
||||
amount_cents: row.amount_cents,
|
||||
direction: row.direction,
|
||||
date: row.date,
|
||||
description: row.description,
|
||||
};
|
||||
// `old_id` indicates whether there was a previous row before the upsert, other old fields are
|
||||
// coalesced with the new row's values in case there was no previous row. This enables static
|
||||
// typing on the previous row instead of having to check nullability for each old field in code.
|
||||
let previous = row.old_id.map(|id| TransactionRow {
|
||||
id,
|
||||
account_id: row.old_account_id,
|
||||
amount_cents: row.old_amount_cents,
|
||||
direction: row.old_direction,
|
||||
date: row.old_date,
|
||||
description: row.old_description,
|
||||
});
|
||||
|
||||
if row.old_id.is_some() {
|
||||
Ok(Upsert::Updated(transaction))
|
||||
} else {
|
||||
Ok(Upsert::Created(transaction))
|
||||
}
|
||||
Ok(Upsert {
|
||||
previous,
|
||||
current: TransactionRow {
|
||||
id: row.id,
|
||||
account_id: row.account_id,
|
||||
amount_cents: row.amount_cents,
|
||||
direction: row.direction,
|
||||
date: row.date,
|
||||
description: row.description,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, DatabaseError>
|
||||
pub async fn delete_by_id<'e, E>(
|
||||
executor: E,
|
||||
id: Uuid,
|
||||
) -> Result<Option<TransactionRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let deleted = sqlx::query!("DELETE FROM transactions WHERE id = $1 RETURNING id", id)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
Ok(if deleted.is_some() {
|
||||
Delete::Deleted
|
||||
} else {
|
||||
Delete::NotFound
|
||||
})
|
||||
sqlx::query_as!(
|
||||
TransactionRow,
|
||||
r#"
|
||||
DELETE FROM transactions
|
||||
WHERE id = $1
|
||||
RETURNING id, account_id, amount_cents, direction, date, description
|
||||
"#,
|
||||
id,
|
||||
)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DatabaseError,
|
||||
dtos::{Delete, Upsert, UserAppSecretRow},
|
||||
dtos::{Upsert, UserAppSecretRow},
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
@@ -73,6 +73,10 @@ where
|
||||
expires_at = EXCLUDED.expires_at
|
||||
RETURNING WITH (OLD AS old_row, NEW AS new_row)
|
||||
old_row.id AS "old_id?",
|
||||
COALESCE(old_row.app_id, new_row.app_id) AS "old_app_id!",
|
||||
COALESCE(old_row.secret_hash, new_row.secret_hash) AS "old_secret_hash!",
|
||||
COALESCE(old_row.expires_at, new_row.expires_at) AS "old_expires_at!",
|
||||
COALESCE(old_row.created_at, new_row.created_at) AS "old_created_at!",
|
||||
new_row.id AS "id!",
|
||||
new_row.app_id AS "app_id!",
|
||||
new_row.secret_hash AS "secret_hash!",
|
||||
@@ -88,18 +92,26 @@ where
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
let secret = UserAppSecretRow {
|
||||
id: row.id,
|
||||
app_id: row.app_id,
|
||||
secret_hash: row.secret_hash,
|
||||
expires_at: row.expires_at,
|
||||
created_at: row.created_at,
|
||||
};
|
||||
// `old_id` indicates whether there was a previous row before the upsert, other old fields are
|
||||
// coalesced with the new row's values in case there was no previous row. This enables static
|
||||
// typing on the previous row instead of having to check nullability for each old field in code.
|
||||
let previous = row.old_id.map(|id| UserAppSecretRow {
|
||||
id,
|
||||
app_id: row.old_app_id,
|
||||
secret_hash: row.old_secret_hash,
|
||||
expires_at: row.old_expires_at,
|
||||
created_at: row.old_created_at,
|
||||
});
|
||||
|
||||
Ok(if row.old_id.is_some() {
|
||||
Upsert::Updated(secret)
|
||||
} else {
|
||||
Upsert::Created(secret)
|
||||
Ok(Upsert {
|
||||
previous,
|
||||
current: UserAppSecretRow {
|
||||
id: row.id,
|
||||
app_id: row.app_id,
|
||||
secret_hash: row.secret_hash,
|
||||
expires_at: row.expires_at,
|
||||
created_at: row.created_at,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -131,22 +143,21 @@ pub async fn delete_by_id<'e, E>(
|
||||
executor: E,
|
||||
app_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<Delete, DatabaseError>
|
||||
) -> Result<Option<UserAppSecretRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let deleted = sqlx::query!(
|
||||
"DELETE FROM user_app_secrets WHERE app_id = $1 AND id = $2 RETURNING id",
|
||||
sqlx::query_as!(
|
||||
UserAppSecretRow,
|
||||
r#"
|
||||
DELETE FROM user_app_secrets
|
||||
WHERE app_id = $1 AND id = $2
|
||||
RETURNING id, app_id, secret_hash, expires_at, created_at
|
||||
"#,
|
||||
app_id,
|
||||
id,
|
||||
)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
Ok(if deleted.is_some() {
|
||||
Delete::Deleted
|
||||
} else {
|
||||
Delete::NotFound
|
||||
})
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DatabaseError,
|
||||
dtos::{Upserted, UserAppRow},
|
||||
dtos::{Upsert, UserAppRow},
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
@@ -67,7 +67,7 @@ where
|
||||
pub async fn upsert<'e, E>(
|
||||
executor: E,
|
||||
app: UserAppRow,
|
||||
) -> Result<Upserted<UserAppRow>, DatabaseError>
|
||||
) -> Result<Upsert<UserAppRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
@@ -92,14 +92,13 @@ where
|
||||
|
||||
// `old_id` indicates whether there was a previous row before the upsert, other old fields are
|
||||
// coalesced with the new row's values in case there was no previous row. This enables static
|
||||
// typing for the previous row instead of having to check nullability for each old field
|
||||
// individually.
|
||||
// typing on the previous row instead of having to check nullability for each old field in code.
|
||||
let previous = row.old_id.map(|id| UserAppRow {
|
||||
id,
|
||||
user_id: row.old_user_id,
|
||||
});
|
||||
|
||||
Ok(Upserted {
|
||||
Ok(Upsert {
|
||||
previous,
|
||||
current: UserAppRow {
|
||||
id: row.id,
|
||||
@@ -112,16 +111,12 @@ pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<UserApp
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let row = sqlx::query!(
|
||||
sqlx::query_as!(
|
||||
UserAppRow,
|
||||
"DELETE FROM user_apps WHERE id = $1 RETURNING id, user_id",
|
||||
id,
|
||||
)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
Ok(row.map(|row| UserAppRow {
|
||||
id: row.id,
|
||||
user_id: row.user_id,
|
||||
}))
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DatabaseError,
|
||||
dtos::{Delete, Upsert, UserRow},
|
||||
dtos::{Upsert, UserRow},
|
||||
};
|
||||
|
||||
pub struct Query {
|
||||
@@ -69,27 +69,18 @@ where
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
let user = UserRow { id: row.id };
|
||||
|
||||
if row.old_id.is_some() {
|
||||
Ok(Upsert::Updated(user))
|
||||
} else {
|
||||
Ok(Upsert::Created(user))
|
||||
}
|
||||
Ok(Upsert {
|
||||
previous: row.old_id.map(|id| UserRow { id }),
|
||||
current: UserRow { id: row.id },
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, DatabaseError>
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<UserRow>, DatabaseError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let deleted = sqlx::query!("DELETE FROM users WHERE id = $1 RETURNING id", id)
|
||||
sqlx::query_as!(UserRow, "DELETE FROM users WHERE id = $1 RETURNING id", id)
|
||||
.fetch_optional(executor)
|
||||
.await
|
||||
.map_err(DatabaseError::from)?;
|
||||
|
||||
Ok(if deleted.is_some() {
|
||||
Delete::Deleted
|
||||
} else {
|
||||
Delete::NotFound
|
||||
})
|
||||
.map_err(DatabaseError::from)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DecodeError, QueryConversionError, ReferenceRelation, ReferenceViolationKind, RepositoryError,
|
||||
dtos::{Delete, Upsert},
|
||||
dtos::Upsert,
|
||||
};
|
||||
use crate::{
|
||||
database::{Constraint, DatabaseError, accounts as db, dtos::AccountRow},
|
||||
@@ -91,16 +91,17 @@ where
|
||||
Ok(account)
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, RepositoryError>
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<Account>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let result = db::delete_by_id(executor, id)
|
||||
let account = db::delete_by_id(executor, id)
|
||||
.await
|
||||
.map_err(classify_delete_error)?
|
||||
.into();
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Ok(result)
|
||||
Ok(account)
|
||||
}
|
||||
|
||||
fn classify_delete_error(error: DatabaseError) -> RepositoryError {
|
||||
|
||||
@@ -1,43 +1,15 @@
|
||||
use crate::database;
|
||||
|
||||
pub enum Upsert<T> {
|
||||
Created(T),
|
||||
Updated(T),
|
||||
}
|
||||
|
||||
impl<T> From<database::dtos::Upsert<T>> for Upsert<T> {
|
||||
fn from(value: database::dtos::Upsert<T>) -> Self {
|
||||
match value {
|
||||
database::dtos::Upsert::Created(v) => Upsert::Created(v),
|
||||
database::dtos::Upsert::Updated(v) => Upsert::Updated(v),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Upserted<T> {
|
||||
pub struct Upsert<T> {
|
||||
pub current: T,
|
||||
pub previous: Option<T>,
|
||||
}
|
||||
|
||||
impl<T> From<database::dtos::Upserted<T>> for Upserted<T> {
|
||||
fn from(value: database::dtos::Upserted<T>) -> Self {
|
||||
impl<T> From<database::dtos::Upsert<T>> for Upsert<T> {
|
||||
fn from(value: database::dtos::Upsert<T>) -> Self {
|
||||
Self {
|
||||
current: value.current,
|
||||
previous: value.previous,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Delete {
|
||||
Deleted,
|
||||
NotFound,
|
||||
}
|
||||
|
||||
impl From<database::dtos::Delete> for Delete {
|
||||
fn from(value: database::dtos::Delete) -> Self {
|
||||
match value {
|
||||
database::dtos::Delete::Deleted => Delete::Deleted,
|
||||
database::dtos::Delete::NotFound => Delete::NotFound,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DecodeError, QueryConversionError, ReferenceRelation, ReferenceViolationKind, RepositoryError,
|
||||
dtos::{Delete, Upsert},
|
||||
dtos::Upsert,
|
||||
encoding::{
|
||||
decode_direction, decode_recurrence_unit, decode_weekend_adjustment, encode_direction,
|
||||
encode_recurrence_unit, encode_weekend_adjustment,
|
||||
@@ -94,13 +94,19 @@ fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, RepositoryError>
|
||||
pub async fn delete_by_id<'e, E>(
|
||||
executor: E,
|
||||
id: Uuid,
|
||||
) -> Result<Option<RecurringTransaction>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let result = db::delete_by_id(executor, id).await?.into();
|
||||
let transaction = db::delete_by_id(executor, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Ok(result)
|
||||
Ok(transaction)
|
||||
}
|
||||
|
||||
fn decode(row: RecurringTransactionRow) -> Result<RecurringTransaction, DecodeError> {
|
||||
|
||||
@@ -2,7 +2,7 @@ use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DecodeError, QueryConversionError, ReferenceRelation, ReferenceViolationKind, RepositoryError,
|
||||
dtos::{Delete, Upsert},
|
||||
dtos::Upsert,
|
||||
encoding::{decode_direction, encode_direction},
|
||||
};
|
||||
use crate::{
|
||||
@@ -78,13 +78,19 @@ fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, RepositoryError>
|
||||
pub async fn delete_by_id<'e, E>(
|
||||
executor: E,
|
||||
id: Uuid,
|
||||
) -> Result<Option<Transaction>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let result = db::delete_by_id(executor, id).await?.into();
|
||||
let transaction = db::delete_by_id(executor, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Ok(result)
|
||||
Ok(transaction)
|
||||
}
|
||||
|
||||
fn decode(row: TransactionRow) -> Result<Transaction, DecodeError> {
|
||||
|
||||
@@ -103,13 +103,16 @@ pub async fn delete_by_id<'e, E>(
|
||||
executor: E,
|
||||
app_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<super::dtos::Delete, RepositoryError>
|
||||
) -> Result<Option<UserAppSecret>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let result = db::delete_by_id(executor, app_id, id).await?.into();
|
||||
let secret = db::delete_by_id(executor, app_id, id)
|
||||
.await?
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Ok(result)
|
||||
Ok(secret)
|
||||
}
|
||||
|
||||
fn decode(row: UserAppSecretRow) -> Result<UserAppSecret, DecodeError> {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::{QueryConversionError, RepositoryError, dtos::Upserted};
|
||||
use super::{QueryConversionError, RepositoryError, dtos::Upsert};
|
||||
use crate::{
|
||||
database::{Constraint, DatabaseError, dtos::UserAppRow, user_apps as db},
|
||||
domain::UserApp,
|
||||
@@ -59,7 +59,7 @@ where
|
||||
Ok(owner)
|
||||
}
|
||||
|
||||
pub async fn upsert<'e, E>(executor: E, app: UserApp) -> Result<Upserted<UserApp>, RepositoryError>
|
||||
pub async fn upsert<'e, E>(executor: E, app: UserApp) -> Result<Upsert<UserApp>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DecodeError, QueryConversionError, RepositoryError,
|
||||
dtos::{Delete, Upsert},
|
||||
};
|
||||
use super::{DecodeError, QueryConversionError, RepositoryError, dtos::Upsert};
|
||||
use crate::{
|
||||
database::{DatabaseError, dtos::UserRow, users as db},
|
||||
domain::User,
|
||||
@@ -59,16 +56,17 @@ where
|
||||
Ok(user)
|
||||
}
|
||||
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Delete, RepositoryError>
|
||||
pub async fn delete_by_id<'e, E>(executor: E, id: Uuid) -> Result<Option<User>, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let result = db::delete_by_id(executor, id)
|
||||
let user = db::delete_by_id(executor, id)
|
||||
.await
|
||||
.map_err(classify_delete_error)?
|
||||
.into();
|
||||
.map(decode)
|
||||
.transpose()?;
|
||||
|
||||
Ok(result)
|
||||
Ok(user)
|
||||
}
|
||||
|
||||
fn classify_delete_error(error: DatabaseError) -> RepositoryError {
|
||||
|
||||
@@ -9,10 +9,7 @@ use super::{
|
||||
};
|
||||
use crate::{
|
||||
error::AppError,
|
||||
repository::{
|
||||
self,
|
||||
dtos::{Delete, Upsert},
|
||||
},
|
||||
repository::{self},
|
||||
};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
@@ -76,12 +73,14 @@ async fn upsert_account(
|
||||
) -> Result<(StatusCode, Json<AccountResponse>), AppError> {
|
||||
let account = account.into_domain(id)?;
|
||||
|
||||
let (status, account) = match repository::accounts::upsert(&pool, account).await? {
|
||||
Upsert::Created(account) => (StatusCode::CREATED, account),
|
||||
Upsert::Updated(account) => (StatusCode::OK, account),
|
||||
let upserted = repository::accounts::upsert(&pool, account).await?;
|
||||
let status = if upserted.previous.is_some() {
|
||||
StatusCode::OK
|
||||
} else {
|
||||
StatusCode::CREATED
|
||||
};
|
||||
|
||||
Ok((status, Json(account.into())))
|
||||
Ok((status, Json(upserted.current.into())))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
@@ -90,8 +89,9 @@ async fn delete_account(
|
||||
Path(id): Path<Uuid>,
|
||||
State(pool): State<PgPool>,
|
||||
) -> Result<StatusCode, AppError> {
|
||||
match repository::accounts::delete_by_id(&pool, id).await? {
|
||||
Delete::Deleted => Ok(StatusCode::NO_CONTENT),
|
||||
Delete::NotFound => Err(AppError::NotFound),
|
||||
}
|
||||
repository::accounts::delete_by_id(&pool, id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
@@ -15,10 +15,7 @@ use super::{
|
||||
use crate::{
|
||||
domain,
|
||||
error::AppError,
|
||||
repository::{
|
||||
self, RepositoryError, accounts,
|
||||
dtos::{Delete, Upsert},
|
||||
},
|
||||
repository::{self, RepositoryError, accounts},
|
||||
};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
@@ -91,18 +88,20 @@ async fn upsert_scheduled_transaction(
|
||||
let transaction = transaction.into_domain(id)?;
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
let (status, transaction) =
|
||||
match repository::recurring_transactions::upsert(&mut *tx, transaction).await? {
|
||||
Upsert::Created(transaction) => (StatusCode::CREATED, transaction),
|
||||
Upsert::Updated(transaction) => (StatusCode::OK, transaction),
|
||||
};
|
||||
let upserted = repository::recurring_transactions::upsert(&mut *tx, transaction).await?;
|
||||
let accounts =
|
||||
get_accounts_for_transactions(&mut tx, std::slice::from_ref(&transaction)).await?;
|
||||
get_accounts_for_transactions(&mut tx, std::slice::from_ref(&upserted.current)).await?;
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
|
||||
let status = if upserted.previous.is_some() {
|
||||
StatusCode::OK
|
||||
} else {
|
||||
StatusCode::CREATED
|
||||
};
|
||||
|
||||
Ok((
|
||||
status,
|
||||
Json(create_response(transaction, &accounts, Utc::now())?),
|
||||
Json(create_response(upserted.current, &accounts, Utc::now())?),
|
||||
))
|
||||
}
|
||||
|
||||
@@ -112,10 +111,11 @@ async fn delete_scheduled_transaction(
|
||||
State(pool): State<PgPool>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<StatusCode, AppError> {
|
||||
match repository::recurring_transactions::delete_by_id(&pool, id).await? {
|
||||
Delete::Deleted => Ok(StatusCode::NO_CONTENT),
|
||||
Delete::NotFound => Err(AppError::NotFound),
|
||||
}
|
||||
repository::recurring_transactions::delete_by_id(&pool, id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
async fn get_accounts_for_transactions(
|
||||
|
||||
@@ -12,10 +12,7 @@ use super::{
|
||||
use crate::{
|
||||
domain,
|
||||
error::AppError,
|
||||
repository::{
|
||||
self, RepositoryError,
|
||||
dtos::{Delete, Upsert},
|
||||
},
|
||||
repository::{self, RepositoryError},
|
||||
};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
@@ -86,16 +83,18 @@ async fn upsert_transaction(
|
||||
let transaction = transaction.into_domain(id)?;
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
let (status, transaction) =
|
||||
match repository::transactions::upsert(&mut *tx, transaction).await? {
|
||||
Upsert::Created(transaction) => (StatusCode::CREATED, transaction),
|
||||
Upsert::Updated(transaction) => (StatusCode::OK, transaction),
|
||||
};
|
||||
let upserted = repository::transactions::upsert(&mut *tx, transaction).await?;
|
||||
let accounts =
|
||||
get_accounts_for_transactions(&mut tx, std::slice::from_ref(&transaction)).await?;
|
||||
get_accounts_for_transactions(&mut tx, std::slice::from_ref(&upserted.current)).await?;
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
|
||||
Ok((status, Json(create_response(transaction, &accounts)?)))
|
||||
let status = if upserted.previous.is_some() {
|
||||
StatusCode::OK
|
||||
} else {
|
||||
StatusCode::CREATED
|
||||
};
|
||||
|
||||
Ok((status, Json(create_response(upserted.current, &accounts)?)))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
@@ -104,10 +103,11 @@ async fn delete_transaction(
|
||||
Path(id): Path<Uuid>,
|
||||
State(pool): State<PgPool>,
|
||||
) -> Result<StatusCode, AppError> {
|
||||
match repository::transactions::delete_by_id(&pool, id).await? {
|
||||
Delete::Deleted => Ok(StatusCode::NO_CONTENT),
|
||||
Delete::NotFound => Err(AppError::NotFound),
|
||||
}
|
||||
repository::transactions::delete_by_id(&pool, id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
async fn get_accounts_for_transactions(
|
||||
|
||||
@@ -13,10 +13,7 @@ use crate::{
|
||||
crypto::{generate_secret, hash_secret},
|
||||
domain::UserAppSecret,
|
||||
error::AppError,
|
||||
repository::{
|
||||
self, ReferenceRelation, RepositoryError,
|
||||
dtos::{Delete, Upsert},
|
||||
},
|
||||
repository::{self, ReferenceRelation, RepositoryError},
|
||||
};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
@@ -102,13 +99,15 @@ async fn upsert_user_app_secret(
|
||||
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
ensure_app_ownership(&mut tx, user_id, app_id).await?;
|
||||
let upsert = repository::user_app_secrets::upsert(&mut *tx, secret).await?;
|
||||
let upserted = repository::user_app_secrets::upsert(&mut *tx, secret).await?;
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
|
||||
let (status, secret) = match upsert {
|
||||
Upsert::Created(secret) => (StatusCode::CREATED, secret),
|
||||
Upsert::Updated(secret) => (StatusCode::OK, secret),
|
||||
let status = if upserted.previous.is_some() {
|
||||
StatusCode::OK
|
||||
} else {
|
||||
StatusCode::CREATED
|
||||
};
|
||||
let secret = upserted.current;
|
||||
|
||||
Ok((
|
||||
status,
|
||||
@@ -127,13 +126,12 @@ async fn delete_user_app_secret(
|
||||
) -> Result<StatusCode, AppError> {
|
||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||
ensure_app_ownership(&mut tx, user_id, app_id).await?;
|
||||
let result = repository::user_app_secrets::delete_by_id(&mut *tx, app_id, secret_id).await?;
|
||||
let deleted = repository::user_app_secrets::delete_by_id(&mut *tx, app_id, secret_id).await?;
|
||||
tx.commit().await.map_err(RepositoryError::from)?;
|
||||
|
||||
match result {
|
||||
Delete::Deleted => Ok(StatusCode::NO_CONTENT),
|
||||
Delete::NotFound => Err(AppError::NotFound),
|
||||
}
|
||||
deleted.ok_or(AppError::NotFound)?;
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
async fn ensure_app_ownership(
|
||||
|
||||
+12
-12
@@ -9,10 +9,7 @@ use super::{
|
||||
};
|
||||
use crate::{
|
||||
error::AppError,
|
||||
repository::{
|
||||
self,
|
||||
dtos::{Delete, Upsert},
|
||||
},
|
||||
repository::{self},
|
||||
};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
@@ -74,12 +71,14 @@ async fn upsert_user(
|
||||
) -> Result<(StatusCode, Json<UserResponse>), AppError> {
|
||||
let user = user.into_domain(id)?;
|
||||
|
||||
let (status, user) = match repository::users::upsert(&pool, user).await? {
|
||||
Upsert::Created(user) => (StatusCode::CREATED, user),
|
||||
Upsert::Updated(user) => (StatusCode::OK, user),
|
||||
let upserted = repository::users::upsert(&pool, user).await?;
|
||||
let status = if upserted.previous.is_some() {
|
||||
StatusCode::OK
|
||||
} else {
|
||||
StatusCode::CREATED
|
||||
};
|
||||
|
||||
Ok((status, Json(user.into())))
|
||||
Ok((status, Json(upserted.current.into())))
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
@@ -88,8 +87,9 @@ async fn delete_user(
|
||||
Path(id): Path<Uuid>,
|
||||
State(pool): State<PgPool>,
|
||||
) -> Result<StatusCode, AppError> {
|
||||
match repository::users::delete_by_id(&pool, id).await? {
|
||||
Delete::Deleted => Ok(StatusCode::NO_CONTENT),
|
||||
Delete::NotFound => Err(AppError::NotFound),
|
||||
}
|
||||
repository::users::delete_by_id(&pool, id)
|
||||
.await?
|
||||
.ok_or(AppError::NotFound)?;
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user