feat(store): pgx pool and endpoints CRUD
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package store
|
||||
|
||||
import "context"
|
||||
|
||||
type Endpoint struct {
|
||||
ID int64
|
||||
RoleLabel string
|
||||
Host string
|
||||
Port int
|
||||
TLSMode string
|
||||
}
|
||||
|
||||
func (s *Store) CreateEndpoint(ctx context.Context, e Endpoint) (int64, error) {
|
||||
var id int64
|
||||
err := s.Pool.QueryRow(ctx,
|
||||
`INSERT INTO endpoints (role_label, host, port, tls_mode)
|
||||
VALUES ($1,$2,$3,$4) RETURNING id`,
|
||||
e.RoleLabel, e.Host, e.Port, e.TLSMode).Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
|
||||
func (s *Store) GetEndpoint(ctx context.Context, id int64) (Endpoint, error) {
|
||||
var e Endpoint
|
||||
err := s.Pool.QueryRow(ctx,
|
||||
`SELECT id, role_label, host, port, tls_mode FROM endpoints WHERE id=$1`, id).
|
||||
Scan(&e.ID, &e.RoleLabel, &e.Host, &e.Port, &e.TLSMode)
|
||||
return e, err
|
||||
}
|
||||
|
||||
func (s *Store) ListEndpoints(ctx context.Context) ([]Endpoint, error) {
|
||||
rows, err := s.Pool.Query(ctx,
|
||||
`SELECT id, role_label, host, port, tls_mode FROM endpoints ORDER BY id`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []Endpoint
|
||||
for rows.Next() {
|
||||
var e Endpoint
|
||||
if err := rows.Scan(&e.ID, &e.RoleLabel, &e.Host, &e.Port, &e.TLSMode); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, e)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
Reference in New Issue
Block a user