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

62 lines
1.5 KiB
Go
Raw Normal View History

2017-05-03 07:30:18 +02:00
// Package with the mostly static content (models) of this microservice
package models
import (
"errors"
"time"
"github.com/jinzhu/gorm"
"github.com/genofire/hs_master-kss-monolith/lib/database"
)
// Goods managed in this stock microservice
type Good struct {
2017-05-12 10:17:27 +02:00
ID int64 `json:"id"`
ProductID int64 `json:"product_id"`
Position string `json:"position"`
Comment string `json:"comment"`
FouledAt *time.Time `json:"fouled_at"`
2017-05-12 10:17:27 +02:00
RecievedAt *time.Time `sql:"default:current_timestamp" json:"recieved_at"`
// Make it temporary unusable
2017-05-12 10:17:27 +02:00
LockedAt *time.Time `json:"-"`
LockedSecret string `json:"-"`
// Make it unusable
2017-05-18 23:42:00 +02:00
DeletedAt *time.Time `json:"-"`
ManuelleDelete bool `json:"-"`
FouledDelete bool `json:"-"`
}
2017-05-15 10:22:24 +02:00
// Function to generate a database and select locked goods with a filter
func (g *Good) FilterAvailable(db *gorm.DB) *gorm.DB {
2017-05-19 13:37:01 +02:00
return db.Model(g).Where("locked_secret = '' OR locked_secret is NULL")
}
2017-05-15 10:22:24 +02:00
// Function to lock a good, so that it cannot be locked (bought) 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
2017-05-03 07:30:18 +02:00
// Function to check if a good is locked
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
func (g *Good) Unlock(secret string) error {
if g.LockedSecret == secret {
g.LockedSecret = ""
g.LockedAt = nil
return nil
}
return errors.New("wrong secret")
}
2017-05-03 07:30:18 +02:00
// Function to initialize the database
func init() {
database.AddModel(&Good{})
}