feat: it works
This commit is contained in:
parent
6302dd62d6
commit
778d5ef146
25 changed files with 638 additions and 106 deletions
23
internal/repository/config.go
Normal file
23
internal/repository/config.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
env "github.com/caarlos0/env/v11"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
ConnStr string `env:"CONNSTR" envDefault:"postgres://gsn:gsn@localhost:5432/gsn?sslmode=disable"`
|
||||
}
|
||||
|
||||
func NewConfig(servicePrefix string) (*Config, error) {
|
||||
cfg := &Config{}
|
||||
|
||||
if err := env.ParseWithOptions(cfg, env.Options{
|
||||
PrefixTagName: fmt.Sprintf("%s_REPOSITORY_", servicePrefix),
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
|
@ -1,4 +1,3 @@
|
|||
-- name: GetStationByID :one
|
||||
-- name: GetSatellites :many
|
||||
select *
|
||||
from stations
|
||||
where id = @id;
|
||||
from satellites;
|
||||
|
|
@ -3,24 +3,23 @@ package repository
|
|||
import (
|
||||
"context"
|
||||
|
||||
"git.intra.yksa.space/gsn/gsn-proxy/internal/ds"
|
||||
"git.intra.yksa.space/gsn/gsn-proxy/internal/repository/sqlc"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
type Repository struct {
|
||||
queries *sqlc.Queries
|
||||
cfg *Config
|
||||
}
|
||||
|
||||
func (r *Repository) GetStationByID(ctx context.Context, ID uuid.UUID) (ds.Station, error) {
|
||||
ret, err := r.queries.GetStationByID(ctx, UUIDToPg(ID))
|
||||
func New(cfg *Config) (*Repository, error) {
|
||||
conn, err := pgx.Connect(context.Background(), cfg.ConnStr)
|
||||
if err != nil {
|
||||
return ds.Station{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ds.Station{
|
||||
ID: PGToUUID(ret.ID),
|
||||
Slug: ret.Slug,
|
||||
Status: ds.Status(ret.Status),
|
||||
return &Repository{
|
||||
queries: sqlc.New(conn),
|
||||
cfg: cfg,
|
||||
}, nil
|
||||
}
|
||||
|
|
|
|||
27
internal/repository/satellites.go
Normal file
27
internal/repository/satellites.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"git.intra.yksa.space/gsn/gsn-proxy/internal/pkg/ds"
|
||||
"git.intra.yksa.space/gsn/gsn-proxy/internal/pkg/errcodes"
|
||||
)
|
||||
|
||||
func (r *Repository) GetSatellites(ctx context.Context) ([]ds.Satellite, error) {
|
||||
satellites, err := r.queries.GetSatellites(ctx)
|
||||
if err != nil {
|
||||
return nil, errcodes.New(http.StatusInternalServerError, "failed to get satellites", err.Error())
|
||||
}
|
||||
|
||||
ret := make([]ds.Satellite, 0, len(satellites))
|
||||
for _, val := range satellites {
|
||||
ret = append(ret, ds.Satellite{
|
||||
ID: PGToUUID(val.ID),
|
||||
DisplayName: val.DisplayName,
|
||||
Status: ds.Status(val.Status),
|
||||
})
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
|
@ -7,19 +7,29 @@ package sqlc
|
|||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const getStationByID = `-- name: GetStationByID :one
|
||||
select id, slug, status
|
||||
from stations
|
||||
where id = $1
|
||||
const getSatellites = `-- name: GetSatellites :many
|
||||
select id, display_name, status
|
||||
from satellites
|
||||
`
|
||||
|
||||
func (q *Queries) GetStationByID(ctx context.Context, id pgtype.UUID) (Station, error) {
|
||||
row := q.db.QueryRow(ctx, getStationByID, id)
|
||||
var i Station
|
||||
err := row.Scan(&i.ID, &i.Slug, &i.Status)
|
||||
return i, err
|
||||
func (q *Queries) GetSatellites(ctx context.Context) ([]Satellite, error) {
|
||||
rows, err := q.db.Query(ctx, getSatellites)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Satellite
|
||||
for rows.Next() {
|
||||
var i Satellite
|
||||
if err := rows.Scan(&i.ID, &i.DisplayName, &i.Status); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue