78 lines
2.4 KiB
Go
78 lines
2.4 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
type Instrument struct {
|
|
ID int32 `json:"id"`
|
|
Type string `json:"type"`
|
|
Code string `json:"code"`
|
|
Name string `json:"name"`
|
|
DeviseCotation string `json:"devise_cotation"`
|
|
}
|
|
|
|
type CreateInstrumentParams struct {
|
|
Type string `json:"type"`
|
|
Code string `json:"code"`
|
|
Name string `json:"name"`
|
|
DeviseCotation string `json:"devise_cotation"`
|
|
}
|
|
|
|
func (s *Store) ListInstruments(ctx context.Context) ([]Instrument, error) {
|
|
rows, err := s.pool.Query(ctx,
|
|
`SELECT id, type, code, name, devise_cotation FROM instrument ORDER BY type, code`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return pgx.CollectRows(rows, pgx.RowToStructByName[Instrument])
|
|
}
|
|
|
|
func (s *Store) GetInstrument(ctx context.Context, id int32) (Instrument, error) {
|
|
rows, err := s.pool.Query(ctx,
|
|
`SELECT id, type, code, name, devise_cotation FROM instrument WHERE id = $1`, id)
|
|
if err != nil {
|
|
return Instrument{}, err
|
|
}
|
|
return pgx.CollectOneRow(rows, pgx.RowToStructByName[Instrument])
|
|
}
|
|
|
|
func (s *Store) GetInstrumentByCode(ctx context.Context, code string) (Instrument, error) {
|
|
rows, err := s.pool.Query(ctx,
|
|
`SELECT id, type, code, name, devise_cotation FROM instrument WHERE code = $1`, code)
|
|
if err != nil {
|
|
return Instrument{}, err
|
|
}
|
|
return pgx.CollectOneRow(rows, pgx.RowToStructByName[Instrument])
|
|
}
|
|
|
|
func (s *Store) CreateInstrument(ctx context.Context, p CreateInstrumentParams) (Instrument, error) {
|
|
rows, err := s.pool.Query(ctx,
|
|
`INSERT INTO instrument (type, code, name, devise_cotation)
|
|
VALUES ($1, $2, $3, $4)
|
|
RETURNING id, type, code, name, devise_cotation`,
|
|
p.Type, p.Code, p.Name, p.DeviseCotation)
|
|
if err != nil {
|
|
return Instrument{}, err
|
|
}
|
|
return pgx.CollectOneRow(rows, pgx.RowToStructByName[Instrument])
|
|
}
|
|
|
|
func (s *Store) UpdateInstrument(ctx context.Context, id int32, p CreateInstrumentParams) (Instrument, error) {
|
|
rows, err := s.pool.Query(ctx,
|
|
`UPDATE instrument SET type = $2, code = $3, name = $4, devise_cotation = $5
|
|
WHERE id = $1
|
|
RETURNING id, type, code, name, devise_cotation`,
|
|
id, p.Type, p.Code, p.Name, p.DeviseCotation)
|
|
if err != nil {
|
|
return Instrument{}, err
|
|
}
|
|
return pgx.CollectOneRow(rows, pgx.RowToStructByName[Instrument])
|
|
}
|
|
|
|
func (s *Store) DeleteInstrument(ctx context.Context, id int32) error {
|
|
_, err := s.pool.Exec(ctx, `DELETE FROM instrument WHERE id = $1`, id)
|
|
return err
|
|
}
|