sql: add default moods
This commit is contained in:
parent
4a4abb7f2f
commit
12f550a138
25
serve.go
25
serve.go
|
@ -113,9 +113,34 @@ func (s *server) makeDB() error {
|
||||||
if _, err := s.db.Exec(q); err != nil {
|
if _, err := s.db.Exec(q); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
q = `select count(*) from mood_category_texts mct inner join mood_categories mc on mct.mood_category_id=mc.id`
|
||||||
|
var count int
|
||||||
|
if err := s.db.Get(&count, q); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if count == 0 {
|
||||||
|
return s.populateMoods()
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *server) populateMoods() error {
|
||||||
|
tx := s.db.MustBegin()
|
||||||
|
res := tx.MustExec(`insert into mood_categories (name) values ('happy')`)
|
||||||
|
id, err := res.LastInsertId()
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
tx.MustExec(`insert into mood_category_texts (mood_category_id,key,value) values (?,?,?)`,
|
||||||
|
id, "😄", 1)
|
||||||
|
tx.MustExec(`insert into mood_category_texts (mood_category_id,key,value) values (?,?,?)`,
|
||||||
|
id, "😐", 0)
|
||||||
|
tx.MustExec(`insert into mood_category_texts (mood_category_id,key,value) values (?,?,?)`,
|
||||||
|
id, "😟", -1)
|
||||||
|
return tx.Commit()
|
||||||
|
}
|
||||||
|
|
||||||
func (s *server) recordMood(mood getMoodsResponse, who UserID) error {
|
func (s *server) recordMood(mood getMoodsResponse, who UserID) error {
|
||||||
q := `insert into moods (user_id,mood_category_id,value,time) values (?,?,?,?)`
|
q := `insert into moods (user_id,mood_category_id,value,time) values (?,?,?,?)`
|
||||||
_, err := s.db.Exec(q, who.ID, mood.CategoryID, mood.Value, time.Now().Unix())
|
_, err := s.db.Exec(q, who.ID, mood.CategoryID, mood.Value, time.Now().Unix())
|
||||||
|
|
Loading…
Reference in New Issue