// NGnius 2020-02-12 package main import ( "encoding/json" "strconv" ) type Jsonable interface { Json() ([]byte, error) JsonPretty() ([]byte, error) Unjson([]byte) error JsonObject() (interface{}, error) } type Rower interface { Intake() []interface{} Output() []interface{} } type Board struct { ID int64 Name string Description string } func LoadBoard(id int64) *Board { b := &Board{ID: id} b.Load() return b } func (b *Board) Load() error { return db.QueryRow("SELECT * FROM Boards WHERE id=?", b.ID).Scan(b.Intake()...) } func (b *Board) Commit() error { tx, _ := db.Begin() statement, _ := tx.Prepare("INSERT OR REPLACE INTO Boards(id, name, description) VALUES (?, ?, ?);") _, err := statement.Exec(b.Output()...) if err != nil { tx.Rollback() return err } return tx.Commit() } func (b *Board) Entries() ([]*Entry, error) { var entries []*Entry rows, err := db.Query("SELECT * FROM Entries WHERE board=?", b.ID) if err != nil { return entries, err } count := 0 for rows.Next() { entries = append(entries, &Entry{}) rows.Scan(entries[count].Intake()...) count++ } return entries, nil } func (b *Board) SomeEntries(start, end int64) ([]*Entry, error) { var entries []*Entry rows, err := db.Query("SELECT * FROM Entries WHERE board=? and rank >= ? and rank <= ? ORDER BY rank ASC;", b.ID, start, end) if err != nil { return entries, err } count := 0 for rows.Next() { entries = append(entries, &Entry{}) rows.Scan(entries[count].Intake()...) count++ } return entries, nil } func (b *Board) Intake() []interface{} { return []interface{}{&b.ID, &b.Name, &b.Description} } func (b *Board) Output() []interface{} { return []interface{}{b.ID, b.Name, b.Description} } type Player struct { ID int64 Name string } func LoadPlayer(id int64) *Player { p := &Player{ID: id} p.Load() return p } func (p *Player) Load() error { return db.QueryRow("SELECT * FROM Players WHERE id=? LIMIT 1;", p.ID).Scan(p.Intake()...) } func (p *Player) Commit() error { tx, _ := db.Begin() statement, _ := tx.Prepare("INSERT OR REPLACE INTO Players(id, name) VALUES (?, ?);") _, err := statement.Exec(p.Output()...) if err != nil { tx.Rollback() return err } return tx.Commit() } func (p *Player) Entries() ([]*Entry, error) { var entries []*Entry rows, err := db.Query("SELECT * FROM Entries WHERE player=?", p.ID) if err != nil { return entries, err } count := 0 for rows.Next() { entries = append(entries, &Entry{}) rows.Scan(entries[count].Intake()...) count++ } return entries, nil } func (p *Player) SomeEntries(limit int64) ([]*Entry, error) { var entries []*Entry rows, err := db.Query("SELECT * FROM Entries WHERE player=? ORDER BY rank ASC LIMIT ?;", p.ID, limit) if err != nil { return entries, err } count := 0 for rows.Next() { entries = append(entries, &Entry{}) rows.Scan(entries[count].Intake()...) count++ } return entries, nil } func (p *Player) Url() string { return "/player/" + strconv.Itoa(int(p.ID)) } // implementation of Rower func (p *Player) Intake() []interface{} { return []interface{}{&p.ID, &p.Name} } func (p *Player) Output() []interface{} { return []interface{}{p.ID, p.Name} } type Entry struct { ID int64 Rank int64 Score int64 Player int64 Board int64 } func LoadEntry(id int64) *Entry { e := &Entry{ID: id} e.Load() return e } func (e *Entry) Load() error { return db.QueryRow("SELECT * FROM Entries WHERE id=? LIMIT 1;", e.ID).Scan(e.Intake()...) } func (e *Entry) Commit() error { tx, _ := db.Begin() statement, _ := tx.Prepare("INSERT OR REPLACE INTO Entries(id, rank, score, player, board) VALUES (?, ?, ?, ?, ?);") _, err := statement.Exec(e.Output()...) if err != nil { tx.Rollback() return err } return tx.Commit() } // implementation of Jsonable func (e *Entry) Json() ([]byte, error) { var data []byte jsonObj, err := e.JsonObject() if err != nil { return data, err } return json.Marshal(jsonObj) } func (e *Entry) JsonPretty() ([]byte, error) { var data []byte jsonObj, err := e.JsonObject() if err != nil { return data, err } return json.MarshalIndent(jsonObj, "", " ") } func (e *Entry) JsonObject() (interface{}, error) { jsonObj := EntryJSON{ID: e.ID, Rank: e.Rank, Score: e.Score} ePlayer := &Player{ID: e.Player} err := ePlayer.Load() if err != nil { return jsonObj, err } jsonObj.PlayerName = ePlayer.Name jsonObj.PlayerURL = ePlayer.Url() return jsonObj, nil } // implementation of Rower func (e *Entry) Intake() []interface{} { return []interface{}{&e.ID, &e.Rank, &e.Score, &e.Player, &e.Board} } func (e *Entry) Output() []interface{} { return []interface{}{e.ID, e.Rank, e.Score, e.Player, e.Board} }