154 lines
4.5 KiB
Go
154 lines
4.5 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
type Repository struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
func NewRepository(pool *pgxpool.Pool) *Repository {
|
|
return &Repository{
|
|
pool: pool,
|
|
}
|
|
}
|
|
|
|
func (r *Repository) GetBales(ctx context.Context) ([]Bale, error) {
|
|
rows, err := r.pool.Query(ctx, `
|
|
SELECT b.id, b.type_id, COALESCE(bt.type, ''), b.timestamp
|
|
FROM bales b
|
|
LEFT JOIN bale_types bt ON b.type_id = bt.id
|
|
ORDER BY b.id`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var bales []Bale
|
|
for rows.Next() {
|
|
var b Bale
|
|
if err := rows.Scan(&b.ID, &b.TypeID, &b.Type, &b.Timestamp); err != nil {
|
|
return nil, err
|
|
}
|
|
bales = append(bales, b)
|
|
}
|
|
return bales, nil
|
|
}
|
|
|
|
func (r *Repository) GetBaleByID(ctx context.Context, id string) (*Bale, error) {
|
|
var b Bale
|
|
err := r.pool.QueryRow(ctx, `
|
|
SELECT b.id, b.type_id, COALESCE(bt.type, ''), b.timestamp
|
|
FROM bales b
|
|
LEFT JOIN bale_types bt ON b.type_id = bt.id
|
|
WHERE b.id = $1`, id).Scan(&b.ID, &b.TypeID, &b.Type, &b.Timestamp)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &b, nil
|
|
}
|
|
|
|
func (r *Repository) CreateBale(ctx context.Context, input Bale) (*Bale, error) {
|
|
var b Bale
|
|
err := r.pool.QueryRow(ctx, `
|
|
INSERT INTO bales (type_id) VALUES ($1)
|
|
RETURNING id, type_id, timestamp`, input.TypeID).Scan(&b.ID, &b.TypeID, &b.Timestamp)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if input.TypeID > 0 {
|
|
var bt BaleType
|
|
r.pool.QueryRow(ctx, "SELECT id, type FROM bale_types WHERE id = $1", b.TypeID).Scan(&bt.ID, &bt.Type)
|
|
b.Type = bt.Type
|
|
}
|
|
return &b, nil
|
|
}
|
|
|
|
func (r *Repository) UpdateBale(ctx context.Context, id string, input Bale) (*Bale, error) {
|
|
var b Bale
|
|
err := r.pool.QueryRow(ctx, `
|
|
UPDATE bales SET type_id = $1 WHERE id = $2
|
|
RETURNING id, type_id, timestamp`, input.TypeID, id).Scan(&b.ID, &b.TypeID, &b.Timestamp)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if input.TypeID > 0 {
|
|
var bt BaleType
|
|
r.pool.QueryRow(ctx, "SELECT id, type FROM bale_types WHERE id = $1", b.TypeID).Scan(&bt.ID, &bt.Type)
|
|
b.Type = bt.Type
|
|
}
|
|
return &b, nil
|
|
}
|
|
|
|
func (r *Repository) DeleteBale(ctx context.Context, id string) error {
|
|
_, err := r.pool.Exec(ctx, "DELETE FROM bales WHERE id = $1", id)
|
|
return err
|
|
}
|
|
|
|
func (r *Repository) GetBaleTypes(ctx context.Context) ([]BaleType, error) {
|
|
rows, err := r.pool.Query(ctx, "SELECT id, type, weight, height, width, length FROM bale_types ORDER BY id")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var types []BaleType
|
|
for rows.Next() {
|
|
var bt BaleType
|
|
if err := rows.Scan(&bt.ID, &bt.Type, &bt.Weight, &bt.Height, &bt.Width, &bt.Length); err != nil {
|
|
return nil, err
|
|
}
|
|
types = append(types, bt)
|
|
}
|
|
return types, nil
|
|
}
|
|
|
|
func (r *Repository) GetBaleTypeByID(ctx context.Context, id int) (*BaleType, error) {
|
|
var bt BaleType
|
|
err := r.pool.QueryRow(ctx, "SELECT id, type, weight, height, width, length FROM bale_types WHERE id = $1", id).Scan(&bt.ID, &bt.Type, &bt.Weight, &bt.Height, &bt.Width, &bt.Length)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &bt, nil
|
|
}
|
|
|
|
func (r *Repository) GetBaleTypeByType(ctx context.Context, typeName string) (*BaleType, error) {
|
|
var bt BaleType
|
|
err := r.pool.QueryRow(ctx, "SELECT id, type, weight, height, width, length FROM bale_types WHERE type = $1", typeName).Scan(&bt.ID, &bt.Type, &bt.Weight, &bt.Height, &bt.Width, &bt.Length)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &bt, nil
|
|
}
|
|
|
|
func (r *Repository) CreateBaleType(ctx context.Context, input BaleType) (*BaleType, error) {
|
|
var bt BaleType
|
|
err := r.pool.QueryRow(ctx, `
|
|
INSERT INTO bale_types (type, weight, height, width, length)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
RETURNING id, type, weight, height, width, length`, input.Type, input.Weight, input.Height, input.Width, input.Length).Scan(&bt.ID, &bt.Type, &bt.Weight, &bt.Height, &bt.Width, &bt.Length)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &bt, nil
|
|
}
|
|
|
|
func (r *Repository) UpdateBaleType(ctx context.Context, id int, input BaleType) (*BaleType, error) {
|
|
var bt BaleType
|
|
err := r.pool.QueryRow(ctx, `
|
|
UPDATE bale_types SET type = $1, weight = $2, height = $3, width = $4, length = $5 WHERE id = $6
|
|
RETURNING id, type, weight, height, width, length`, input.Type, input.Weight, input.Height, input.Width, input.Length, id).Scan(&bt.ID, &bt.Type, &bt.Weight, &bt.Height, &bt.Width, &bt.Length)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &bt, nil
|
|
}
|
|
|
|
func (r *Repository) DeleteBaleType(ctx context.Context, id int) error {
|
|
_, err := r.pool.Exec(ctx, "DELETE FROM bale_types WHERE id = $1", id)
|
|
return err
|
|
}
|