cml04-falcon-system/internal/storage/storage.go

127 lines
3.2 KiB
Go
Raw Normal View History

2024-10-01 20:28:58 +02:00
package storage
import (
"context"
"fmt"
2024-10-03 00:32:27 +02:00
"sync"
2024-10-01 20:28:58 +02:00
"git.espin.casa/albert/cml04-falcon-system/internal/types"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
type Storager interface {
StoreBarcode(ctx context.Context, in *types.Barcode) error
2024-10-02 18:51:41 +02:00
StoreProductionOrder(ctx context.Context, in *types.ProductionOrder) error
2024-10-03 00:29:18 +02:00
StoreCustomerOrder(ctx context.Context, in *types.CustomerOrder) error
2024-10-03 09:32:24 +02:00
StoreBeamCutPattern(ctx context.Context, in *types.BeamCutPattern) error
StoreLayerCutPattern(ctx context.Context, in *types.LayerCutPattern) error
StoreBeamGroup(ctx context.Context, in *types.BeamGroup) error
2024-10-03 10:10:11 +02:00
StoreBundle(ctx context.Context, in *types.BundleData) error
2024-10-02 13:35:38 +02:00
ConfirmBundle(ctx context.Context, barcode string) error
2024-10-01 20:28:58 +02:00
}
type storage struct {
2024-10-03 00:32:27 +02:00
db *gorm.DB
mux sync.RWMutex
2024-10-01 20:28:58 +02:00
}
type DBConfig struct {
Username string
Password string
Host string
Port int
Name string
}
2024-10-03 10:10:11 +02:00
// StoreBundle implements Storager.
func (s *storage) StoreBundle(ctx context.Context, in *types.BundleData) error {
return s.db.Save(in).Error
}
2024-10-03 09:32:24 +02:00
// StoreBeamGroup implements Storager.
func (s *storage) StoreBeamGroup(ctx context.Context, in *types.BeamGroup) error {
return s.db.Save(in).Error
}
// StoreLayerCutPattern implements Storager.
func (s *storage) StoreLayerCutPattern(ctx context.Context, in *types.LayerCutPattern) error {
return s.db.Save(in).Error
}
// StoreBeamCutPattern implements Storager.
func (s *storage) StoreBeamCutPattern(ctx context.Context, in *types.BeamCutPattern) error {
return s.db.Save(in).Error
}
2024-10-03 00:29:18 +02:00
// StoreCustomerOrder implements Storager.
func (s *storage) StoreCustomerOrder(ctx context.Context, in *types.CustomerOrder) error {
return s.db.Save(in).Error
}
2024-10-02 18:51:41 +02:00
// StoreProductionOrder implements Storager.
func (s *storage) StoreProductionOrder(ctx context.Context, in *types.ProductionOrder) error {
return s.db.Save(in).Error
}
2024-10-02 13:35:38 +02:00
// ConfirmBundle implements Storager.
func (s *storage) ConfirmBundle(ctx context.Context, barcode string) error {
2024-10-17 11:57:15 +02:00
bc := &types.Barcode{}
if err := s.db.First(&bc, barcode).Error; err != nil {
return err
}
return s.db.Model(&bc).Where("nromatricula = ?", barcode).Update("confirmed", true).Error
2024-10-02 13:35:38 +02:00
}
2024-10-01 20:28:58 +02:00
// StoreBarcode implements IStorage.
func (s *storage) StoreBarcode(ctx context.Context, in *types.Barcode) error {
return s.db.Save(in).Error
}
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 Automigrate(db *gorm.DB) error {
return db.AutoMigrate(
&types.Barcode{},
&types.ProductionOrder{},
2024-10-03 00:29:18 +02:00
&types.CustomerOrder{},
2024-10-03 08:44:48 +02:00
&types.BeamCutPattern{},
&types.LayerCutPattern{},
2024-10-03 09:32:24 +02:00
&types.BeamGroup{},
2024-10-01 20:28:58 +02:00
)
}
func New(conf *DBConfig) (Storager, error) {
// database holder
var db *gorm.DB
// producctio
db, err := ProductionDataBase(conf)
if err != nil {
return nil, err
}
// auto migration
if err := Automigrate(db); err != nil {
return nil, err
}
// done
return &storage{
2024-10-03 00:32:27 +02:00
db: db,
mux: sync.RWMutex{},
2024-10-01 20:28:58 +02:00
}, nil
}