69 lines
2.1 KiB
Go
69 lines
2.1 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
type Account struct {
|
|
ID int32 `json:"id"`
|
|
Nom string `json:"nom"`
|
|
Type string `json:"type"`
|
|
DeviseReference string `json:"devise_reference"`
|
|
Plafond *float64 `json:"plafond,omitempty"`
|
|
}
|
|
|
|
type CreateAccountParams struct {
|
|
Nom string `json:"nom"`
|
|
Type string `json:"type"`
|
|
DeviseReference string `json:"devise_reference"`
|
|
Plafond *float64 `json:"plafond,omitempty"`
|
|
}
|
|
|
|
func (s *Store) ListAccounts(ctx context.Context) ([]Account, error) {
|
|
rows, err := s.pool.Query(ctx,
|
|
`SELECT id, nom, type, devise_reference, plafond FROM account ORDER BY nom`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return pgx.CollectRows(rows, pgx.RowToStructByName[Account])
|
|
}
|
|
|
|
func (s *Store) GetAccount(ctx context.Context, id int32) (Account, error) {
|
|
rows, err := s.pool.Query(ctx,
|
|
`SELECT id, nom, type, devise_reference, plafond FROM account WHERE id = $1`, id)
|
|
if err != nil {
|
|
return Account{}, err
|
|
}
|
|
return pgx.CollectOneRow(rows, pgx.RowToStructByName[Account])
|
|
}
|
|
|
|
func (s *Store) CreateAccount(ctx context.Context, p CreateAccountParams) (Account, error) {
|
|
rows, err := s.pool.Query(ctx,
|
|
`INSERT INTO account (nom, type, devise_reference, plafond, taux)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
RETURNING id, nom, type, devise_reference, plafond`,
|
|
p.Nom, p.Type, p.DeviseReference, p.Plafond)
|
|
if err != nil {
|
|
return Account{}, err
|
|
}
|
|
return pgx.CollectOneRow(rows, pgx.RowToStructByName[Account])
|
|
}
|
|
|
|
func (s *Store) UpdateAccount(ctx context.Context, id int32, p CreateAccountParams) (Account, error) {
|
|
rows, err := s.pool.Query(ctx,
|
|
`UPDATE account SET nom = $2, type = $3, devise_reference = $4, plafond = $5
|
|
WHERE id = $1
|
|
RETURNING id, nom, type, devise_reference, plafond`,
|
|
id, p.Nom, p.Type, p.DeviseReference, p.Plafond)
|
|
if err != nil {
|
|
return Account{}, err
|
|
}
|
|
return pgx.CollectOneRow(rows, pgx.RowToStructByName[Account])
|
|
}
|
|
|
|
func (s *Store) DeleteAccount(ctx context.Context, id int32) error {
|
|
_, err := s.pool.Exec(ctx, `DELETE FROM account WHERE id = $1`, id)
|
|
return err
|
|
}
|