Documentation ¶
Index ¶
- Constants
- Variables
- func InitLogger(w io.Writer, level string, format string, prettyJson bool)
- func LogInboundRequestMw(next http.Handler) http.Handler
- type DBServer
- type DatabaseVer
- type Effect
- type Effects
- type Flavor
- type Flavors
- type Server
- func (s *Server) CreateStrainHandler(w http.ResponseWriter, r *http.Request)
- func (s *Server) ListenAndServe() error
- func (s *Server) StrainByEffectHandler(w http.ResponseWriter, r *http.Request)
- func (s *Server) StrainByFlavorHandler(w http.ResponseWriter, r *http.Request)
- func (s *Server) StrainByIDHandler(w http.ResponseWriter, r *http.Request)
- func (s *Server) StrainByNameHandler(w http.ResponseWriter, r *http.Request)
- func (s *Server) StrainByRaceHandler(w http.ResponseWriter, r *http.Request)
- type Strain
- func (s *Strain) CreateInDB() error
- func (s *Strain) EffectsFromDBByRefID(id uint) (Effects, error)
- func (s *Strain) FlavorsFromDBByRefID(id uint) (Flavors, error)
- func (s *Strain) FromDBByName(name string) error
- func (s *Strain) FromDBByRefID(id uint) error
- func (s *Strain) ToStrainRepr() StrainRepr
- type StrainRepr
- type StrainReprs
- type Strains
Constants ¶
const DBConnectOptions = "charset=utf8&parseTime=True&loc=Local"
Variables ¶
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") )
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") )
var ErrStrainIdMustBeInteger = errors.New("strain ID must be an integer")
Functions ¶
func InitLogger ¶
InitLogger sets values on the global logger for use everywhere else in the application.
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
ReplaceInDB creates the entry in the database. An error is returned if the create fails, or if the record already exists.
func (*Strain) EffectsFromDBByRefID ¶
EffectsFromDBByRefID gets all associated effects from the database by searching on the strain id.
func (*Strain) FlavorsFromDBByRefID ¶
FlavorsFromDB gets all associated flavors from the database by searching on the strain id.
func (*Strain) FromDBByName ¶
FromDBByName populates the struct with details from the database by searching on the strain name.
func (*Strain) FromDBByRefID ¶
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 ¶
func (*Strains) FromDBByEffect ¶
FromDBByEffect populates the struct with all strains from the database by searching on strain effect name and category.
func (*Strains) FromDBByFlavor ¶
FromDBByFlavor populates the struct with all strains from the database by searching on strain flavor names.
func (*Strains) FromDBByRace ¶
FromDBByRace populates the struct with all strains from the database by searching on strain race.
func (*Strains) ToStrainRepr ¶
func (s *Strains) ToStrainRepr() StrainReprs