chore: add qe and integrations with rabbit mq

This commit is contained in:
d3m0k1d
2026-04-29 14:26:59 +03:00
parent fe740da06b
commit d3a2fe0f9c
18 changed files with 1823 additions and 47 deletions
+138
View File
@@ -1,6 +1,8 @@
package storage
import (
"context"
"github.com/jackc/pgx/v5/pgxpool"
)
@@ -13,3 +15,139 @@ func NewRepository(pool *pgxpool.Pool) *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
}
+10 -3
View File
@@ -1,5 +1,9 @@
package storage
import "time"
// BaleType Тип тюка
// @Description Тип тюка - характеристики тюка
type BaleType struct {
ID int `json:"id"`
Type string `json:"type"`
@@ -9,10 +13,13 @@ type BaleType struct {
Length float64 `json:"length"`
}
// Bale Тюк
// @Description Тюк - единица готовой продукции
type Bale struct {
ID int `json:"id"`
Type string `json:"type"`
Timestamp string `json:"timestamp"`
ID int `json:"id"`
TypeID int `json:"typeId"`
Type string `json:"type"`
Timestamp time.Time `json:"timestamp"`
}
type User struct {