From fb653292612eddca5b088ed88466c546d1597107 Mon Sep 17 00:00:00 2001 From: Elizabeth Alexander Hunt Date: Sat, 20 Jun 2026 09:40:07 -0700 Subject: Initial commit --- database/minecraft.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 database/minecraft.go (limited to 'database/minecraft.go') diff --git a/database/minecraft.go b/database/minecraft.go new file mode 100644 index 0000000..d12b4b8 --- /dev/null +++ b/database/minecraft.go @@ -0,0 +1,28 @@ +package database + +import ( + "database/sql" + "errors" + "time" + + _ "github.com/mattn/go-sqlite3" +) + +func SaveMinecraftStatus(db *sql.DB, status string) error { + _, err := db.Exec(`INSERT INTO minecraft_status (id, status, updated_at) VALUES (1, ?, ?) + ON CONFLICT(id) DO UPDATE SET status = excluded.status, updated_at = excluded.updated_at`, + status, time.Now()) + return err +} + +// GetMinecraftStatus returns the cached status JSON and when it was written. An +// empty string (with no error) means nothing has been cached yet. +func GetMinecraftStatus(db *sql.DB) (string, time.Time, error) { + var status string + var updatedAt time.Time + err := db.QueryRow("SELECT status, updated_at FROM minecraft_status WHERE id = 1").Scan(&status, &updatedAt) + if errors.Is(err, sql.ErrNoRows) { + return "", time.Time{}, nil + } + return status, updatedAt, err +} -- cgit v1.3