Write docs!

This commit is contained in:
nemunaire 2018-03-09 19:07:08 +01:00
commit bcc598ebd5
37 changed files with 478 additions and 188 deletions

View file

@ -4,6 +4,7 @@ import (
"time"
)
// Event represents a challenge event.
type Event struct {
Id int64 `json:"id"`
Kind string `json:"kind"`
@ -11,6 +12,7 @@ type Event struct {
Time time.Time `json:"time"`
}
// GetLastEvents returns the list of the last 10 events, sorted by date, last first
func GetLastEvents() ([]Event, error) {
if rows, err := DBQuery("SELECT id_event, txt, kind, time FROM events ORDER BY time DESC LIMIT 10"); err != nil {
return nil, err
@ -33,6 +35,7 @@ func GetLastEvents() ([]Event, error) {
}
}
// GetEvents returns the list of all events, sorted by date, last first
func GetEvents() ([]Event, error) {
if rows, err := DBQuery("SELECT id_event, txt, kind, time FROM events ORDER BY time DESC"); err != nil {
return nil, err
@ -55,6 +58,7 @@ func GetEvents() ([]Event, error) {
}
}
// GetEvent retrieves the event with the given id
func GetEvent(id int) (Event, error) {
var e Event
if err := DBQueryRow("SELECT id_event, txt, kind, time FROM events WHERE id_event=?", id).Scan(&e.Id, &e.Text, &e.Kind, &e.Time); err != nil {
@ -64,6 +68,7 @@ func GetEvent(id int) (Event, error) {
return e, nil
}
// NewEvent creates a new event in the database and returns the corresponding structure
func NewEvent(txt string, kind string) (Event, error) {
if res, err := DBExec("INSERT INTO events (txt, kind, time) VALUES (?, ?, ?)", txt, kind, time.Now()); err != nil {
return Event{}, err
@ -74,6 +79,7 @@ func NewEvent(txt string, kind string) (Event, error) {
}
}
// Update applies modifications back to the database
func (e Event) Update() (int64, error) {
if res, err := DBExec("UPDATE events SET txt = ?, kind = ?, time = ? WHERE id_event = ?", e.Text, e.Kind, e.Time, e.Id); err != nil {
return 0, err
@ -84,6 +90,7 @@ func (e Event) Update() (int64, error) {
}
}
// Delete the event from the database
func (e Event) Delete() (int64, error) {
if res, err := DBExec("DELETE FROM events WHERE id_event = ?", e.Id); err != nil {
return 0, err
@ -94,6 +101,7 @@ func (e Event) Delete() (int64, error) {
}
}
// ClearEvents removes all events from database
func ClearEvents() (int64, error) {
if res, err := DBExec("DELETE FROM events"); err != nil {
return 0, err