tms

package
v0.0.0-...-1b3344a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 27, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const DBConnectOptions = "charset=utf8&parseTime=True&loc=Local"

Variables

View Source
var (
	ErrDatabaseNameNotSet     = errors.New("database name was not set")
	ErrDatabaseUsernameNotSet = errors.New("database username was not set")
	ErrDatabaseVersionNewer   = errors.New("the actual database version is newer than the desired migration version")
)
View Source
var (
	ErrRecordAlreadyExists   = errors.New("the record already exists")
	ErrNotExists             = errors.New("the record does not exist")
	ErrDatabaseConnectionNil = errors.New("the given database connection is not connected")
	ErrReferenceIDNotSet     = errors.New("the reference ID must be set for this operation")
)
View Source
var ErrStrainIdMustBeInteger = errors.New("strain ID must be an integer")

Functions

func InitLogger

func InitLogger(w io.Writer, level string, format string, prettyJson bool)

InitLogger sets values on the global logger for use everywhere else in the application.

func LogInboundRequestMw

func LogInboundRequestMw(next http.Handler) http.Handler

Types

type DBServer

type DBServer struct {
	// Name of the logical database on the server.
	Name string
	// DBIteration tracks the current iteration of the database schema.  Migrate() must be called on the
	// database to ensure the current schema is up to date with DBIteration.
	DBIteration uint
	Username    string
	Password    string

	// DB is the database connection through which all transactions are brokered.
	DB *gorm.DB
	// contains filtered or unexported fields
}

DBServer is the database server where records will be stored and queried.

func NewDBServer

func NewDBServer(name, username, password string) *DBServer

func (*DBServer) Close

func (srv *DBServer) Close() error

Close terminates the connection with the database.

func (*DBServer) Migrate

func (srv *DBServer) Migrate() error

Migrate will migrate the database to ensure the current version of the schema.

func (*DBServer) Open

func (srv *DBServer) Open() error

Open establishes a connection to the database.

type DatabaseVer

type DatabaseVer struct {
	CreatedAt time.Time  `json:"-"`
	UpdatedAt time.Time  `json:"-"`
	DeletedAt *time.Time `json:"-"`
	VersionID uint       `gorm:"primary_key;auto_increment" json:"-"`
	// Iteration holds the current iteration of the database schema.
	Iteration uint `gorm:"unique;not null"`
}

DatabaseVer tracks the database schema version information.

type Effect

type Effect struct {
	CreatedAt time.Time  `json:"-"`
	UpdatedAt time.Time  `json:"-"`
	DeletedAt *time.Time `json:"-"`
	EffectID  uint       `gorm:"primary_key;auto_increment" json:"-"`
	Name      string     `gorm:"not null"`
	Category  string     `gorm:"not null"`
}

Effect is the observed effects of the Strain on the user, and is used to directly model the database schema.

type Effects

type Effects []Effect

func (*Effects) Difference

func (e *Effects) Difference(effects Effects) Effects

Difference returns source Effects minus effects. This comparison is done based on the effect name and category only.

type Flavor

type Flavor struct {
	CreatedAt time.Time  `json:"-"`
	UpdatedAt time.Time  `json:"-"`
	DeletedAt *time.Time `json:"-"`
	FlavorID  uint       `gorm:"primary_key;auto_increment" json:"-"`
	Name      string     `gorm:"not null"`
	// DB is the database instance
	DB *gorm.DB `gorm:"-" json:"-"`
}

Flavor is how the Strain will taste, and is used to directly model the database schema.

type Flavors

type Flavors []Flavor

func (*Flavors) Difference

func (f *Flavors) Difference(flavors Flavors) Flavors

Difference returns source Flavors minus flavors. This comparison is done based on the flavor name only.

type Server

type Server struct {
	// Port is the port where the server will listen.
	Port int32
	// DB is the database instance
	DB *gorm.DB
}

func (*Server) CreateStrainHandler

func (s *Server) CreateStrainHandler(w http.ResponseWriter, r *http.Request)

CreateStrainHandler handles API requests to write new strains.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServer starts the API server.

func (*Server) StrainByEffectHandler

func (s *Server) StrainByEffectHandler(w http.ResponseWriter, r *http.Request)

StrainByEffectHandler handles API requests for strains by Effect.

func (*Server) StrainByFlavorHandler

func (s *Server) StrainByFlavorHandler(w http.ResponseWriter, r *http.Request)

StrainByFlavorHandler handles API requests for strains by flavor.

func (*Server) StrainByIDHandler

func (s *Server) StrainByIDHandler(w http.ResponseWriter, r *http.Request)

StrainByIDHandler handles API requests for strains by the strain ID.

func (*Server) StrainByNameHandler

func (s *Server) StrainByNameHandler(w http.ResponseWriter, r *http.Request)

StrainByNameHandler handles API requests for strains by the strain name.

func (*Server) StrainByRaceHandler

func (s *Server) StrainByRaceHandler(w http.ResponseWriter, r *http.Request)

StrainByRaceHandler handles API requests for strains by race.

type Strain

type Strain struct {
	// CreatedAt is the record creation time, handled by gorm.
	CreatedAt time.Time `json:"-"`
	// CreatedAt is the record update time, handled by gorm.
	UpdatedAt time.Time `json:"-"`
	// DeletedAt is the record deletion time, when using soft deletes in gorm.
	DeletedAt *time.Time `json:"-"`
	// StrainID is the unique identifier of the record in the database, not to be confused with the ReferenceID. This
	// field will auto update.
	StrainID uint `gorm:"primary_key;auto_increment" json:"-"`
	// ReferenceID, distinct from StrainID, is the tracking identifier of the strain.  Specifically this can be
	// used as a reference identifier distinct from the context of the database.
	ReferenceID uint `gorm:"unique;not null"`
	// Name is the name of the strain.
	Name string `gorm:"not null"`
	// Race indicates the strain's genetic makeup.
	Race string
	// Flavors stores all flavors of the strain.
	Flavors []Flavor `gorm:"many2many:strain_flavors"`
	// Effects stores side effects and their category.
	Effects []Effect `gorm:"many2many:strain_effects"`

	// DB is the database instance
	DB *gorm.DB `gorm:"-" json:"-"`
}

Strain stores all information about each strain and associated traits, and is used to directly model the database schema.

func (*Strain) CreateInDB

func (s *Strain) CreateInDB() error

ReplaceInDB creates the entry in the database. An error is returned if the create fails, or if the record already exists.

func (*Strain) EffectsFromDBByRefID

func (s *Strain) EffectsFromDBByRefID(id uint) (Effects, error)

EffectsFromDBByRefID gets all associated effects from the database by searching on the strain id.

func (*Strain) FlavorsFromDBByRefID

func (s *Strain) FlavorsFromDBByRefID(id uint) (Flavors, error)

FlavorsFromDB gets all associated flavors from the database by searching on the strain id.

func (*Strain) FromDBByName

func (s *Strain) FromDBByName(name string) error

FromDBByName populates the struct with details from the database by searching on the strain name.

func (*Strain) FromDBByRefID

func (s *Strain) FromDBByRefID(id uint) error

FromDBByRefID populates the struct with details from the database by searching on the strain id.

func (*Strain) ToStrainRepr

func (s *Strain) ToStrainRepr() StrainRepr

type StrainRepr

type StrainRepr struct {
	Name    string   `json:"name"`
	ID      uint     `json:"id"`
	Race    string   `json:"race"`
	Flavors []string `json:"flavors"`
	Effects struct {
		Positive []string `json:"positive"`
		Negative []string `json:"negative"`
		Medical  []string `json:"medical"`
	} `json:"effects"`

	DB *gorm.DB `json:"-"`
}

StrainRepr is the representation of a strain from the JSON input format.

func ParseStrain

func ParseStrain(src io.Reader) (StrainRepr, error)

ParseStrain populates a StrainRepr from src.

func (*StrainRepr) CreateInDB

func (rs *StrainRepr) CreateInDB() error

CreateInDB will create the strain record in the database. An error is returned if the strain ID already exists.

func (*StrainRepr) ReplaceInDB

func (rs *StrainRepr) ReplaceInDB() error

ReplaceInDB will create or replace the strain record in the database.

func (*StrainRepr) Write

func (rs *StrainRepr) Write(w io.Writer)

type StrainReprs

type StrainReprs []StrainRepr

func ParseStrains

func ParseStrains(src io.Reader) (StrainReprs, error)

ParseStrains populates a StrainReprs from src.

func (*StrainReprs) ToJson

func (rs *StrainReprs) ToJson() ([]byte, error)

type Strains

type Strains struct {
	DB *gorm.DB
	// contains filtered or unexported fields
}

func (*Strains) FromDBByEffect

func (s *Strains) FromDBByEffect(effect string) error

FromDBByEffect populates the struct with all strains from the database by searching on strain effect name and category.

func (*Strains) FromDBByFlavor

func (s *Strains) FromDBByFlavor(flavor string) error

FromDBByFlavor populates the struct with all strains from the database by searching on strain flavor names.

func (*Strains) FromDBByRace

func (s *Strains) FromDBByRace(race string) error

FromDBByRace populates the struct with all strains from the database by searching on strain race.

func (*Strains) ToStrainRepr

func (s *Strains) ToStrainRepr() StrainReprs

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL