From 12f550a138348522881714af3c5b5d17634a6918 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Mon, 28 Oct 2019 11:52:25 -0400 Subject: [PATCH] sql: add default moods --- serve.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/serve.go b/serve.go index 8cee5b9..f9f72d9 100644 --- a/serve.go +++ b/serve.go @@ -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())