server/libfic/event.go

111 lines
2.8 KiB
Go
Raw Normal View History

2016-01-25 02:09:22 +00:00
package fic
import (
"time"
)
2018-03-09 18:07:08 +00:00
// Event represents a challenge event.
2016-01-25 02:09:22 +00:00
type Event struct {
Id int64 `json:"id"`
Kind string `json:"kind"`
Text string `json:"txt"`
Time time.Time `json:"time"`
}
2018-03-09 18:07:08 +00:00
// GetLastEvents returns the list of the last 10 events, sorted by date, last first
2016-12-26 02:44:04 +00:00
func GetLastEvents() ([]Event, error) {
if rows, err := DBQuery("SELECT id_event, txt, kind, time FROM events ORDER BY time DESC LIMIT 10"); err != nil {
2016-01-25 02:09:22 +00:00
return nil, err
} else {
defer rows.Close()
var events = make([]Event, 0)
for rows.Next() {
var e Event
if err := rows.Scan(&e.Id, &e.Text, &e.Kind, &e.Time); err != nil {
return nil, err
}
events = append(events, e)
}
if err := rows.Err(); err != nil {
return nil, err
}
return events, nil
}
}
2018-03-09 18:07:08 +00:00
// GetEvents returns the list of all events, sorted by date, last first
2021-11-22 14:35:07 +00:00
func GetEvents() ([]*Event, error) {
2016-12-26 02:44:04 +00:00
if rows, err := DBQuery("SELECT id_event, txt, kind, time FROM events ORDER BY time DESC"); err != nil {
return nil, err
} else {
defer rows.Close()
2021-11-22 14:35:07 +00:00
var events []*Event
2016-12-26 02:44:04 +00:00
for rows.Next() {
2021-11-22 14:35:07 +00:00
e := &Event{}
2016-12-26 02:44:04 +00:00
if err := rows.Scan(&e.Id, &e.Text, &e.Kind, &e.Time); err != nil {
return nil, err
}
events = append(events, e)
}
if err := rows.Err(); err != nil {
return nil, err
}
return events, nil
}
}
2018-03-09 18:07:08 +00:00
// GetEvent retrieves the event with the given id
2021-11-22 14:35:07 +00:00
func GetEvent(id int64) (e *Event, err error) {
e = &Event{}
2018-12-01 15:15:28 +00:00
err = DBQueryRow("SELECT id_event, txt, kind, time FROM events WHERE id_event=?", id).Scan(&e.Id, &e.Text, &e.Kind, &e.Time)
return
2016-12-26 02:44:04 +00:00
}
2018-03-09 18:07:08 +00:00
// NewEvent creates a new event in the database and returns the corresponding structure
2021-11-22 14:35:07 +00:00
func NewEvent(txt string, kind string) (*Event, error) {
2016-01-25 02:09:22 +00:00
if res, err := DBExec("INSERT INTO events (txt, kind, time) VALUES (?, ?, ?)", txt, kind, time.Now()); err != nil {
2021-11-22 14:35:07 +00:00
return nil, err
2016-01-25 02:09:22 +00:00
} else if eid, err := res.LastInsertId(); err != nil {
2021-11-22 14:35:07 +00:00
return nil, err
2016-01-25 02:09:22 +00:00
} else {
2021-11-22 14:35:07 +00:00
return &Event{eid, txt, kind, time.Now()}, nil
2016-01-25 02:09:22 +00:00
}
}
2018-03-09 18:07:08 +00:00
// Update applies modifications back to the database
2021-11-22 14:35:07 +00:00
func (e *Event) Update() (int64, error) {
2016-01-25 02:09:22 +00:00
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
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err
} else {
return nb, err
}
}
2018-03-09 18:07:08 +00:00
// Delete the event from the database
2021-11-22 14:35:07 +00:00
func (e *Event) Delete() (int64, error) {
2016-01-25 02:09:22 +00:00
if res, err := DBExec("DELETE FROM events WHERE id_event = ?", e.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err
} else {
return nb, err
}
}
2016-12-26 02:44:04 +00:00
2018-03-09 18:07:08 +00:00
// ClearEvents removes all events from database
2016-12-26 02:44:04 +00:00
func ClearEvents() (int64, error) {
if res, err := DBExec("DELETE FROM events"); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err
} else {
return nb, err
}
}