diff options
Diffstat (limited to 'database/minecraft.go')
| -rw-r--r-- | database/minecraft.go | 28 |
1 files changed, 28 insertions, 0 deletions
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 +} |
