123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- package controllers
- import (
- "strings"
- "time"
- "github.com/revel/revel"
- "github.com/revolvingcow/grassfed/app/models"
- )
- type Profile struct {
- Application
- }
- func (c Profile) getHistory(account *models.Account) []*models.History {
- if account == nil {
- return nil
- }
- results, err := c.Transaction.Select(
- models.History{},
- `select * from History where AccountId = ? order by Date desc`,
- account.Id)
- if err != nil {
- return nil
- }
- rows := len(results)
- if rows == 0 {
- return nil
- }
- history := make([]*models.History, rows)
- for i := 0; i < rows; i++ {
- history = append(history, results[i].(*models.History))
- }
- return history
- }
- func (c Profile) getMoment(id int64) *models.History {
- history, err := c.Transaction.Select(models.History{}, `select * from History where Id = ?`, id)
- if err != nil {
- panic(err)
- }
- if len(history) == 0 {
- return nil
- }
- return history[0].(*models.History)
- }
- func (c Profile) Index() revel.Result {
- return c.Render()
- }
- func (c Profile) Logon(id string) revel.Result {
- c.Response.ContentType = "application/json"
- c.Validation.Required(id).Message("You must be logged on.")
- if c.Validation.HasErrors() {
- revel.INFO.Println("Validation errors found.")
- c.Validation.Keep()
- c.FlashParams()
- return c.RenderJson(nil)
- }
- revel.INFO.Println("Setting up the variables for storage.")
- now := time.Now()
- account := c.getAccount(id)
- if account == nil {
- revel.INFO.Println("Creating account.")
- account = &models.Account{}
- account.Profile = id
- account.Goal = 2000
- account.Created = now
- account.LastVisit = now
- c.Transaction.Insert(account)
- } else {
- revel.INFO.Println("Updating account.")
- account.LastVisit = now
- c.Transaction.Update(account)
- }
- c.Session["account"] = id
- c.Session.SetDefaultExpiration()
- return c.RenderJson(true)
- }
- func (c Profile) History() revel.Result {
- account := c.Connected()
- if account == nil {
- return c.RenderJson(nil)
- }
- history := c.getHistory(account)
- return c.RenderJson(history)
- }
- func (c Profile) Stats() revel.Result {
- account := c.Connected()
- if account == nil {
- return c.RenderJson(nil)
- }
- history := c.getHistory(account)
- current := int64(0)
- if history != nil {
- for i := 0; i < len(history); i++ {
- if history[i] != nil {
- current += history[i].Calories
- }
- }
- }
- response := models.ResponseStatistics {
- Goal: account.Goal,
- Current: current,
- }
- return c.RenderJson(response)
- }
- func (c Profile) Add(product string, calories int64) revel.Result {
- account := c.Connected()
- if account == nil || strings.TrimSpace(product) == "" {
- return c.RenderJson(nil)
- }
- c.Validation.Required(product).Message("You must include a product.")
- c.Validation.Required(calories).Message("You must provide the amount of calories")
- if c.Validation.HasErrors() {
- c.Validation.Keep()
- c.FlashParams()
- return c.RenderJson(nil)
- }
- moment := models.History {
- AccountId: account.Id,
- Product: product,
- Calories: calories,
- Date: time.Now(),
- }
- c.Transaction.Insert(&moment)
- return c.RenderJson(moment)
- }
- func (c Profile) Delete(id int64) revel.Result {
- account := c.Connected()
- if account == nil {
- return c.RenderJson(nil)
- }
- moment := c.getMoment(id)
- if moment == nil {
- return c.RenderJson(nil)
- }
- c.Transaction.Delete(moment)
- return c.RenderJson(true)
- }
- func (c Profile) Goal(calories int64) revel.Result {
- account := c.Connected()
- if account == nil {
- return c.RenderJson(nil)
- }
- account.Goal = calories
- c.Transaction.Update(account)
- return c.RenderJson(true)
- }
|