cml04-mediciones-service/internal/storage/storage.go
2024-08-20 09:09:31 +02:00

165 lines
5.0 KiB
Go

package storage
import (
"context"
"fmt"
"time"
"git.espin.casa/albert/cml04-mediciones-service/internal/types"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
type IStorage interface {
GetProductTolerance(ctx context.Context, product string) (types.Tolerancia, error)
CreateTolerance(ctx context.Context, tolerance types.Tolerancia) error
CreateProductionOrder(ctx context.Context, po *types.OrdenFabricacion) error
GetProductionOrder(ctx context.Context, orderNo int) (*types.OrdenFabricacion, error)
GetProductionOrders(ctx context.Context, limit int) ([]types.OrdenFabricacion, error)
PostMediciones(ctx context.Context, medicion *types.Mediciones) error
GetMediciones(ctx context.Context, pono int) ([]types.Mediciones, error)
GetMedicion(ctx context.Context, id uint64) (*types.Mediciones, error)
CreateUser(ctx context.Context, user *types.Usuario) error
GetUser(ctx context.Context, email string) (*types.Usuario, error)
GetExcel(ctx context.Context, product, startTime, endTime string) ([]types.Mediciones, error)
}
type StorageConfig struct {
User string
Pass string
Host string
Port string
DBName string
}
type storage struct {
db *gorm.DB
}
// GetExcel implements IStorage.
func (s *storage) GetExcel(ctx context.Context, product string, startTime string, endTime string) ([]types.Mediciones, error) {
mediciones := []types.Mediciones{}
sTime, err := time.Parse("02/01/2006 15:04:03", startTime)
if err != nil {
return nil, err
}
eTime, err := time.Parse("02/01/2006 15:04:03", endTime)
if err != nil {
return nil, err
}
if err := s.db.Where("producto = ? AND created_at BETWEEN ? AND ?", product, sTime, eTime).Find(&mediciones).Error; err != nil {
return mediciones, err
}
return mediciones, nil
}
// CreateUSer implements IStorage.
func (s *storage) CreateUser(ctx context.Context, user *types.Usuario) error {
return s.db.Create(user).Error
}
// GetUser implements IStorage.
func (s *storage) GetUser(ctx context.Context, email string) (*types.Usuario, error) {
user := &types.Usuario{}
if err := s.db.Where("email", email).Find(user).Error; err != nil {
return nil, err
}
return user, nil
}
// GetMedicion implements IStorage.
func (s *storage) GetMedicion(ctx context.Context, id uint64) (*types.Mediciones, error) {
medicion := &types.Mediciones{}
if err := s.db.First(medicion, id).Error; err != nil {
return nil, err
}
return medicion, nil
}
// GetMediciones implements IStorage.
func (s *storage) GetMediciones(ctx context.Context, pono int) ([]types.Mediciones, error) {
// tolerance var holder
mediciones := []types.Mediciones{}
// get mediciones
if err := s.db.Order("created_at desc").Where("p_order_no = ?", pono).Find(&mediciones).Error; err != nil {
return nil, err
}
return mediciones, nil
}
// PostMediciones implements IStorage.
func (s *storage) PostMediciones(ctx context.Context, medicion *types.Mediciones) error {
return s.db.Create(&medicion).Error
}
// GetProductionOrders implements IStorage.
func (s *storage) GetProductionOrders(ctx context.Context, limit int) ([]types.OrdenFabricacion, error) {
// result holder
res := []types.OrdenFabricacion{}
if err := s.db.Order("created_at desc").Limit(limit).Find(&res).Error; err != nil {
return nil, err
}
return res, nil
}
// GetProductionOrder implements IStorage.
func (s *storage) GetProductionOrder(ctx context.Context, orderNo int) (*types.OrdenFabricacion, error) {
// tolerance var holder
tolerancia := &types.OrdenFabricacion{}
// get medida by product/medida
if err := s.db.Where("p_order_no = ?", orderNo).First(tolerancia).Error; err != nil {
return nil, err
}
return tolerancia, nil
}
// CreateProductionOrder implements IStorage.
func (s *storage) CreateProductionOrder(ctx context.Context, po *types.OrdenFabricacion) error {
if err := s.db.Create(po).Error; err != nil {
return err
}
return nil
}
// CreateTolerance implements IStorage.
func (s *storage) CreateTolerance(ctx context.Context, tolerance types.Tolerancia) error {
if err := s.db.Create(&tolerance).Error; err != nil {
return err
}
return nil
}
// GetProductTolerance implements IStorage.
func (s *storage) GetProductTolerance(ctx context.Context, product string) (types.Tolerancia, error) {
// tolerance var holder
tolerancia := types.Tolerancia{}
// get medida by product/medida
if err := s.db.Where("medida = ?", product).First(&tolerancia).Error; err != nil {
return types.Tolerancia{}, err
}
return tolerancia, nil
}
func AutoMigrate(db *gorm.DB) error {
return db.AutoMigrate(
&types.Tolerancia{},
&types.OrdenFabricacion{},
&types.Mediciones{},
&types.Usuario{},
)
}
func NewStorage(config *StorageConfig) (IStorage, error) {
// create dsn string
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", config.User, config.Pass, config.Host, config.Port, config.DBName)
// create database connection
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
// auto migrate database
if err := AutoMigrate(db); err != nil {
return nil, err
}
return &storage{
db: db,
}, err
}