genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0

[Task]: Comment \models

This commit is contained in:
mlabusch 2017-05-03 07:30:18 +02:00
parent 591695c0b6
commit 1259f9cfe3
8 changed files with 34 additions and 23 deletions

View File

@ -1,3 +1,4 @@
// Package with the mostly static content (models) of this microservice
package models package models
import ( import (
@ -9,9 +10,9 @@ import (
"github.com/genofire/hs_master-kss-monolith/lib/log" "github.com/genofire/hs_master-kss-monolith/lib/log"
) )
//config file of this daemon (for more the config_example.conf in git repository) // Config file for this daemon (mor information at the config_example.conf in this git repository)
type Config struct { type Config struct {
// address on which the api and static content webserver runs // address under which the api and static content of the webserver runs
WebserverBind string `toml:"webserver_bind"` WebserverBind string `toml:"webserver_bind"`
// path to deliver static content // path to deliver static content
@ -21,33 +22,33 @@ type Config struct {
GoodRelease GoodReleaseConfig `toml:"good_release"` GoodRelease GoodReleaseConfig `toml:"good_release"`
CacheClean CacheWorkerConfig `toml:"cache_clean"` CacheClean CacheWorkerConfig `toml:"cache_clean"`
// path to the svg image templaes to show availablity of a given good // path to the svg image templaes to show availablity of a given good with a traffic light food labeling system
GoodAvailablityTemplate string `toml:"good_availablity_template"` GoodAvailablityTemplate string `toml:"good_availablity_template"`
// URLs to other microservices // URLs to other microservices that this services uses
MicroserviceDependencies struct { MicroserviceDependencies struct {
Product string `toml:"product"` Product string `toml:"product"`
Permission string `toml:"permission"` Permission string `toml:"permission"`
} `toml:"microservice_dependencies"` } `toml:"microservice_dependencies"`
} }
//config of worker to clean caches from values of other microservice // Configuration of the Worker to clean the cache from values of other microservice
type CacheWorkerConfig struct { type CacheWorkerConfig struct {
// run worker every ... // Run Worker every Duration
Every Duration Every Duration
// remove cache which is not used since .. // Remove cache, which is not used since Duration
After Duration After Duration
} }
//config of worker to release looked goods after a time // Configuration of the Worker to release locked goods after a time period
type GoodReleaseConfig struct { type GoodReleaseConfig struct {
// run worker every ... // Run worker every Duration
Every Duration `toml:"every"` Every Duration `toml:"every"`
// unlock which is not used since .. // unlock which is not used since Duration
After Duration `toml:"after"` After Duration `toml:"after"`
} }
//reads a config model from path of a yml file // Function that reads a config model from a given path of a yml file
func ReadConfigFile(path string) *Config { func ReadConfigFile(path string) *Config {
config := &Config{} config := &Config{}
file, err := ioutil.ReadFile(path) file, err := ioutil.ReadFile(path)

View File

@ -1,3 +1,4 @@
// Package with the mostly static content (models) of this microservice
package models package models
import ( import (
@ -6,6 +7,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
// Function to test the configuration of the microservice
func TestReadConfig(t *testing.T) { func TestReadConfig(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)

View File

@ -1,3 +1,4 @@
// Package with the mostly static content (models) of this microservice
package models package models
import ( import (
@ -7,15 +8,13 @@ import (
) )
// Duration is a TOML datatype // Duration is a TOML datatype
// A duration string is a possibly signed sequence of // A duration string is a possibly signed sequence of decimal numbers and a unit suffix,
// decimal numbers and a unit suffix, // such as "300s", "1.5h" or "5d". Valid time units are "s", "m", "h", "d", "w".
// such as "300s", "1.5h" or "5d".
// Valid time units are "s", "m", "h", "d", "w".
type Duration struct { type Duration struct {
time.Duration time.Duration
} }
// UnmarshalTOML parses a duration string. // Function UnmarshalTOML parses a duration string
func (d *Duration) UnmarshalTOML(dataInterface interface{}) error { func (d *Duration) UnmarshalTOML(dataInterface interface{}) error {
var data string var data string
switch dataInterface.(type) { switch dataInterface.(type) {

View File

@ -1,3 +1,4 @@
// Package with the mostly static content (models) of this microservice
package models package models
import ( import (
@ -7,6 +8,8 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
// Function to test UnmarshalTOML()
func TestDuration(t *testing.T) { func TestDuration(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)

View File

@ -1,3 +1,4 @@
// Package with the mostly static content (models) of this microservice
package models package models
import ( import (
@ -9,7 +10,7 @@ import (
"github.com/genofire/hs_master-kss-monolith/lib/database" "github.com/genofire/hs_master-kss-monolith/lib/database"
) )
// this stock microservice manage goods // Goods managemd in this stock microservice
type Good struct { type Good struct {
ID int64 ID int64
ProductID int64 ProductID int64
@ -26,24 +27,24 @@ type Good struct {
Sended bool Sended bool
} }
// generate database select which filtered locked goods // Function to enerate a database and select locked goods with a filter
func (g *Good) FilterAvailable(db *gorm.DB) *gorm.DB { func (g *Good) FilterAvailable(db *gorm.DB) *gorm.DB {
return db.Model(g).Where("locked_secret == '' OR locked_secret is NULL") return db.Model(g).Where("locked_secret == '' OR locked_secret is NULL")
} }
// lock the good, on a way, that it could not be used by other users // Function to lock a good, so that it cannot be locked or bought by other users
func (g *Good) Lock(secret string) { func (g *Good) Lock(secret string) {
now := time.Now() now := time.Now()
g.LockedSecret = secret g.LockedSecret = secret
g.LockedAt = &now g.LockedAt = &now
} }
// is this good locked? // Function to check if a good is locked
func (g *Good) IsLock() bool { func (g *Good) IsLock() bool {
return len(g.LockedSecret) > 0 return len(g.LockedSecret) > 0
} }
// unlock the good, that it could be usered again // Function to unlock a good
func (g *Good) Unlock(secret string) error { func (g *Good) Unlock(secret string) error {
if g.LockedSecret == secret { if g.LockedSecret == secret {
g.LockedSecret = "" g.LockedSecret = ""
@ -53,6 +54,7 @@ func (g *Good) Unlock(secret string) error {
return errors.New("wrong secret") return errors.New("wrong secret")
} }
// Function to initialize the database
func init() { func init() {
database.AddModel(&Good{}) database.AddModel(&Good{})
} }

View File

@ -1,3 +1,4 @@
// Package with the mostly static content (models) of this microservice
package models package models
import ( import (
@ -7,6 +8,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
// Function to test the locking and unlocking of a good
func TestGood(t *testing.T) { func TestGood(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)

View File

@ -1,2 +0,0 @@
// a package to store all the struct
package models

4
models/structstorage.go Normal file
View File

@ -0,0 +1,4 @@
// Package with the mostly static content (models) of this microservice
package models
// Store all the structs