From f25bbb347c2d4962e49f21008fbff7c03c301e57 Mon Sep 17 00:00:00 2001 From: itsscb Date: Tue, 10 Oct 2023 00:17:52 +0200 Subject: [PATCH] rf/improves *_id uint64 handling --- bff/db/mock/store.go | 6 +- bff/db/query/document.sql | 11 +-- bff/db/query/payment.sql | 5 +- bff/db/query/person.sql | 1 - bff/db/query/return.sql | 2 - bff/db/query/returnsLog.sql | 2 - bff/db/sqlc/document.sql.go | 33 +++++---- bff/db/sqlc/models.go | 12 ++-- bff/db/sqlc/payment.sql.go | 35 +++++---- bff/db/sqlc/payment_test.go | 112 ++++++++++++++--------------- bff/db/sqlc/person.sql.go | 23 +++--- bff/db/sqlc/person_test.go | 2 +- bff/db/sqlc/querier.go | 6 +- bff/db/sqlc/return.sql.go | 26 +++---- bff/db/sqlc/return_test.go | 4 +- bff/db/sqlc/returnsLog.sql.go | 26 +++---- bff/db/sqlc/returnsLog_test.go | 4 +- bff/db/sqlc/tx_create_person.go | 4 +- bff/doc/swagger/df.swagger.json | 4 +- bff/gapi/rpc_create_payment.go | 3 +- bff/gapi/rpc_create_person.go | 3 +- bff/gapi/rpc_delete_payment.go | 7 +- bff/gapi/rpc_delete_person.go | 7 +- bff/gapi/rpc_get_payment.go | 4 +- bff/gapi/rpc_get_person.go | 4 +- bff/gapi/rpc_list_payments.go | 4 +- bff/gapi/rpc_list_persons.go | 4 +- bff/gapi/rpc_update_payment.go | 6 +- bff/pb/rpc_delete_payment.pb.go | 8 +-- bff/pb/rpc_delete_person.pb.go | 8 +-- bff/proto/rpc_delete_payment.proto | 2 +- bff/proto/rpc_delete_person.proto | 2 +- bff/sqlc.yaml | 15 +++- 33 files changed, 181 insertions(+), 214 deletions(-) diff --git a/bff/db/mock/store.go b/bff/db/mock/store.go index 7bd70f9..e102dcc 100644 --- a/bff/db/mock/store.go +++ b/bff/db/mock/store.go @@ -510,7 +510,7 @@ func (mr *MockStoreMockRecorder) GetReturn(arg0, arg1 any) *gomock.Call { } // GetReturns mocks base method. -func (m *MockStore) GetReturns(arg0 context.Context, arg1 int64) ([]db.Return, error) { +func (m *MockStore) GetReturns(arg0 context.Context, arg1 uint64) ([]db.Return, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetReturns", arg0, arg1) ret0, _ := ret[0].([]db.Return) @@ -615,7 +615,7 @@ func (mr *MockStoreMockRecorder) ListMails(arg0, arg1 any) *gomock.Call { } // ListPayments mocks base method. -func (m *MockStore) ListPayments(arg0 context.Context, arg1 int64) ([]db.Payment, error) { +func (m *MockStore) ListPayments(arg0 context.Context, arg1 uint64) ([]db.Payment, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ListPayments", arg0, arg1) ret0, _ := ret[0].([]db.Payment) @@ -630,7 +630,7 @@ func (mr *MockStoreMockRecorder) ListPayments(arg0, arg1 any) *gomock.Call { } // ListPersons mocks base method. -func (m *MockStore) ListPersons(arg0 context.Context, arg1 int64) ([]db.Person, error) { +func (m *MockStore) ListPersons(arg0 context.Context, arg1 uint64) ([]db.Person, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ListPersons", arg0, arg1) ret0, _ := ret[0].([]db.Person) diff --git a/bff/db/query/document.sql b/bff/db/query/document.sql index ff2afa3..eadc243 100644 --- a/bff/db/query/document.sql +++ b/bff/db/query/document.sql @@ -10,9 +10,10 @@ INSERT INTO documents ( "path", "url", "creator", - "changer" + "changer", + "mail_id" ) VALUES ( - $1, $2, $3, $4, $5, $6, $7 + $1, $2, $3, $4, $5, $6, $7, NULL ) RETURNING *; -- name: CreateDocumentMail :one @@ -23,9 +24,10 @@ INSERT INTO documents ( "path", "url", "creator", - "changer" + "changer", + "person_id" ) VALUES ( - $1, $2, $3, $4, $5, $6, $7 + $1, $2, $3, $4, $5, $6, $7, NULL ) RETURNING *; -- name: ListDocuments :many @@ -37,7 +39,6 @@ OFFSET $2; -- name: UpdateDocument :one UPDATE documents SET - "person_id" = COALESCE(sqlc.narg(person_id), "person_id"), "name" = COALESCE(sqlc.narg(name), "name"), "type" = COALESCE(sqlc.narg(type), "type"), "path" = COALESCE(sqlc.narg(path), "path"), diff --git a/bff/db/query/payment.sql b/bff/db/query/payment.sql index a3f726c..1fbe330 100644 --- a/bff/db/query/payment.sql +++ b/bff/db/query/payment.sql @@ -27,7 +27,6 @@ ORDER BY "payment_category"; -- name: UpdatePayment :one UPDATE payments SET - "account_id" = COALESCE(sqlc.narg(account_id), "account_id"), "payment_category" = COALESCE(sqlc.narg(payment_category), "payment_category"), "bankname" = COALESCE(sqlc.narg(bankname), "bankname"), "IBAN" = COALESCE(sqlc.narg(IBAN), "IBAN"), @@ -36,9 +35,9 @@ SET "paypal_id" = COALESCE(sqlc.narg(paypal_id), "paypal_id"), "payment_system" = COALESCE(sqlc.narg(payment_system), "payment_system"), "type" = COALESCE(sqlc.narg(type), "type"), - "changer" = $2, + "changer" = sqlc.arg(changer), "changed" = now() -WHERE "id" = $1 +WHERE "id" = sqlc.arg(id) RETURNING *; -- name: DeletePayment :exec diff --git a/bff/db/query/person.sql b/bff/db/query/person.sql index 4c1fe63..d64a659 100644 --- a/bff/db/query/person.sql +++ b/bff/db/query/person.sql @@ -26,7 +26,6 @@ ORDER BY "lastname", "firstname"; -- name: UpdatePerson :one UPDATE persons SET - "account_id" = COALESCE(sqlc.narg(account_id), "account_id"), "firstname" = COALESCE(sqlc.narg(firstname), "firstname"), "lastname" = COALESCE(sqlc.narg(lastname), "lastname"), "birthday" = COALESCE(sqlc.narg(birthday), "birthday"), diff --git a/bff/db/query/return.sql b/bff/db/query/return.sql index 9abcc77..eb551cb 100644 --- a/bff/db/query/return.sql +++ b/bff/db/query/return.sql @@ -34,8 +34,6 @@ OFFSET $2; -- name: UpdateReturn :one UPDATE returns SET - "person_id" = COALESCE(sqlc.narg(person_id), "person_id"), - "provider_id" = COALESCE(sqlc.narg(provider_id), "provider_id"), "name" = COALESCE(sqlc.narg(name), "name"), "description" = COALESCE(sqlc.narg(description), "description"), "category" = COALESCE(sqlc.narg(category), "category"), diff --git a/bff/db/query/returnsLog.sql b/bff/db/query/returnsLog.sql index 3b33e2a..f6bcbe4 100644 --- a/bff/db/query/returnsLog.sql +++ b/bff/db/query/returnsLog.sql @@ -26,8 +26,6 @@ OFFSET $2; -- name: UpdateReturnsLog :one UPDATE "returnsLog" SET - "return_id" = COALESCE(sqlc.narg(return_id), "return_id"), - "mail_id" = COALESCE(sqlc.narg(mail_id), "mail_id"), "status" = COALESCE(sqlc.narg(status), "status"), "changer" = $1, "changed" = now() diff --git a/bff/db/sqlc/document.sql.go b/bff/db/sqlc/document.sql.go index e6b2825..7fbccaa 100644 --- a/bff/db/sqlc/document.sql.go +++ b/bff/db/sqlc/document.sql.go @@ -18,9 +18,10 @@ INSERT INTO documents ( "path", "url", "creator", - "changer" + "changer", + "person_id" ) VALUES ( - $1, $2, $3, $4, $5, $6, $7 + $1, $2, $3, $4, $5, $6, $7, NULL ) RETURNING id, person_id, name, type, path, url, valid, valid_date, validated_by, mail_id, creator, created, changer, changed ` @@ -72,9 +73,10 @@ INSERT INTO documents ( "path", "url", "creator", - "changer" + "changer", + "mail_id" ) VALUES ( - $1, $2, $3, $4, $5, $6, $7 + $1, $2, $3, $4, $5, $6, $7, NULL ) RETURNING id, person_id, name, type, path, url, valid, valid_date, validated_by, mail_id, creator, created, changer, changed ` @@ -247,11 +249,10 @@ func (q *Queries) ListDocuments(ctx context.Context, arg ListDocumentsParams) ([ const updateDocument = `-- name: UpdateDocument :one UPDATE documents SET - "person_id" = COALESCE($3, "person_id"), - "name" = COALESCE($4, "name"), - "type" = COALESCE($5, "type"), - "path" = COALESCE($6, "path"), - "url" = COALESCE($7, "url"), + "name" = COALESCE($3, "name"), + "type" = COALESCE($4, "type"), + "path" = COALESCE($5, "path"), + "url" = COALESCE($6, "url"), changer = $2, changed = now() WHERE "id" = $1 @@ -259,20 +260,18 @@ RETURNING id, person_id, name, type, path, url, valid, valid_date, validated_by, ` type UpdateDocumentParams struct { - ID uint64 `json:"id"` - Changer string `json:"changer"` - PersonID sql.NullInt64 `json:"person_id"` - Name sql.NullString `json:"name"` - Type sql.NullString `json:"type"` - Path sql.NullString `json:"path"` - Url sql.NullString `json:"url"` + ID uint64 `json:"id"` + Changer string `json:"changer"` + Name sql.NullString `json:"name"` + Type sql.NullString `json:"type"` + Path sql.NullString `json:"path"` + Url sql.NullString `json:"url"` } func (q *Queries) UpdateDocument(ctx context.Context, arg UpdateDocumentParams) (Document, error) { row := q.db.QueryRowContext(ctx, updateDocument, arg.ID, arg.Changer, - arg.PersonID, arg.Name, arg.Type, arg.Path, diff --git a/bff/db/sqlc/models.go b/bff/db/sqlc/models.go index b27c817..57f9915 100644 --- a/bff/db/sqlc/models.go +++ b/bff/db/sqlc/models.go @@ -65,7 +65,7 @@ type Mail struct { type Payment struct { ID uint64 `json:"id"` - AccountID int64 `json:"account_id"` + AccountID uint64 `json:"account_id"` PaymentCategory string `json:"payment_category"` Bankname sql.NullString `json:"bankname"` IBAN sql.NullString `json:"IBAN"` @@ -82,7 +82,7 @@ type Payment struct { type Person struct { ID uint64 `json:"id"` - AccountID int64 `json:"account_id"` + AccountID uint64 `json:"account_id"` Firstname string `json:"firstname"` Lastname string `json:"lastname"` Birthday time.Time `json:"birthday"` @@ -110,8 +110,8 @@ type Provider struct { type Return struct { ID uint64 `json:"id"` - PersonID int64 `json:"person_id"` - ProviderID int64 `json:"provider_id"` + PersonID uint64 `json:"person_id"` + ProviderID uint64 `json:"provider_id"` Name string `json:"name"` Description string `json:"description"` Category string `json:"category"` @@ -125,8 +125,8 @@ type Return struct { type ReturnsLog struct { ID uint64 `json:"id"` - ReturnID int64 `json:"return_id"` - MailID int64 `json:"mail_id"` + ReturnID uint64 `json:"return_id"` + MailID uint64 `json:"mail_id"` Status sql.NullString `json:"status"` Creator string `json:"creator"` Created time.Time `json:"created"` diff --git a/bff/db/sqlc/payment.sql.go b/bff/db/sqlc/payment.sql.go index c1eb584..4b1ee17 100644 --- a/bff/db/sqlc/payment.sql.go +++ b/bff/db/sqlc/payment.sql.go @@ -29,7 +29,7 @@ INSERT INTO payments ( ` type CreatePaymentParams struct { - AccountID int64 `json:"account_id"` + AccountID uint64 `json:"account_id"` PaymentCategory string `json:"payment_category"` Bankname sql.NullString `json:"bankname"` IBAN sql.NullString `json:"IBAN"` @@ -119,7 +119,7 @@ WHERE "account_id" = $1 ORDER BY "payment_category" ` -func (q *Queries) ListPayments(ctx context.Context, accountID int64) ([]Payment, error) { +func (q *Queries) ListPayments(ctx context.Context, accountID uint64) ([]Payment, error) { rows, err := q.db.QueryContext(ctx, listPayments, accountID) if err != nil { return nil, err @@ -160,25 +160,21 @@ func (q *Queries) ListPayments(ctx context.Context, accountID int64) ([]Payment, const updatePayment = `-- name: UpdatePayment :one UPDATE payments SET - "account_id" = COALESCE($3, "account_id"), - "payment_category" = COALESCE($4, "payment_category"), - "bankname" = COALESCE($5, "bankname"), - "IBAN" = COALESCE($6, "IBAN"), - "BIC" = COALESCE($7, "BIC"), - "paypal_account" = COALESCE($8, "paypal_account"), - "paypal_id" = COALESCE($9, "paypal_id"), - "payment_system" = COALESCE($10, "payment_system"), - "type" = COALESCE($11, "type"), - "changer" = $2, + "payment_category" = COALESCE($1, "payment_category"), + "bankname" = COALESCE($2, "bankname"), + "IBAN" = COALESCE($3, "IBAN"), + "BIC" = COALESCE($4, "BIC"), + "paypal_account" = COALESCE($5, "paypal_account"), + "paypal_id" = COALESCE($6, "paypal_id"), + "payment_system" = COALESCE($7, "payment_system"), + "type" = COALESCE($8, "type"), + "changer" = $9, "changed" = now() -WHERE "id" = $1 +WHERE "id" = $10 RETURNING id, account_id, payment_category, bankname, "IBAN", "BIC", paypal_account, paypal_id, payment_system, type, creator, created, changer, changed ` type UpdatePaymentParams struct { - ID uint64 `json:"id"` - Changer string `json:"changer"` - AccountID sql.NullInt64 `json:"account_id"` PaymentCategory sql.NullString `json:"payment_category"` Bankname sql.NullString `json:"bankname"` Iban sql.NullString `json:"iban"` @@ -187,13 +183,12 @@ type UpdatePaymentParams struct { PaypalID sql.NullString `json:"paypal_id"` PaymentSystem sql.NullString `json:"payment_system"` Type sql.NullString `json:"type"` + Changer string `json:"changer"` + ID uint64 `json:"id"` } func (q *Queries) UpdatePayment(ctx context.Context, arg UpdatePaymentParams) (Payment, error) { row := q.db.QueryRowContext(ctx, updatePayment, - arg.ID, - arg.Changer, - arg.AccountID, arg.PaymentCategory, arg.Bankname, arg.Iban, @@ -202,6 +197,8 @@ func (q *Queries) UpdatePayment(ctx context.Context, arg UpdatePaymentParams) (P arg.PaypalID, arg.PaymentSystem, arg.Type, + arg.Changer, + arg.ID, ) var i Payment err := row.Scan( diff --git a/bff/db/sqlc/payment_test.go b/bff/db/sqlc/payment_test.go index 48db528..844f77a 100644 --- a/bff/db/sqlc/payment_test.go +++ b/bff/db/sqlc/payment_test.go @@ -10,6 +10,27 @@ import ( "github.com/stretchr/testify/require" ) +func TestUpdatePayment(t *testing.T) { + payment1 := createRandomPayment(t) + require.NotEmpty(t, payment1) + + arg := UpdatePaymentParams{ + ID: payment1.ID, + Bankname: sql.NullString{ + String: util.RandomName(), + Valid: true, + }, + } + + payment2, err := testQueries.UpdatePayment(context.Background(), arg) + require.NoError(t, err) + require.NotEmpty(t, payment2) + + require.Equal(t, payment1.ID, payment2.ID) + require.Equal(t, payment1.PaymentCategory, payment2.PaymentCategory) + require.NotEqual(t, payment1.Bankname, payment2.Bankname) +} + func createRandomPayment(t *testing.T) Payment { account := createRandomAccount(t) require.NotEmpty(t, account) @@ -17,7 +38,7 @@ func createRandomPayment(t *testing.T) Payment { creator := util.RandomName() arg := CreatePaymentParams{ - AccountID: int64(account.ID), + AccountID: account.ID, PaymentCategory: util.RandomName(), Bankname: sql.NullString{ Valid: true, @@ -48,25 +69,25 @@ func createRandomPayment(t *testing.T) Payment { Changer: creator, } - person, err := testQueries.CreatePayment(context.Background(), arg) + payment, err := testQueries.CreatePayment(context.Background(), arg) require.NoError(t, err) - require.NotEmpty(t, person) + require.NotEmpty(t, payment) - require.Equal(t, arg.PaymentCategory, person.PaymentCategory) - require.Equal(t, arg.Bankname, person.Bankname) - require.Equal(t, arg.AccountID, person.AccountID) - require.Equal(t, arg.IBAN, person.IBAN) - require.Equal(t, arg.BIC, person.BIC) - require.Equal(t, arg.PaypalAccount, person.PaypalAccount) - require.Equal(t, arg.PaymentSystem, person.PaymentSystem) - require.Equal(t, arg.PaypalID, person.PaypalID) - require.Equal(t, arg.Creator, person.Creator) - require.Equal(t, arg.Type, person.Type) + require.Equal(t, arg.PaymentCategory, payment.PaymentCategory) + require.Equal(t, arg.Bankname, payment.Bankname) + require.Equal(t, arg.AccountID, payment.AccountID) + require.Equal(t, arg.IBAN, payment.IBAN) + require.Equal(t, arg.BIC, payment.BIC) + require.Equal(t, arg.PaypalAccount, payment.PaypalAccount) + require.Equal(t, arg.PaymentSystem, payment.PaymentSystem) + require.Equal(t, arg.PaypalID, payment.PaypalID) + require.Equal(t, arg.Creator, payment.Creator) + require.Equal(t, arg.Type, payment.Type) - require.NotZero(t, person.ID) - require.NotZero(t, person.Created) + require.NotZero(t, payment.ID) + require.NotZero(t, payment.Created) - return person + return payment } func TestCreatePayment(t *testing.T) { @@ -74,55 +95,34 @@ func TestCreatePayment(t *testing.T) { } func TestGetPayment(t *testing.T) { - newperson := createRandomPayment(t) - require.NotEmpty(t, newperson) + newpayment := createRandomPayment(t) + require.NotEmpty(t, newpayment) - person, err := testQueries.GetPayment(context.Background(), newperson.ID) + payment, err := testQueries.GetPayment(context.Background(), newpayment.ID) require.NoError(t, err) - require.NotEmpty(t, person) + require.NotEmpty(t, payment) - require.Equal(t, newperson.PaymentCategory, person.PaymentCategory) - require.Equal(t, newperson.Bankname, person.Bankname) - require.Equal(t, newperson.AccountID, person.AccountID) - require.Equal(t, newperson.IBAN, person.IBAN) - require.Equal(t, newperson.BIC, person.BIC) - require.Equal(t, newperson.PaypalAccount, person.PaypalAccount) - require.Equal(t, newperson.PaymentSystem, person.PaymentSystem) - require.Equal(t, newperson.PaypalID, person.PaypalID) - require.Equal(t, newperson.Creator, person.Creator) - require.Equal(t, newperson.Type, person.Type) + require.Equal(t, newpayment.PaymentCategory, payment.PaymentCategory) + require.Equal(t, newpayment.Bankname, payment.Bankname) + require.Equal(t, newpayment.AccountID, payment.AccountID) + require.Equal(t, newpayment.IBAN, payment.IBAN) + require.Equal(t, newpayment.BIC, payment.BIC) + require.Equal(t, newpayment.PaypalAccount, payment.PaypalAccount) + require.Equal(t, newpayment.PaymentSystem, payment.PaymentSystem) + require.Equal(t, newpayment.PaypalID, payment.PaypalID) + require.Equal(t, newpayment.Creator, payment.Creator) + require.Equal(t, newpayment.Type, payment.Type) - require.WithinDuration(t, newperson.Created, person.Created, time.Second) + require.WithinDuration(t, newpayment.Created, payment.Created, time.Second) } func TestDeletePayment(t *testing.T) { - person1 := createRandomPayment(t) - err := testQueries.DeletePayment(context.Background(), person1.ID) + payment1 := createRandomPayment(t) + err := testQueries.DeletePayment(context.Background(), payment1.ID) require.NoError(t, err) - person2, err := testQueries.GetPayment(context.Background(), person1.ID) + payment2, err := testQueries.GetPayment(context.Background(), payment1.ID) require.Error(t, err) require.EqualError(t, err, sql.ErrNoRows.Error()) - require.Empty(t, person2) -} - -func TestUpdatePayment(t *testing.T) { - person1 := createRandomPayment(t) - require.NotEmpty(t, person1) - - arg := UpdatePaymentParams{ - ID: person1.ID, - Bankname: sql.NullString{ - String: util.RandomName(), - Valid: true, - }, - } - - person2, err := testQueries.UpdatePayment(context.Background(), arg) - require.NoError(t, err) - require.NotEmpty(t, person2) - - require.Equal(t, person1.ID, person2.ID) - require.Equal(t, person1.PaymentCategory, person2.PaymentCategory) - require.NotEqual(t, person1.Bankname, person2.Bankname) + require.Empty(t, payment2) } diff --git a/bff/db/sqlc/person.sql.go b/bff/db/sqlc/person.sql.go index ddfd32e..8eb9693 100644 --- a/bff/db/sqlc/person.sql.go +++ b/bff/db/sqlc/person.sql.go @@ -29,7 +29,7 @@ INSERT INTO persons ( ` type CreatePersonParams struct { - AccountID int64 `json:"account_id"` + AccountID uint64 `json:"account_id"` Firstname string `json:"firstname"` Lastname string `json:"lastname"` Birthday time.Time `json:"birthday"` @@ -114,7 +114,7 @@ SELECT id, person_id, provider_id, name, description, category, email, status, c WHERE "person_id" = $1 ` -func (q *Queries) GetReturns(ctx context.Context, id int64) ([]Return, error) { +func (q *Queries) GetReturns(ctx context.Context, id uint64) ([]Return, error) { rows, err := q.db.QueryContext(ctx, getReturns, id) if err != nil { return nil, err @@ -156,7 +156,7 @@ WHERE "account_id" = $1 ORDER BY "lastname", "firstname" ` -func (q *Queries) ListPersons(ctx context.Context, accountID int64) ([]Person, error) { +func (q *Queries) ListPersons(ctx context.Context, accountID uint64) ([]Person, error) { rows, err := q.db.QueryContext(ctx, listPersons, accountID) if err != nil { return nil, err @@ -196,14 +196,13 @@ func (q *Queries) ListPersons(ctx context.Context, accountID int64) ([]Person, e const updatePerson = `-- name: UpdatePerson :one UPDATE persons SET - "account_id" = COALESCE($3, "account_id"), - "firstname" = COALESCE($4, "firstname"), - "lastname" = COALESCE($5, "lastname"), - "birthday" = COALESCE($6, "birthday"), - "city" = COALESCE($7, "city"), - "zip" = COALESCE($8, "zip"), - "street" = COALESCE($9, "street"), - "country" = COALESCE($10, "country"), + "firstname" = COALESCE($3, "firstname"), + "lastname" = COALESCE($4, "lastname"), + "birthday" = COALESCE($5, "birthday"), + "city" = COALESCE($6, "city"), + "zip" = COALESCE($7, "zip"), + "street" = COALESCE($8, "street"), + "country" = COALESCE($9, "country"), "changer" = $2, "changed" = now() WHERE "id" = $1 @@ -213,7 +212,6 @@ RETURNING id, account_id, firstname, lastname, birthday, city, zip, street, coun type UpdatePersonParams struct { ID uint64 `json:"id"` Changer string `json:"changer"` - AccountID sql.NullInt64 `json:"account_id"` Firstname sql.NullString `json:"firstname"` Lastname sql.NullString `json:"lastname"` Birthday sql.NullTime `json:"birthday"` @@ -227,7 +225,6 @@ func (q *Queries) UpdatePerson(ctx context.Context, arg UpdatePersonParams) (Per row := q.db.QueryRowContext(ctx, updatePerson, arg.ID, arg.Changer, - arg.AccountID, arg.Firstname, arg.Lastname, arg.Birthday, diff --git a/bff/db/sqlc/person_test.go b/bff/db/sqlc/person_test.go index 85780e6..8a1256c 100644 --- a/bff/db/sqlc/person_test.go +++ b/bff/db/sqlc/person_test.go @@ -17,7 +17,7 @@ func createRandomPerson(t *testing.T) Person { creator := util.RandomName() arg := CreatePersonParams{ - AccountID: int64(account.ID), + AccountID: account.ID, Firstname: util.RandomName(), Lastname: util.RandomName(), Birthday: time.Date(1990, 1, 1, 0, 0, 0, 0, time.UTC), diff --git a/bff/db/sqlc/querier.go b/bff/db/sqlc/querier.go index 9f3282d..91dddfc 100644 --- a/bff/db/sqlc/querier.go +++ b/bff/db/sqlc/querier.go @@ -53,15 +53,15 @@ type Querier interface { GetPerson(ctx context.Context, id uint64) (Person, error) GetProvider(ctx context.Context, id uint64) (Provider, error) GetReturn(ctx context.Context, id uint64) (Return, error) - GetReturns(ctx context.Context, id int64) ([]Return, error) + GetReturns(ctx context.Context, id uint64) ([]Return, error) GetReturnsLog(ctx context.Context, id uint64) (ReturnsLog, error) GetSession(ctx context.Context, id uuid.UUID) (Session, error) InvalidateDocument(ctx context.Context, arg InvalidateDocumentParams) (Document, error) ListAccounts(ctx context.Context, arg ListAccountsParams) ([]Account, error) ListDocuments(ctx context.Context, arg ListDocumentsParams) ([]Document, error) ListMails(ctx context.Context, arg ListMailsParams) ([]Mail, error) - ListPayments(ctx context.Context, accountID int64) ([]Payment, error) - ListPersons(ctx context.Context, accountID int64) ([]Person, error) + ListPayments(ctx context.Context, accountID uint64) ([]Payment, error) + ListPersons(ctx context.Context, accountID uint64) ([]Person, error) ListProviders(ctx context.Context, arg ListProvidersParams) ([]Provider, error) ListReturns(ctx context.Context, arg ListReturnsParams) ([]Return, error) ListReturnsLogs(ctx context.Context, arg ListReturnsLogsParams) ([]ReturnsLog, error) diff --git a/bff/db/sqlc/return.sql.go b/bff/db/sqlc/return.sql.go index 915158d..4618e2e 100644 --- a/bff/db/sqlc/return.sql.go +++ b/bff/db/sqlc/return.sql.go @@ -37,7 +37,7 @@ FROM providers type CloneProvidersParams struct { Creator string `json:"creator"` - PersonID int64 `json:"person_id"` + PersonID uint64 `json:"person_id"` } func (q *Queries) CloneProviders(ctx context.Context, arg CloneProvidersParams) error { @@ -70,8 +70,8 @@ INSERT INTO returns ( ` type CreateReturnParams struct { - PersonID int64 `json:"person_id"` - ProviderID int64 `json:"provider_id"` + PersonID uint64 `json:"person_id"` + ProviderID uint64 `json:"provider_id"` Name string `json:"name"` Description string `json:"description"` Category string `json:"category"` @@ -197,22 +197,18 @@ func (q *Queries) ListReturns(ctx context.Context, arg ListReturnsParams) ([]Ret const updateReturn = `-- name: UpdateReturn :one UPDATE returns SET - "person_id" = COALESCE($1, "person_id"), - "provider_id" = COALESCE($2, "provider_id"), - "name" = COALESCE($3, "name"), - "description" = COALESCE($4, "description"), - "category" = COALESCE($5, "category"), - "email" = COALESCE($6, "email"), - "status" = COALESCE($7, "status"), - "changer" = $8, + "name" = COALESCE($1, "name"), + "description" = COALESCE($2, "description"), + "category" = COALESCE($3, "category"), + "email" = COALESCE($4, "email"), + "status" = COALESCE($5, "status"), + "changer" = $6, "changed" = now() -WHERE "id" = $9 +WHERE "id" = $7 RETURNING id, person_id, provider_id, name, description, category, email, status, creator, created, changer, changed ` type UpdateReturnParams struct { - PersonID sql.NullInt64 `json:"person_id"` - ProviderID sql.NullInt64 `json:"provider_id"` Name sql.NullString `json:"name"` Description sql.NullString `json:"description"` Category sql.NullString `json:"category"` @@ -224,8 +220,6 @@ type UpdateReturnParams struct { func (q *Queries) UpdateReturn(ctx context.Context, arg UpdateReturnParams) (Return, error) { row := q.db.QueryRowContext(ctx, updateReturn, - arg.PersonID, - arg.ProviderID, arg.Name, arg.Description, arg.Category, diff --git a/bff/db/sqlc/return_test.go b/bff/db/sqlc/return_test.go index a57bbee..019caf1 100644 --- a/bff/db/sqlc/return_test.go +++ b/bff/db/sqlc/return_test.go @@ -18,8 +18,8 @@ func createRandomReturn(t *testing.T) Return { creator := util.RandomName() arg := CreateReturnParams{ - PersonID: int64(person.ID), - ProviderID: int64(provider.ID), + PersonID: person.ID, + ProviderID: provider.ID, Status: util.RandomString(7), Name: util.RandomName(), Description: util.RandomString(30), diff --git a/bff/db/sqlc/returnsLog.sql.go b/bff/db/sqlc/returnsLog.sql.go index 1fac37e..380feee 100644 --- a/bff/db/sqlc/returnsLog.sql.go +++ b/bff/db/sqlc/returnsLog.sql.go @@ -27,8 +27,8 @@ INSERT INTO "returnsLog" ( ` type CreateReturnsLogParams struct { - ReturnID int64 `json:"return_id"` - MailID int64 `json:"mail_id"` + ReturnID uint64 `json:"return_id"` + MailID uint64 `json:"mail_id"` Status sql.NullString `json:"status"` Creator string `json:"creator"` } @@ -132,31 +132,21 @@ func (q *Queries) ListReturnsLogs(ctx context.Context, arg ListReturnsLogsParams const updateReturnsLog = `-- name: UpdateReturnsLog :one UPDATE "returnsLog" SET - "return_id" = COALESCE($2, "return_id"), - "mail_id" = COALESCE($3, "mail_id"), - "status" = COALESCE($4, "status"), + "status" = COALESCE($2, "status"), "changer" = $1, "changed" = now() -WHERE "id" = $5 +WHERE "id" = $3 RETURNING id, return_id, mail_id, status, creator, created, changer, changed ` type UpdateReturnsLogParams struct { - Changer string `json:"changer"` - ReturnID sql.NullInt64 `json:"return_id"` - MailID sql.NullInt64 `json:"mail_id"` - Status sql.NullString `json:"status"` - ID uint64 `json:"id"` + Changer string `json:"changer"` + Status sql.NullString `json:"status"` + ID uint64 `json:"id"` } func (q *Queries) UpdateReturnsLog(ctx context.Context, arg UpdateReturnsLogParams) (ReturnsLog, error) { - row := q.db.QueryRowContext(ctx, updateReturnsLog, - arg.Changer, - arg.ReturnID, - arg.MailID, - arg.Status, - arg.ID, - ) + row := q.db.QueryRowContext(ctx, updateReturnsLog, arg.Changer, arg.Status, arg.ID) var i ReturnsLog err := row.Scan( &i.ID, diff --git a/bff/db/sqlc/returnsLog_test.go b/bff/db/sqlc/returnsLog_test.go index 51bb91d..30beeee 100644 --- a/bff/db/sqlc/returnsLog_test.go +++ b/bff/db/sqlc/returnsLog_test.go @@ -18,8 +18,8 @@ func createRandomReturnsLog(t *testing.T) ReturnsLog { creator := util.RandomName() arg := CreateReturnsLogParams{ - ReturnID: int64(ret.ID), - MailID: int64(mail.ID), + ReturnID: ret.ID, + MailID: mail.ID, Status: sql.NullString{ Valid: true, String: util.RandomString(7), diff --git a/bff/db/sqlc/tx_create_person.go b/bff/db/sqlc/tx_create_person.go index 68ef658..953edcf 100644 --- a/bff/db/sqlc/tx_create_person.go +++ b/bff/db/sqlc/tx_create_person.go @@ -6,7 +6,7 @@ import ( ) type CreatePersonTxParams struct { - AccountID int64 `json:"account_id"` + AccountID uint64 `json:"account_id"` Firstname string `json:"firstname"` Lastname string `json:"lastname"` Birthday time.Time `json:"birthday"` @@ -35,7 +35,7 @@ func (store *SQLStore) CreatePersonTx(ctx context.Context, arg CreatePersonTxPar err = q.CloneProviders(ctx, CloneProvidersParams{ Creator: arg.Creator, - PersonID: int64(result.Person.ID), + PersonID: result.Person.ID, }) return err }) diff --git a/bff/doc/swagger/df.swagger.json b/bff/doc/swagger/df.swagger.json index 2eac134..a4135a5 100644 --- a/bff/doc/swagger/df.swagger.json +++ b/bff/doc/swagger/df.swagger.json @@ -1015,7 +1015,7 @@ "properties": { "id": { "type": "string", - "format": "int64" + "format": "uint64" }, "deleted": { "type": "boolean" @@ -1032,7 +1032,7 @@ "properties": { "id": { "type": "string", - "format": "int64" + "format": "uint64" }, "deleted": { "type": "boolean" diff --git a/bff/gapi/rpc_create_payment.go b/bff/gapi/rpc_create_payment.go index f1278b0..ea36c1f 100644 --- a/bff/gapi/rpc_create_payment.go +++ b/bff/gapi/rpc_create_payment.go @@ -38,9 +38,8 @@ func (server *Server) CreatePayment(ctx context.Context, req *pb.CreatePaymentRe } } - accountID := int64(req.GetAccountId()) arg := db.CreatePaymentParams{ - AccountID: accountID, + AccountID: req.GetAccountId(), PaymentCategory: req.GetPaymentCategory(), Bankname: sql.NullString{ Valid: req.GetBankname() != "", diff --git a/bff/gapi/rpc_create_person.go b/bff/gapi/rpc_create_person.go index 8985917..09c9d83 100644 --- a/bff/gapi/rpc_create_person.go +++ b/bff/gapi/rpc_create_person.go @@ -38,9 +38,8 @@ func (server *Server) CreatePerson(ctx context.Context, req *pb.CreatePersonRequ } } - accountID := int64(req.GetAccountId()) arg := db.CreatePersonTxParams{ - AccountID: accountID, + AccountID: account.ID, Firstname: req.GetFirstname(), Lastname: req.GetLastname(), Birthday: req.GetBirthday().AsTime(), diff --git a/bff/gapi/rpc_delete_payment.go b/bff/gapi/rpc_delete_payment.go index f635ea8..15b5f43 100644 --- a/bff/gapi/rpc_delete_payment.go +++ b/bff/gapi/rpc_delete_payment.go @@ -44,10 +44,7 @@ func (server *Server) DeletePayment(ctx context.Context, req *pb.DeletePaymentRe return nil, status.Errorf(codes.Internal, "failed to get payment") } - paymentID := int64(req.GetId()) - accountID := int64(account.ID) - - if payment.AccountID != accountID { + if payment.AccountID != account.ID { if !server.isAdmin(ctx, authPayload) { return nil, status.Error(codes.NotFound, "payment not found") } @@ -60,7 +57,7 @@ func (server *Server) DeletePayment(ctx context.Context, req *pb.DeletePaymentRe } rsp := &pb.DeletePaymentResponse{ - Id: paymentID, + Id: payment.ID, Deleted: true, } return rsp, nil diff --git a/bff/gapi/rpc_delete_person.go b/bff/gapi/rpc_delete_person.go index 968f62e..2d43878 100644 --- a/bff/gapi/rpc_delete_person.go +++ b/bff/gapi/rpc_delete_person.go @@ -44,10 +44,7 @@ func (server *Server) DeletePerson(ctx context.Context, req *pb.DeletePersonRequ return nil, status.Errorf(codes.Internal, "failed to get person") } - personID := int64(person.ID) - accountID := int64(account.ID) - - if person.AccountID != accountID { + if person.AccountID != account.ID { if !server.isAdmin(ctx, authPayload) { return nil, status.Error(codes.NotFound, "person not found") } @@ -60,7 +57,7 @@ func (server *Server) DeletePerson(ctx context.Context, req *pb.DeletePersonRequ } rsp := &pb.DeletePersonResponse{ - Id: personID, + Id: person.ID, Deleted: true, } return rsp, nil diff --git a/bff/gapi/rpc_get_payment.go b/bff/gapi/rpc_get_payment.go index 08cf18c..ed72d10 100644 --- a/bff/gapi/rpc_get_payment.go +++ b/bff/gapi/rpc_get_payment.go @@ -44,9 +44,7 @@ func (server *Server) GetPayment(ctx context.Context, req *pb.GetPaymentRequest) return nil, status.Error(codes.NotFound, "failed to get payments") } - accountID := int64(account.ID) - - if accountID != payment.AccountID { + if account.ID != payment.AccountID { if !server.isAdmin(ctx, authPayload) { return nil, status.Error(codes.NotFound, "account not found") } diff --git a/bff/gapi/rpc_get_person.go b/bff/gapi/rpc_get_person.go index 6439a90..7687a39 100644 --- a/bff/gapi/rpc_get_person.go +++ b/bff/gapi/rpc_get_person.go @@ -44,9 +44,7 @@ func (server *Server) GetPerson(ctx context.Context, req *pb.GetPersonRequest) ( return nil, status.Error(codes.NotFound, "failed to get persons") } - accountID := int64(account.ID) - - if accountID != person.AccountID { + if account.ID != person.AccountID { if !server.isAdmin(ctx, authPayload) { return nil, status.Error(codes.NotFound, "account not found") } diff --git a/bff/gapi/rpc_list_payments.go b/bff/gapi/rpc_list_payments.go index 124987f..2f262f4 100644 --- a/bff/gapi/rpc_list_payments.go +++ b/bff/gapi/rpc_list_payments.go @@ -42,9 +42,7 @@ func (server *Server) ListPayments(ctx context.Context, req *pb.ListPaymentsRequ } } - accountID := int64(account.ID) - - dbPayments, err := server.store.ListPayments(ctx, accountID) + dbPayments, err := server.store.ListPayments(ctx, account.ID) if err != nil { if errors.Is(err, sql.ErrNoRows) { return nil, status.Error(codes.NotFound, "no payments found") diff --git a/bff/gapi/rpc_list_persons.go b/bff/gapi/rpc_list_persons.go index 129fc9b..dc28a37 100644 --- a/bff/gapi/rpc_list_persons.go +++ b/bff/gapi/rpc_list_persons.go @@ -42,9 +42,7 @@ func (server *Server) ListPersons(ctx context.Context, req *pb.ListPersonsReques } } - accountID := int64(account.ID) - - dbPersons, err := server.store.ListPersons(ctx, accountID) + dbPersons, err := server.store.ListPersons(ctx, account.ID) if err != nil { if errors.Is(err, sql.ErrNoRows) { return nil, status.Error(codes.NotFound, "no persons found") diff --git a/bff/gapi/rpc_update_payment.go b/bff/gapi/rpc_update_payment.go index b892d81..40d8e47 100644 --- a/bff/gapi/rpc_update_payment.go +++ b/bff/gapi/rpc_update_payment.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "errors" + "log/slog" db "github.com/itsscb/df/bff/db/sqlc" "github.com/itsscb/df/bff/pb" @@ -46,9 +47,7 @@ func (server *Server) UpdatePayment(ctx context.Context, req *pb.UpdatePaymentRe return nil, status.Error(codes.Internal, "failed to get payment") } - accountID := int64(account.ID) - - if dbPayment.AccountID != accountID { + if dbPayment.AccountID != account.ID { if !server.isAdmin(ctx, authPayload) { return nil, status.Error(codes.NotFound, "payment not found") } @@ -93,6 +92,7 @@ func (server *Server) UpdatePayment(ctx context.Context, req *pb.UpdatePaymentRe payment, err := server.store.UpdatePayment(ctx, arg) if err != nil { + slog.Error("Update Payment", slog.Int64("id", int64(req.GetId())), slog.String("error", err.Error())) return nil, status.Error(codes.Internal, "failed to update payment") } diff --git a/bff/pb/rpc_delete_payment.pb.go b/bff/pb/rpc_delete_payment.pb.go index 2ca05c5..4cc13af 100644 --- a/bff/pb/rpc_delete_payment.pb.go +++ b/bff/pb/rpc_delete_payment.pb.go @@ -73,8 +73,8 @@ type DeletePaymentResponse struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` - Deleted bool `protobuf:"varint,2,opt,name=deleted,proto3" json:"deleted,omitempty"` + Id uint64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` + Deleted bool `protobuf:"varint,2,opt,name=deleted,proto3" json:"deleted,omitempty"` } func (x *DeletePaymentResponse) Reset() { @@ -109,7 +109,7 @@ func (*DeletePaymentResponse) Descriptor() ([]byte, []int) { return file_rpc_delete_payment_proto_rawDescGZIP(), []int{1} } -func (x *DeletePaymentResponse) GetId() int64 { +func (x *DeletePaymentResponse) GetId() uint64 { if x != nil { return x.Id } @@ -139,7 +139,7 @@ var file_rpc_delete_payment_proto_rawDesc = []byte{ 0x01, 0x02, 0x69, 0x64, 0x32, 0x0b, 0x7b, 0x22, 0x69, 0x64, 0x22, 0x3a, 0x20, 0x22, 0x31, 0x22, 0x7d, 0x22, 0x7f, 0x0a, 0x15, 0x44, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x50, 0x61, 0x79, 0x6d, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x69, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x64, 0x65, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x04, 0x52, 0x02, 0x69, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x64, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x64, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x64, 0x3a, 0x3c, 0x92, 0x41, 0x39, 0x0a, 0x19, 0x2a, 0x17, 0x44, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x20, 0x50, 0x61, 0x79, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x52, 0x65, 0x73, 0x70, diff --git a/bff/pb/rpc_delete_person.pb.go b/bff/pb/rpc_delete_person.pb.go index 15abbbd..333b791 100644 --- a/bff/pb/rpc_delete_person.pb.go +++ b/bff/pb/rpc_delete_person.pb.go @@ -73,8 +73,8 @@ type DeletePersonResponse struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` - Deleted bool `protobuf:"varint,2,opt,name=deleted,proto3" json:"deleted,omitempty"` + Id uint64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` + Deleted bool `protobuf:"varint,2,opt,name=deleted,proto3" json:"deleted,omitempty"` } func (x *DeletePersonResponse) Reset() { @@ -109,7 +109,7 @@ func (*DeletePersonResponse) Descriptor() ([]byte, []int) { return file_rpc_delete_person_proto_rawDescGZIP(), []int{1} } -func (x *DeletePersonResponse) GetId() int64 { +func (x *DeletePersonResponse) GetId() uint64 { if x != nil { return x.Id } @@ -139,7 +139,7 @@ var file_rpc_delete_person_proto_rawDesc = []byte{ 0x32, 0x0b, 0x7b, 0x22, 0x69, 0x64, 0x22, 0x3a, 0x20, 0x22, 0x31, 0x22, 0x7d, 0x22, 0x7d, 0x0a, 0x14, 0x44, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x50, 0x65, 0x72, 0x73, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, - 0x03, 0x52, 0x02, 0x69, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x64, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x64, + 0x04, 0x52, 0x02, 0x69, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x64, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x64, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x64, 0x3a, 0x3b, 0x92, 0x41, 0x38, 0x0a, 0x18, 0x2a, 0x16, 0x44, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x20, 0x50, 0x65, 0x72, 0x73, 0x6f, 0x6e, 0x20, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x32, 0x1c, diff --git a/bff/proto/rpc_delete_payment.proto b/bff/proto/rpc_delete_payment.proto index 7e01512..f8a2ba6 100644 --- a/bff/proto/rpc_delete_payment.proto +++ b/bff/proto/rpc_delete_payment.proto @@ -27,6 +27,6 @@ message DeletePaymentResponse { }; example: "{\"id\": \"1\", \"deleted\": true}" }; - int64 id = 1; + uint64 id = 1; bool deleted = 2; } \ No newline at end of file diff --git a/bff/proto/rpc_delete_person.proto b/bff/proto/rpc_delete_person.proto index 5f7345f..1d15bbe 100644 --- a/bff/proto/rpc_delete_person.proto +++ b/bff/proto/rpc_delete_person.proto @@ -27,6 +27,6 @@ message DeletePersonResponse { }; example: "{\"id\": \"1\", \"deleted\": true}" }; - int64 id = 1; + uint64 id = 1; bool deleted = 2; } \ No newline at end of file diff --git a/bff/sqlc.yaml b/bff/sqlc.yaml index 92a4550..9fcc26c 100644 --- a/bff/sqlc.yaml +++ b/bff/sqlc.yaml @@ -15,8 +15,19 @@ sql: overrides: - db_type: "bigserial" go_type: "uint64" - - db_type: "bigint" - null: false + - column: "payments.account_id" + go_type: "uint64" + - column: "persons.account_id" + go_type: "uint64" + - column: "documents.account_id" + go_type: "uint64" + - column: "returnsLog.return_id" + go_type: "uint64" + - column: "returnsLog.mail_id" + go_type: "uint64" + - column: "returns.provider_id" + go_type: "uint64" + - column: "returns.person_id" go_type: "uint64" - db_type: "timestamptz" go_type: "time.Time"