110 lines
3.2 KiB
Go
110 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
// TagClass : holds information about a specific TagClass
|
|
type TagClass struct {
|
|
ID int `json:"id"`
|
|
ClassType string `json:"classType"`
|
|
Description string `json:"description"`
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
UpdatedAt time.Time `json:"updatedAt"`
|
|
}
|
|
|
|
// TagClasses : a list of TagClass items
|
|
type TagClasses []TagClass
|
|
|
|
const tagClassTableCreationQuery = `CREATE TABLE IF NOT EXISTS tagClasses (
|
|
id int(10) unsigned AUTO_INCREMENT,
|
|
classType varchar(255),
|
|
description varchar(255),
|
|
createdAt datetime,
|
|
updatedAt datetime,
|
|
UNIQUE KEY classType (classType),
|
|
PRIMARY KEY (id)
|
|
)`
|
|
|
|
func ensureTagClassTableExists(db *sql.DB) {
|
|
if _, err := db.Exec(tagClassTableCreationQuery); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func seedTagClassData(db *sql.DB) {
|
|
sqlQuery := `INSERT INTO tagClasses VALUES
|
|
(1,'normal','Historical Data','2016-10-13 15:05:32','2016-10-13 15:05:32'),
|
|
(2,'handshake','PLC Handshake','2016-10-13 15:05:32','2016-10-13 15:05:32');`
|
|
insStmt, insErr := db.Prepare(sqlQuery)
|
|
|
|
if insErr != nil {
|
|
panic(insErr.Error()) // proper error handling instead of panic in your app
|
|
}
|
|
defer insStmt.Close() // Close the statement when we leave main() / the program terminates
|
|
|
|
insStmt.Exec()
|
|
}
|
|
|
|
// getTagClass : used during GET command
|
|
func (c *TagClass) getTagClass(db *sql.DB) error {
|
|
return db.QueryRow("SELECT classType, description, createdAt, updatedAt FROM tagClasses WHERE id=?", c.ID).Scan(&c.ClassType, &c.Description, &c.CreatedAt, &c.UpdatedAt)
|
|
}
|
|
|
|
// updateTagClass : used during PUT command
|
|
func (c *TagClass) updateTagClass(db *sql.DB) error {
|
|
updStmt, updErr := db.Prepare("UPDATE tagClasses SET classType=?, description=?, 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.ClassType, c.Description, time.Now(), c.ID)
|
|
return err
|
|
}
|
|
|
|
// deleteTagClass : used during DELETE command
|
|
func (c *TagClass) deleteTagClass(db *sql.DB) error {
|
|
_, err := db.Exec("DELETE FROM tagClasses WHERE id=?", c.ID)
|
|
return err
|
|
}
|
|
|
|
// createTagClass : used during PUSH command
|
|
func (c *TagClass) createTagClass(db *sql.DB) error {
|
|
stmtIns, insErr := db.Prepare("INSERT INTO tagClasses (classType, description, 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.ClassType, c.Description, time.Now(), time.Now())
|
|
return err
|
|
}
|
|
|
|
// getTagClasses : used during GET command for all
|
|
func getTagClasses(db *sql.DB, start, count int) (TagClasses, error) {
|
|
rows, err := db.Query(
|
|
"SELECT id, classType, description, createdAt, updatedAt FROM tagClasses LIMIT ? OFFSET ?",
|
|
count, start)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
tagClasses := TagClasses{}
|
|
|
|
for rows.Next() {
|
|
var c TagClass
|
|
if err := rows.Scan(&c.ID, &c.ClassType, &c.Description, &c.CreatedAt, &c.UpdatedAt); err != nil {
|
|
return nil, err
|
|
}
|
|
tagClasses = append(tagClasses, c)
|
|
}
|
|
|
|
return tagClasses, nil
|
|
}
|