diff --git a/db/query/payment.sql b/db/query/payment.sql new file mode 100644 index 0000000..46ca537 --- /dev/null +++ b/db/query/payment.sql @@ -0,0 +1,47 @@ +-- name: GetPayment :one +SELECT * FROM payments +WHERE "ID" = $1 LIMIT 1; + +-- name: CreatePayment :one +INSERT INTO payments ( + "customerID", + "paymentCategory", + bankname, + "IBAN", + "BIC", + "paypalAccount", + "paypalID", + "paymentSystem", + "type", + creator, + changer +) VALUES ( + $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11 +) RETURNING *; + +-- name: ListPayments :many +SELECT * FROM payments +ORDER BY "paymentCategory" +LIMIT $1 +OFFSET $2; + +-- name: UpdatePayment :one +UPDATE payments +SET + "customerID" = COALESCE(sqlc.narg(customerID), "customerID"), + "paymentCategory" = COALESCE(sqlc.narg(paymentCategory), "paymentCategory"), + bankname = COALESCE(sqlc.narg(bankname), bankname), + "IBAN" = COALESCE(sqlc.narg(IBAN), "IBAN"), + "BIC" = COALESCE(sqlc.narg(BIC), "BIC"), + "paypalAccount" = COALESCE(sqlc.narg(paypalAccount), "paypalAccount"), + "paypalID" = COALESCE(sqlc.narg(paypalID), "paypalID"), + "paymentSystem" = COALESCE(sqlc.narg(paymentSystem), "paymentSystem"), + "type" = COALESCE(sqlc.narg(type), "type"), + changer = $2, + changed = now() +WHERE "ID" = $1 +RETURNING *; + +-- name: DeletePayment :exec +DELETE FROM payments +WHERE "ID" = $1; \ No newline at end of file diff --git a/db/sqlc/payment.sql.go b/db/sqlc/payment.sql.go new file mode 100644 index 0000000..0b6d01b --- /dev/null +++ b/db/sqlc/payment.sql.go @@ -0,0 +1,230 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.21.0 +// source: payment.sql + +package db + +import ( + "context" + "database/sql" +) + +const createPayment = `-- name: CreatePayment :one +INSERT INTO payments ( + "customerID", + "paymentCategory", + bankname, + "IBAN", + "BIC", + "paypalAccount", + "paypalID", + "paymentSystem", + "type", + creator, + changer +) VALUES ( + $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11 +) RETURNING "ID", "customerID", "paymentCategory", bankname, "IBAN", "BIC", "paypalAccount", "paypalID", "paymentSystem", type, creator, created, changer, changed +` + +type CreatePaymentParams struct { + CustomerID int64 `json:"customerID"` + PaymentCategory string `json:"paymentCategory"` + Bankname sql.NullString `json:"bankname"` + IBAN sql.NullString `json:"IBAN"` + BIC sql.NullString `json:"BIC"` + PaypalAccount sql.NullString `json:"paypalAccount"` + PaypalID sql.NullString `json:"paypalID"` + PaymentSystem sql.NullString `json:"paymentSystem"` + Type string `json:"type"` + Creator string `json:"creator"` + Changer string `json:"changer"` +} + +func (q *Queries) CreatePayment(ctx context.Context, arg CreatePaymentParams) (Payment, error) { + row := q.db.QueryRowContext(ctx, createPayment, + arg.CustomerID, + arg.PaymentCategory, + arg.Bankname, + arg.IBAN, + arg.BIC, + arg.PaypalAccount, + arg.PaypalID, + arg.PaymentSystem, + arg.Type, + arg.Creator, + arg.Changer, + ) + var i Payment + err := row.Scan( + &i.ID, + &i.CustomerID, + &i.PaymentCategory, + &i.Bankname, + &i.IBAN, + &i.BIC, + &i.PaypalAccount, + &i.PaypalID, + &i.PaymentSystem, + &i.Type, + &i.Creator, + &i.Created, + &i.Changer, + &i.Changed, + ) + return i, err +} + +const deletePayment = `-- name: DeletePayment :exec +DELETE FROM payments +WHERE "ID" = $1 +` + +func (q *Queries) DeletePayment(ctx context.Context, id int64) error { + _, err := q.db.ExecContext(ctx, deletePayment, id) + return err +} + +const getPayment = `-- name: GetPayment :one +SELECT "ID", "customerID", "paymentCategory", bankname, "IBAN", "BIC", "paypalAccount", "paypalID", "paymentSystem", type, creator, created, changer, changed FROM payments +WHERE "ID" = $1 LIMIT 1 +` + +func (q *Queries) GetPayment(ctx context.Context, id int64) (Payment, error) { + row := q.db.QueryRowContext(ctx, getPayment, id) + var i Payment + err := row.Scan( + &i.ID, + &i.CustomerID, + &i.PaymentCategory, + &i.Bankname, + &i.IBAN, + &i.BIC, + &i.PaypalAccount, + &i.PaypalID, + &i.PaymentSystem, + &i.Type, + &i.Creator, + &i.Created, + &i.Changer, + &i.Changed, + ) + return i, err +} + +const listPayments = `-- name: ListPayments :many +SELECT "ID", "customerID", "paymentCategory", bankname, "IBAN", "BIC", "paypalAccount", "paypalID", "paymentSystem", type, creator, created, changer, changed FROM payments +ORDER BY "paymentCategory" +LIMIT $1 +OFFSET $2 +` + +type ListPaymentsParams struct { + Limit int32 `json:"limit"` + Offset int32 `json:"offset"` +} + +func (q *Queries) ListPayments(ctx context.Context, arg ListPaymentsParams) ([]Payment, error) { + rows, err := q.db.QueryContext(ctx, listPayments, arg.Limit, arg.Offset) + if err != nil { + return nil, err + } + defer rows.Close() + items := []Payment{} + for rows.Next() { + var i Payment + if err := rows.Scan( + &i.ID, + &i.CustomerID, + &i.PaymentCategory, + &i.Bankname, + &i.IBAN, + &i.BIC, + &i.PaypalAccount, + &i.PaypalID, + &i.PaymentSystem, + &i.Type, + &i.Creator, + &i.Created, + &i.Changer, + &i.Changed, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const updatePayment = `-- name: UpdatePayment :one +UPDATE payments +SET + "customerID" = COALESCE($3, "customerID"), + "paymentCategory" = COALESCE($4, "paymentCategory"), + bankname = COALESCE($5, bankname), + "IBAN" = COALESCE($6, "IBAN"), + "BIC" = COALESCE($7, "BIC"), + "paypalAccount" = COALESCE($8, "paypalAccount"), + "paypalID" = COALESCE($9, "paypalID"), + "paymentSystem" = COALESCE($10, "paymentSystem"), + "type" = COALESCE($11, "type"), + changer = $2, + changed = now() +WHERE "ID" = $1 +RETURNING "ID", "customerID", "paymentCategory", bankname, "IBAN", "BIC", "paypalAccount", "paypalID", "paymentSystem", type, creator, created, changer, changed +` + +type UpdatePaymentParams struct { + ID int64 `json:"ID"` + Changer string `json:"changer"` + Customerid sql.NullInt64 `json:"customerid"` + Paymentcategory sql.NullString `json:"paymentcategory"` + Bankname sql.NullString `json:"bankname"` + Iban sql.NullString `json:"iban"` + Bic sql.NullString `json:"bic"` + Paypalaccount sql.NullString `json:"paypalaccount"` + Paypalid sql.NullString `json:"paypalid"` + Paymentsystem sql.NullString `json:"paymentsystem"` + Type sql.NullString `json:"type"` +} + +func (q *Queries) UpdatePayment(ctx context.Context, arg UpdatePaymentParams) (Payment, error) { + row := q.db.QueryRowContext(ctx, updatePayment, + arg.ID, + arg.Changer, + arg.Customerid, + arg.Paymentcategory, + arg.Bankname, + arg.Iban, + arg.Bic, + arg.Paypalaccount, + arg.Paypalid, + arg.Paymentsystem, + arg.Type, + ) + var i Payment + err := row.Scan( + &i.ID, + &i.CustomerID, + &i.PaymentCategory, + &i.Bankname, + &i.IBAN, + &i.BIC, + &i.PaypalAccount, + &i.PaypalID, + &i.PaymentSystem, + &i.Type, + &i.Creator, + &i.Created, + &i.Changer, + &i.Changed, + ) + return i, err +} diff --git a/db/sqlc/payment_test.go b/db/sqlc/payment_test.go new file mode 100644 index 0000000..fcaee68 --- /dev/null +++ b/db/sqlc/payment_test.go @@ -0,0 +1,144 @@ +package db + +import ( + "context" + "database/sql" + "testing" + "time" + + "github.com/itsscb/df/util" + "github.com/stretchr/testify/require" +) + +func createRandomPayment(t *testing.T) Payment { + account := createRandomCustomer(t) + require.NotEmpty(t, account) + + arg := CreatePaymentParams{ + CustomerID: account.ID, + PaymentCategory: util.RandomUser(), + Bankname: sql.NullString{ + Valid: true, + String: util.RandomUser(), + }, + IBAN: sql.NullString{ + Valid: true, + String: util.RandomUser(), + }, + BIC: sql.NullString{ + Valid: true, + String: util.RandomUser(), + }, + PaypalAccount: sql.NullString{ + Valid: true, + String: util.RandomUser(), + }, + PaypalID: sql.NullString{ + Valid: true, + String: util.RandomUser(), + }, + PaymentSystem: sql.NullString{ + Valid: true, + String: util.RandomUser(), + }, + Type: util.RandomUser(), + Creator: util.RandomUser(), + } + + person, err := testQueries.CreatePayment(context.Background(), arg) + require.NoError(t, err) + require.NotEmpty(t, person) + + require.Equal(t, arg.PaymentCategory, person.PaymentCategory) + require.Equal(t, arg.Bankname, person.Bankname) + require.Equal(t, arg.CustomerID, person.CustomerID) + 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.NotZero(t, person.ID) + require.NotZero(t, person.Created) + + return person +} + +func TestCreatePayment(t *testing.T) { + createRandomPayment(t) +} + +func TestGetPayment(t *testing.T) { + newperson := createRandomPayment(t) + require.NotEmpty(t, newperson) + + person, err := testQueries.GetPayment(context.Background(), newperson.ID) + require.NoError(t, err) + require.NotEmpty(t, person) + + require.Equal(t, newperson.PaymentCategory, person.PaymentCategory) + require.Equal(t, newperson.Bankname, person.Bankname) + require.Equal(t, newperson.CustomerID, person.CustomerID) + 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.WithinDuration(t, newperson.Created, person.Created, time.Second) +} + +func TestDeletePayment(t *testing.T) { + person1 := createRandomPayment(t) + err := testQueries.DeletePayment(context.Background(), person1.ID) + require.NoError(t, err) + + person2, err := testQueries.GetPayment(context.Background(), person1.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.RandomUser(), + 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) +} + +func TestListPayments(t *testing.T) { + for i := 0; i < 10; i++ { + createRandomPayment(t) + } + + arg := ListPaymentsParams{ + Limit: 5, + Offset: 5, + } + + persons, err := testQueries.ListPayments(context.Background(), arg) + require.NoError(t, err) + require.Len(t, persons, 5) + + for _, person := range persons { + require.NotEmpty(t, person) + } +}