Calorie counting web application written in the Go language

profile.go 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package controllers
  2. import (
  3. "strings"
  4. "time"
  5. "github.com/revel/revel"
  6. "github.com/revolvingcow/grassfed/app/models"
  7. )
  8. type Profile struct {
  9. Application
  10. }
  11. func (c Profile) getHistory(account *models.Account) []*models.History {
  12. if account == nil {
  13. return nil
  14. }
  15. results, err := c.Transaction.Select(
  16. models.History{},
  17. `select * from History where AccountId = ? order by Date desc`,
  18. account.Id)
  19. if err != nil {
  20. return nil
  21. }
  22. rows := len(results)
  23. if rows == 0 {
  24. return nil
  25. }
  26. history := make([]*models.History, rows)
  27. for i := 0; i < rows; i++ {
  28. history = append(history, results[i].(*models.History))
  29. }
  30. return history
  31. }
  32. func (c Profile) getMoment(id int64) *models.History {
  33. history, err := c.Transaction.Select(models.History{}, `select * from History where Id = ?`, id)
  34. if err != nil {
  35. panic(err)
  36. }
  37. if len(history) == 0 {
  38. return nil
  39. }
  40. return history[0].(*models.History)
  41. }
  42. func (c Profile) Index() revel.Result {
  43. return c.Render()
  44. }
  45. func (c Profile) Logon(id string) revel.Result {
  46. c.Response.ContentType = "application/json"
  47. c.Validation.Required(id).Message("You must be logged on.")
  48. if c.Validation.HasErrors() {
  49. revel.INFO.Println("Validation errors found.")
  50. c.Validation.Keep()
  51. c.FlashParams()
  52. return c.RenderJson(nil)
  53. }
  54. revel.INFO.Println("Setting up the variables for storage.")
  55. now := time.Now()
  56. account := c.getAccount(id)
  57. if account == nil {
  58. revel.INFO.Println("Creating account.")
  59. account = &models.Account{}
  60. account.Profile = id
  61. account.Goal = 2000
  62. account.Created = now
  63. account.LastVisit = now
  64. c.Transaction.Insert(account)
  65. } else {
  66. revel.INFO.Println("Updating account.")
  67. account.LastVisit = now
  68. c.Transaction.Update(account)
  69. }
  70. c.Session["account"] = id
  71. c.Session.SetDefaultExpiration()
  72. return c.RenderJson(true)
  73. }
  74. func (c Profile) History() revel.Result {
  75. account := c.Connected()
  76. if account == nil {
  77. return c.RenderJson(nil)
  78. }
  79. history := c.getHistory(account)
  80. return c.RenderJson(history)
  81. }
  82. func (c Profile) Stats() revel.Result {
  83. account := c.Connected()
  84. if account == nil {
  85. return c.RenderJson(nil)
  86. }
  87. history := c.getHistory(account)
  88. current := int64(0)
  89. if history != nil {
  90. for i := 0; i < len(history); i++ {
  91. if history[i] != nil {
  92. current += history[i].Calories
  93. }
  94. }
  95. }
  96. response := models.ResponseStatistics {
  97. Goal: account.Goal,
  98. Current: current,
  99. }
  100. return c.RenderJson(response)
  101. }
  102. func (c Profile) Add(product string, calories int64) revel.Result {
  103. account := c.Connected()
  104. if account == nil || strings.TrimSpace(product) == "" {
  105. return c.RenderJson(nil)
  106. }
  107. c.Validation.Required(product).Message("You must include a product.")
  108. c.Validation.Required(calories).Message("You must provide the amount of calories")
  109. if c.Validation.HasErrors() {
  110. c.Validation.Keep()
  111. c.FlashParams()
  112. return c.RenderJson(nil)
  113. }
  114. moment := models.History {
  115. AccountId: account.Id,
  116. Product: product,
  117. Calories: calories,
  118. Date: time.Now(),
  119. }
  120. c.Transaction.Insert(&moment)
  121. return c.RenderJson(moment)
  122. }
  123. func (c Profile) Delete(id int64) revel.Result {
  124. account := c.Connected()
  125. if account == nil {
  126. return c.RenderJson(nil)
  127. }
  128. moment := c.getMoment(id)
  129. if moment == nil {
  130. return c.RenderJson(nil)
  131. }
  132. c.Transaction.Delete(moment)
  133. return c.RenderJson(true)
  134. }
  135. func (c Profile) Goal(calories int64) revel.Result {
  136. account := c.Connected()
  137. if account == nil {
  138. return c.RenderJson(nil)
  139. }
  140. account.Goal = calories
  141. c.Transaction.Update(account)
  142. return c.RenderJson(true)
  143. }