km

package
v0.0.0-...-992216a Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2014 License: BSD-3-Clause Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// NotFound standard 404 not found
	NotFound = newResponse("^404 page not found\n$", 404)
	// Ok 200 ok
	Ok = newResponse("ok\n", 200)
	// UnknownField 400 an unknown field encountered in supplied data
	UnknownField = newResponse("invalid fieldname\n", 400)
	// NotParsable 400 could not parse request
	NotParsable = newResponse("could not parse request\n", 400)
	// InvalidDate coudl not parse the date provided
	InvalidDate = newResponse("invalid date\n", 400)
	// InvalidURL 400 invalid url, correct structure, but invalid
	InvalidURL = newResponse("invalid url", 400)
	// DbError error connecting to database
	DbError = newResponse("database eror", 500)
)

Functions

func ParseURLDate

func ParseURLDate(dateStr string) (err error, date time.Time)

ParseURLDate parse a datestring used in the url to a time.Time object

func SaveKilometers

func SaveKilometers(dbmap *gorp.DbMap, date time.Time, fields []Field) (err error)

SaveKilometers saves a the given Field array (wich is supplied by the user) if no data is saved for today it results in an insert, otherwise a update of the already saved data is done

func SaveTimes

func SaveTimes(dbmap *gorp.DbMap, date time.Time, fields []Field) (err error)

SaveTimes saves a given fields array to the db backend

Types

type Config

type Config struct {
	Env  string
	Log  string
	Port int
	Db   string
}

Config is configuration for the app, parsed from config file

type Field

type Field struct {
	Km   int
	Time string
	Name string
}

Field holds the data for 1 row in the ui form

func ParseJSONBody

func ParseJSONBody(bodyReader io.Reader) (err error, fields []Field)

ParseJSONBody parse the posted data into a Field array

type GetTimesInterface

type GetTimesInterface func(dbmap *gorp.DbMap, year, month int64) (rows []TimeRow, err error)

GetTimesInterface is the interface to swap out the GetTimes function when testing

type Kilometers

type Kilometers struct {
	ID                            int64 `db:"Id"`
	Date                          time.Time
	Begin, Eerste, Laatste, Terug int
	Comment                       string
}

Kilometers is the struct representing a db row in the kilometers table

func (*Kilometers) AddFields

func (k *Kilometers) AddFields(fields []Field)

AddFields updates an existing Kilometers truct to update/insert into db with data posted by user

type Response

type Response struct {
	Code  int
	Regex *regexp.Regexp
	Extra string
}

Response a custum error response that gives some more ditails on the error

func CustomResponse

func CustomResponse(r Response, err error) Response

CustomResponse takes a error and adds extra fields to convert it to a custom Response object

func (Response) Error

func (r Response) Error() string

Error implements the error interface

func (Response) String

func (r Response) String() string

String impletent the Stringer interface for easy printing

type SaveInterface

type SaveInterface func(dbmap *gorp.DbMap, date time.Time, fields []Field) (err error)

SaveInterface is the interface to swap out the Save function when testing

type Server

type Server struct {
	mux.Router
	Dbmap *gorp.DbMap

	StateFunc StateGetter
	SaveKilos SaveInterface
	SaveTimes SaveInterface
	GetTimes  GetTimesInterface
	// contains filtered or unexported fields
}

Server is the main type of this package it holds all the data required to run the app, the database connection, the webserver routes to handle, and the templates to parse

func NewServer

func NewServer(dbName string, config Config) (s *Server, err error)

NewServer creates a new server object with a given name and with a specific configuration

type State

type State struct {
	Fields       []Field
	LastDayError string
	LastDayKm    int
}

State represents the stucture of the data to fill the form

func GetState

func GetState(dbmap *gorp.DbMap, dateStr string) (err error, state State)

GetState returns the data already saved in the databse to fill the form with

type StateGetter

type StateGetter func(dbmap *gorp.DbMap, dateStr string) (err error, state State)

StateGetter is the interface to swap out the GetState function when testing

type TimeRow

type TimeRow struct {
	ID                                int64
	Date                              time.Time
	Begin, CheckIn, CheckOut, Laatste string
	Hours                             float64
}

TimeRow is a Times row converted to the format displayed in the frontend

func GetAllTimes

func GetAllTimes(dbmap *gorp.DbMap, year, month int64) (rows []TimeRow, err error)

GetAllTimes pulls all rows for a given month from the db and converts it all to TimeRow for displaying in the frontend

func NewTimeRow

func NewTimeRow() TimeRow

NewTimeRow creates new initialized TimeRow

type Times

type Times struct {
	ID                                int64 `db:"Id"`
	Date                              time.Time
	Begin, CheckIn, CheckOut, Laatste int64
}

Times represents a db row in the times table

func (*Times) UpdateObject

func (t *Times) UpdateObject(date string, fields []Field) error

UpdateObject updates the times struct with posted data coming from the user this struct can used to update the current state in the db

Jump to

Keyboard shortcuts

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