// NGnius 2020-02-12 package main import ( "encoding/json" "strconv" // test "fmt" ) 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} loadErr := b.Load() if loadErr != nil { return nil } 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 } // implementation of Jsonable func (b *Board) Json() ([]byte, error) { var data []byte jsonObj, err := b.JsonObject() if err != nil { return data, err } return json.Marshal(jsonObj) } func (b *Board) JsonPretty() ([]byte, error) { var data []byte jsonObj, err := b.JsonObject() if err != nil { return data, err } return json.MarshalIndent(jsonObj, "", " ") } func (b *Board) JsonObject() (interface{}, error) { jsonObj := BoardJSON{ID: b.ID, Name: b.Name, Description: b.Description} return jsonObj, nil } // implementation of Rower 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} loadErr := p.Load() if loadErr != nil { return nil } 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 time DESC LIMIT ?;", p.ID, limit) if err != nil { return entries, err } count := 0 for rows.Next() { entries = append(entries, &Entry{}) //rows.Scan(entries[count].Intake()...) scanErr := rows.Scan(&entries[count].ID, &entries[count].Rank, &entries[count].Score, &entries[count].Player, &entries[count].Board, &entries[count].Time) if scanErr != nil { fmt.Println(scanErr) } count++ } return entries, nil } func (p *Player) Url() string { return "/player?id=" + strconv.Itoa(int(p.ID)) } // implementation of Jsonable func (p *Player) Json() ([]byte, error) { var data []byte jsonObj, err := p.JsonObject() if err != nil { return data, err } return json.Marshal(jsonObj) } func (p *Player) JsonPretty() ([]byte, error) { var data []byte jsonObj, err := p.JsonObject() if err != nil { return data, err } return json.MarshalIndent(jsonObj, "", " ") } func (p *Player) JsonObject() (interface{}, error) { jsonObj := PlayerJSON{ID: p.ID, Name: p.Name} return jsonObj, nil } // 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 Time int64 // Created time (seconds since Unix epoch) } func LoadEntry(id int64) *Entry { e := &Entry{ID: id} loadErr := e.Load() if loadErr != nil { return nil } 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, time) 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, PlayerID: e.Player, BoardID: e.Board} 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, &e.Time} } func (e *Entry) Output() []interface{} { return []interface{}{e.ID, e.Rank, e.Score, e.Player, e.Board, e.Time} }