2017-05-03 07:30:18 +02:00
|
|
|
// Package with the mostly static content (models) of this microservice
|
2017-04-03 14:59:43 +02:00
|
|
|
package models
|
|
|
|
|
2017-04-04 19:28:46 +02:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
|
|
|
|
"github.com/genofire/hs_master-kss-monolith/lib/database"
|
|
|
|
)
|
2017-04-03 14:59:43 +02:00
|
|
|
|
2017-05-03 07:30:18 +02:00
|
|
|
// Goods managemd in this stock microservice
|
2017-04-03 14:59:43 +02:00
|
|
|
type Good struct {
|
|
|
|
ID int64
|
|
|
|
ProductID int64
|
2017-04-04 19:28:46 +02:00
|
|
|
Position string
|
2017-04-03 14:59:43 +02:00
|
|
|
Comment string
|
2017-04-04 19:28:46 +02:00
|
|
|
FouledAt *time.Time
|
|
|
|
|
|
|
|
RecievedAt *time.Time `sql:"default:current_timestamp"`
|
|
|
|
// Make it temporary unusable
|
|
|
|
LockedAt *time.Time
|
2017-04-05 19:03:44 +02:00
|
|
|
LockedSecret string `json:"-"`
|
2017-04-04 19:28:46 +02:00
|
|
|
// Make it unusable
|
|
|
|
DeletedAt *time.Time
|
|
|
|
Sended bool
|
|
|
|
}
|
|
|
|
|
2017-05-03 07:30:18 +02:00
|
|
|
// Function to enerate a database and select locked goods with a filter
|
2017-04-04 19:28:46 +02:00
|
|
|
func (g *Good) FilterAvailable(db *gorm.DB) *gorm.DB {
|
2017-04-04 23:21:05 +02:00
|
|
|
return db.Model(g).Where("locked_secret == '' OR locked_secret is NULL")
|
2017-04-04 19:28:46 +02:00
|
|
|
}
|
|
|
|
|
2017-05-03 07:30:18 +02:00
|
|
|
// Function to lock a good, so that it cannot be locked or bought by other users
|
2017-04-04 19:28:46 +02:00
|
|
|
func (g *Good) Lock(secret string) {
|
|
|
|
now := time.Now()
|
|
|
|
g.LockedSecret = secret
|
|
|
|
g.LockedAt = &now
|
|
|
|
}
|
2017-04-28 10:27:36 +02:00
|
|
|
|
2017-05-03 07:30:18 +02:00
|
|
|
// Function to check if a good is locked
|
2017-04-04 19:28:46 +02:00
|
|
|
func (g *Good) IsLock() bool {
|
|
|
|
return len(g.LockedSecret) > 0
|
|
|
|
}
|
2017-04-28 10:27:36 +02:00
|
|
|
|
2017-05-03 07:30:18 +02:00
|
|
|
// Function to unlock a good
|
2017-04-04 19:28:46 +02:00
|
|
|
func (g *Good) Unlock(secret string) error {
|
|
|
|
if g.LockedSecret == secret {
|
|
|
|
g.LockedSecret = ""
|
|
|
|
g.LockedAt = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New("wrong secret")
|
2017-04-03 14:59:43 +02:00
|
|
|
}
|
|
|
|
|
2017-05-03 07:30:18 +02:00
|
|
|
// Function to initialize the database
|
2017-04-03 14:59:43 +02:00
|
|
|
func init() {
|
|
|
|
database.AddModel(&Good{})
|
|
|
|
}
|