2024-10-04 20:02:51 +02:00
|
|
|
package storage
|
|
|
|
|
2024-10-06 04:55:38 +02:00
|
|
|
import (
|
|
|
|
"context"
|
2024-10-07 10:25:25 +02:00
|
|
|
"fmt"
|
2024-10-07 10:38:01 +02:00
|
|
|
"sync"
|
2024-10-07 11:32:26 +02:00
|
|
|
"time"
|
2024-10-06 04:55:38 +02:00
|
|
|
|
|
|
|
"git.espin.casa/albert/cml04-falcon-ui/types"
|
2024-10-07 10:38:01 +02:00
|
|
|
"gorm.io/driver/postgres"
|
|
|
|
"gorm.io/gorm"
|
2024-10-06 04:55:38 +02:00
|
|
|
)
|
|
|
|
|
2024-10-07 10:38:01 +02:00
|
|
|
type DBConfig struct {
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2024-10-06 04:55:38 +02:00
|
|
|
type Storager interface {
|
|
|
|
Barcode(ctx context.Context, reading string) (barcode *types.Barcode, err error)
|
|
|
|
ListBarcode(ctx context.Context, lb types.LoadingBed, inicio, final string) (barcodes []types.Barcode, err error)
|
|
|
|
Bundle(ctx context.Context, ua string) (bundle *types.BundleData, err error)
|
2024-10-07 11:32:26 +02:00
|
|
|
ListBundle(ctx context.Context, lb types.LoadingBed, inicio string, final string) (bundles []types.BundleData, err error)
|
2024-10-06 04:55:38 +02:00
|
|
|
}
|
2024-10-04 20:02:51 +02:00
|
|
|
|
2024-10-07 10:38:01 +02:00
|
|
|
type storage struct {
|
|
|
|
db *gorm.DB
|
|
|
|
mux sync.RWMutex
|
|
|
|
}
|
2024-10-04 20:02:51 +02:00
|
|
|
|
2024-10-06 04:55:38 +02:00
|
|
|
// ListBundle implements Storager.
|
2024-10-07 11:32:26 +02:00
|
|
|
func (s *storage) ListBundle(ctx context.Context, lb types.LoadingBed, inicio string, final string) (bundles []types.BundleData, err error) {
|
|
|
|
ti, err := time.Parse("02/01/2006 15:04", inicio)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tf, err := time.Parse("02/01/2006 15:04", final)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if lb == types.ALL {
|
|
|
|
if err := s.db.Where("created_at >= ? and created_at < ?", ti, tf).Find(bundles).WithContext(ctx).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := s.db.Where("loading_bed = ? and created_at >= ? and created_at < ?", lb, ti, tf).Find(bundles).WithContext(ctx).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2024-10-06 04:55:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bundle implements Storager.
|
|
|
|
func (s *storage) Bundle(ctx context.Context, ua string) (bundle *types.BundleData, err error) {
|
2024-10-07 11:32:26 +02:00
|
|
|
if err := s.db.First(bundle, ua).WithContext(ctx).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return
|
2024-10-06 04:55:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListBarcode implements Storager.
|
|
|
|
func (s *storage) ListBarcode(ctx context.Context, lb types.LoadingBed, inicio string, final string) (barcodes []types.Barcode, err error) {
|
2024-10-07 11:32:26 +02:00
|
|
|
ti, err := time.Parse("02/01/2006 15:04", inicio)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tf, err := time.Parse("02/01/2006 15:04", final)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-10-07 15:24:46 +02:00
|
|
|
|
2024-10-07 11:32:26 +02:00
|
|
|
if lb == types.ALL {
|
2024-10-07 15:24:46 +02:00
|
|
|
if err := s.db.Where("created_at >= ? and created_at < ?", ti, tf).Find(&barcodes).WithContext(ctx).Error; err != nil {
|
2024-10-07 11:32:26 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2024-10-07 15:24:46 +02:00
|
|
|
if err := s.db.Where("loading_bed = ? and created_at >= ? and created_at < ?", lb, ti, tf).Find(&barcodes).WithContext(ctx).Error; err != nil {
|
2024-10-07 11:32:26 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2024-10-07 15:24:46 +02:00
|
|
|
fmt.Printf("result: +%v\n", barcodes)
|
2024-10-07 11:32:26 +02:00
|
|
|
return
|
2024-10-06 04:55:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Barcode implements Storager.
|
|
|
|
func (s *storage) Barcode(ctx context.Context, reading string) (barcode *types.Barcode, err error) {
|
2024-10-07 15:24:46 +02:00
|
|
|
if err := s.db.First(&barcode, reading).WithContext(ctx).Error; err != nil {
|
2024-10-07 11:32:26 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return
|
2024-10-06 04:55:38 +02:00
|
|
|
}
|
|
|
|
|
2024-10-07 10:38:01 +02:00
|
|
|
func ProductionDataBase(conf *DBConfig) (*gorm.DB, error) {
|
|
|
|
// create dsn string
|
|
|
|
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Europe/Madrid",
|
|
|
|
conf.Host,
|
|
|
|
conf.Username,
|
|
|
|
conf.Password,
|
|
|
|
conf.Name,
|
|
|
|
conf.Port,
|
|
|
|
)
|
|
|
|
// create open database connection
|
|
|
|
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(conf *DBConfig) (Storager, error) {
|
|
|
|
// database holder
|
|
|
|
var db *gorm.DB
|
|
|
|
// producctio
|
|
|
|
db, err := ProductionDataBase(conf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// done
|
|
|
|
return &storage{
|
|
|
|
db: db,
|
|
|
|
mux: sync.RWMutex{},
|
|
|
|
}, nil
|
2024-10-04 20:02:51 +02:00
|
|
|
}
|