genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
hs_monolith/models/good.go

59 lines
1.2 KiB
Go
Raw Normal View History

package models
import (
"errors"
"time"
"github.com/jinzhu/gorm"
"github.com/genofire/hs_master-kss-monolith/lib/database"
)
2017-04-28 10:27:36 +02:00
// this stock microservice manage goods
type Good struct {
ID int64
ProductID int64
Position string
Comment string
FouledAt *time.Time
RecievedAt *time.Time `sql:"default:current_timestamp"`
// Make it temporary unusable
LockedAt *time.Time
LockedSecret string `json:"-"`
// Make it unusable
DeletedAt *time.Time
Sended bool
}
2017-04-28 10:27:36 +02:00
// generate database select which filtered locked goods
func (g *Good) FilterAvailable(db *gorm.DB) *gorm.DB {
return db.Model(g).Where("locked_secret == '' OR locked_secret is NULL")
}
2017-04-28 10:27:36 +02:00
// lock the good, on a way, that it could not be used by other users
func (g *Good) Lock(secret string) {
now := time.Now()
g.LockedSecret = secret
g.LockedAt = &now
}
2017-04-28 10:27:36 +02:00
// is this good locked?
func (g *Good) IsLock() bool {
return len(g.LockedSecret) > 0
}
2017-04-28 10:27:36 +02:00
// unlock the good, that it could be usered again
func (g *Good) Unlock(secret string) error {
if g.LockedSecret == secret {
g.LockedSecret = ""
g.LockedAt = nil
return nil
}
return errors.New("wrong secret")
}
func init() {
database.AddModel(&Good{})
}