104 lines
2.9 KiB
Go
104 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
// File : holds information about a specific File
|
|
type File struct {
|
|
ID int `json:"id"`
|
|
Name string `json:"name"`
|
|
Location string `json:"location"`
|
|
Description string `json:"description"`
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
UpdatedAt time.Time `json:"updatedAt"`
|
|
}
|
|
|
|
// Files : a list of File items
|
|
type Files []File
|
|
|
|
const fileTableCreationQuery = `CREATE TABLE IF NOT EXISTS files (
|
|
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
|
name varchar(255),
|
|
location varchar(255),
|
|
description varchar(255),
|
|
createdAt datetime,
|
|
updatedAt datetime,
|
|
PRIMARY KEY (id)
|
|
);`
|
|
|
|
func ensureFileTableExists(db *sql.DB) {
|
|
if _, err := db.Exec(fileTableCreationQuery); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// getFile : used during GET command
|
|
func (f *File) getFile(db *sql.DB) error {
|
|
sqlQuery := `SELECT name, location, description, createdAt, updatedAt FROM files WHERE id=?`
|
|
return db.QueryRow(sqlQuery, f.ID).Scan(&f.Name, &f.Location, &f.Description, &f.CreatedAt, &f.UpdatedAt)
|
|
}
|
|
|
|
// updateFile : used during PUT command
|
|
func (f *File) updateFile(db *sql.DB) error {
|
|
updStmt, updErr := db.Prepare("UPDATE files SET name=?, location=?, 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(f.Name, f.Location, f.Description, time.Now(), f.ID)
|
|
return err
|
|
}
|
|
|
|
// deleteFile : used during DELETE command
|
|
func (f *File) deleteFile(db *sql.DB) error {
|
|
_, err := db.Exec("DELETE FROM files WHERE id=?", f.ID)
|
|
return err
|
|
}
|
|
|
|
// createFile : used during PUSH command
|
|
func (f *File) createFile(db *sql.DB) error {
|
|
stmtIns, insErr := db.Prepare("INSERT INTO files (name, location, 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(f.Name, f.Location, f.Description, time.Now(), time.Now())
|
|
return err
|
|
}
|
|
|
|
// getFiles : used during GET command for all
|
|
func getFiles(db *sql.DB, start, count int) (Files, error) {
|
|
sqlQuery := `SELECT id, name, location, description, createdAt, updatedAt FROM files LIMIT ? OFFSET ?;`
|
|
|
|
getStmt, prepErr := db.Prepare(sqlQuery)
|
|
if prepErr != nil {
|
|
panic(prepErr.Error()) // proper error handling instead of panic in your app
|
|
}
|
|
defer getStmt.Close() // Close the statement when we leave main() / the program terminates
|
|
|
|
rows, err := getStmt.Query(count, start)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
files := Files{}
|
|
|
|
for rows.Next() {
|
|
var f File
|
|
if err := rows.Scan(&f.ID, &f.Name, &f.Location, &f.Description, &f.CreatedAt, &f.UpdatedAt); err != nil {
|
|
return nil, err
|
|
}
|
|
files = append(files, f)
|
|
}
|
|
|
|
return files, nil
|
|
}
|