sql: add default moods

This commit is contained in:
Chris Sexton 2019-10-28 11:52:25 -04:00
parent 4a4abb7f2f
commit 12f550a138
1 changed files with 25 additions and 0 deletions

View File

@ -113,9 +113,34 @@ func (s *server) makeDB() error {
if _, err := s.db.Exec(q); err != nil {
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
}
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 {
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())