Initial Commit

This commit is contained in:
Patrick McDonagh
2017-09-27 11:18:17 -05:00
commit 62ed942495
16 changed files with 922 additions and 0 deletions

78
model_config.go Normal file
View File

@@ -0,0 +1,78 @@
package main
import (
"database/sql"
"time"
)
// Config : holds information about a specific Config
type Config struct {
ID int `json:"id"`
Parameter string `json:"parameter"`
Val string `json:"val"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
// Configs : a list of Config items
type Configs []Config
// getConfig : used during GET command
func (c *Config) getConfig(db *sql.DB) error {
return db.QueryRow("SELECT parameter, val, createdAt, updatedAt FROM configs WHERE id=?", c.ID).Scan(&c.Parameter, &c.Val, &c.CreatedAt, &c.UpdatedAt)
}
// updateConfig : used during PUT command
func (c *Config) updateConfig(db *sql.DB) error {
updStmt, updErr := db.Prepare("UPDATE configs SET parameter=?, val=?, updatedAt=? WHERE id=?")
if updErr != nil {
panic(updErr.Error()) // proper error handling instead of panic in your app
}
defer updStmt.Close() // Close the statement when we leave main() / the program terminates
_, err := updStmt.Exec(c.Parameter, c.Val, time.Now(), c.ID)
return err
}
// deleteConfig : used during DELETE command
func (c *Config) deleteConfig(db *sql.DB) error {
_, err := db.Exec("DELETE FROM configs WHERE id=?", c.ID)
return err
}
// createConfig : used during PUSH command
func (c *Config) createConfig(db *sql.DB) error {
stmtIns, insErr := db.Prepare("INSERT INTO configs (parameter, val, createdAt, updatedAt) VALUES (?, ?, ?, ?);")
if insErr != nil {
panic(insErr.Error()) // proper error handling instead of panic in your app
}
defer stmtIns.Close() // Close the statement when we leave main() / the program terminates
_, err := stmtIns.Exec(c.Parameter, c.Val, time.Now(), time.Now())
return err
}
// getConfigs : used during GET command for all
func getConfigs(db *sql.DB, start, count int) (Configs, error) {
rows, err := db.Query(
"SELECT id, parameter, val, createdAt, updatedAt FROM configs LIMIT ? OFFSET ?",
count, start)
if err != nil {
return nil, err
}
defer rows.Close()
configs := Configs{}
for rows.Next() {
var c Config
if err := rows.Scan(&c.ID, &c.Parameter, &c.CreatedAt, &c.UpdatedAt); err != nil {
return nil, err
}
configs = append(configs, c)
}
return configs, nil
}