瀏覽代碼

added some more tables for weight and goal to be more flexible and fixed some minor UI issues

bmallred 10 年之前
父節點
當前提交
c3c09798cc

+ 3 - 1
app/controllers/database.go

19
19
20
    DbMap.AddTable(models.Account{}).SetKeys(true, "Id")
20
    DbMap.AddTable(models.Account{}).SetKeys(true, "Id")
21
    DbMap.AddTable(models.History{}).SetKeys(true, "Id")
21
    DbMap.AddTable(models.History{}).SetKeys(true, "Id")
22
    DbMap.AddTable(models.Goal{}).SetKeys(true, "Id")
23
    DbMap.AddTable(models.Weight{}).SetKeys(true, "Id")
22
24
23
    DbMap.TraceOn("[db]", revel.INFO)
25
    DbMap.TraceOn("[db]", revel.INFO)
24
    DbMap.CreateTables()
26
    DbMap.CreateTablesIfNotExists()
25
}
27
}
26
28
27
type DatabaseController struct {
29
type DatabaseController struct {

+ 57 - 7
app/controllers/profile.go

39
    return history
39
    return history
40
}
40
}
41
41
42
func (c Profile) getGoal(account *models.Account) (goal int64) {
43
    goal = 2000
44
45
    if account == nil {
46
        return goal
47
    }
48
49
    results := models.Goal{}
50
    err := c.Transaction.SelectOne(
51
        &results,
52
        `select * from Goal where AccountId = ? order by Date desc limit 1`,
53
        account.Id)
54
55
    if err != nil {
56
        return goal
57
    }
58
59
    goal = results.Calories
60
    return goal
61
}
62
63
func (c Profile) setGoal(account *models.Account, calories int64) {
64
    goals, err := c.Transaction.Select(
65
        models.Goal{},
66
        `select * from Goal where AccountId = ? order by Date desc limit 1`,
67
        account.Id)
68
69
    if err != nil {
70
        revel.INFO.Println(err)
71
        return
72
    }
73
74
    now := time.Now().Local()
75
76
    if len(goals) > 0 {
77
        goal := goals[0].(*models.Goal)
78
        local := goal.Date.Local()
79
80
        if now.Day() == local.Day() && now.Month() == local.Month() && now.Year() == local.Year() {
81
            goal.Calories = calories
82
            c.Transaction.Update(goal)
83
        } else {
84
            newGoal := models.Goal{ AccountId: account.Id, Calories: calories, Date: now }
85
            c.Transaction.Insert(&newGoal)
86
        }
87
    } else {
88
        newGoal := models.Goal{ AccountId: account.Id, Calories: calories, Date: now }
89
        c.Transaction.Insert(&newGoal)
90
    }
91
}
92
42
func (c Profile) getCaloriesForDate(history []*models.History, date time.Time) (current int64) {
93
func (c Profile) getCaloriesForDate(history []*models.History, date time.Time) (current int64) {
43
    current = 0
94
    current = 0
44
95
94
}
145
}
95
146
96
func (c Profile) Index() revel.Result {
147
func (c Profile) Index() revel.Result {
97
    return c.Render()
148
    account := c.Connected()
149
    return c.Render(account)
98
}
150
}
99
151
100
func (c Profile) Logon(id string) revel.Result {
152
func (c Profile) Logon(id string) revel.Result {
116
        revel.INFO.Println("Creating account.")
168
        revel.INFO.Println("Creating account.")
117
        account = &models.Account{}
169
        account = &models.Account{}
118
        account.Profile = id
170
        account.Profile = id
119
        account.Goal = 2000
120
        account.Created = now
171
        account.Created = now
121
        account.LastVisit = now
172
        account.LastVisit = now
122
        c.Transaction.Insert(account)
173
        c.Transaction.Insert(account)
148
        return c.RenderJson(nil)
199
        return c.RenderJson(nil)
149
    }
200
    }
150
201
202
    goal := c.getGoal(account)
151
    history := c.getHistory(account)
203
    history := c.getHistory(account)
152
204
153
    response := models.ResponseStatistics {
205
    response := models.ResponseStatistics {
154
        Goal: account.Goal,
206
        Goal: goal,
155
        Current: c.getCaloriesForDate(history, time.Now()),
207
        Current: c.getCaloriesForDate(history, time.Now()),
156
        Streak: c.getStreak(history, account.Goal),
208
        Streak: c.getStreak(history, goal),
157
    }
209
    }
158
210
159
    return c.RenderJson(response)
211
    return c.RenderJson(response)
206
        return c.RenderJson(nil)
258
        return c.RenderJson(nil)
207
    }
259
    }
208
260
209
    account.Goal = calories
210
    c.Transaction.Update(account)
211
261
    c.setGoal(account, calories)
212
    return c.RenderJson(true)
262
    return c.RenderJson(true)
213
}
263
}

+ 0 - 1
app/models/account.go

7
type Account struct {
7
type Account struct {
8
    Id              int64
8
    Id              int64
9
    Profile         string
9
    Profile         string
10
    Goal            int64
11
    Created         time.Time
10
    Created         time.Time
12
    LastVisit       time.Time
11
    LastVisit       time.Time
13
}
12
}

+ 10 - 0
app/models/goal.go

1
package models
2
3
import "time"
4
5
type Goal struct {
6
    Id              int64
7
    AccountId       int64
8
    Calories        int64
9
    Date            time.Time
10
}

+ 10 - 0
app/models/weight.go

1
package models
2
3
import "time"
4
5
type Weight struct {
6
    Id              int64
7
    AccountId       int64
8
    Weight          float64
9
    Date            time.Time
10
}

+ 43 - 43
app/routes/routes.go

30
}
30
}
31
31
32
32
33
type tTestRunner struct {}
34
var TestRunner tTestRunner
35
36
37
func (_ tTestRunner) Index(
38
		) string {
39
	args := make(map[string]string)
40
	
41
	return revel.MainRouter.Reverse("TestRunner.Index", args).Url
42
}
43
44
func (_ tTestRunner) Run(
45
		suite string,
46
		test string,
47
		) string {
48
	args := make(map[string]string)
49
	
50
	revel.Unbind(args, "suite", suite)
51
	revel.Unbind(args, "test", test)
52
	return revel.MainRouter.Reverse("TestRunner.Run", args).Url
53
}
54
55
func (_ tTestRunner) List(
56
		) string {
57
	args := make(map[string]string)
58
	
59
	return revel.MainRouter.Reverse("TestRunner.List", args).Url
60
}
61
62
33
type tStatic struct {}
63
type tStatic struct {}
34
var Static tStatic
64
var Static tStatic
35
65
59
}
89
}
60
90
61
91
62
type tTestRunner struct {}
63
var TestRunner tTestRunner
92
type tApplication struct {}
93
var Application tApplication
64
94
65
95
66
func (_ tTestRunner) Index(
96
97
type tHome struct {}
98
var Home tHome
99
100
101
func (_ tHome) Index(
67
		) string {
102
		) string {
68
	args := make(map[string]string)
103
	args := make(map[string]string)
69
	
104
	
70
	return revel.MainRouter.Reverse("TestRunner.Index", args).Url
105
	return revel.MainRouter.Reverse("Home.Index", args).Url
71
}
106
}
72
107
73
func (_ tTestRunner) Run(
74
		suite string,
75
		test string,
108
func (_ tHome) About(
76
		) string {
109
		) string {
77
	args := make(map[string]string)
110
	args := make(map[string]string)
78
	
111
	
79
	revel.Unbind(args, "suite", suite)
80
	revel.Unbind(args, "test", test)
81
	return revel.MainRouter.Reverse("TestRunner.Run", args).Url
112
	return revel.MainRouter.Reverse("Home.About", args).Url
82
}
113
}
83
114
84
func (_ tTestRunner) List(
115
func (_ tHome) Overview(
85
		) string {
116
		) string {
86
	args := make(map[string]string)
117
	args := make(map[string]string)
87
	
118
	
88
	return revel.MainRouter.Reverse("TestRunner.List", args).Url
119
	return revel.MainRouter.Reverse("Home.Overview", args).Url
89
}
120
}
90
121
91
122
92
type tApplication struct {}
93
var Application tApplication
94
95
96
97
type tProfile struct {}
123
type tProfile struct {}
98
var Profile tProfile
124
var Profile tProfile
99
125
158
}
184
}
159
185
160
186
161
type tHome struct {}
162
var Home tHome
163
164
165
func (_ tHome) Index(
166
		) string {
167
	args := make(map[string]string)
168
	
169
	return revel.MainRouter.Reverse("Home.Index", args).Url
170
}
171
172
func (_ tHome) About(
173
		) string {
174
	args := make(map[string]string)
175
	
176
	return revel.MainRouter.Reverse("Home.About", args).Url
177
}
178
179
func (_ tHome) Overview(
180
		) string {
181
	args := make(map[string]string)
182
	
183
	return revel.MainRouter.Reverse("Home.Overview", args).Url
184
}
185
186

+ 52 - 51
app/tmp/main.go

6
	"reflect"
6
	"reflect"
7
	"github.com/revel/revel"
7
	"github.com/revel/revel"
8
	_ "github.com/mattn/go-sqlite3"
8
	_ "github.com/mattn/go-sqlite3"
9
	controllers0 "github.com/revel/revel/modules/static/app/controllers"
9
	controllers1 "github.com/revel/revel/modules/static/app/controllers"
10
	_ "github.com/revel/revel/modules/testrunner/app"
10
	_ "github.com/revel/revel/modules/testrunner/app"
11
	controllers1 "github.com/revel/revel/modules/testrunner/app/controllers"
11
	controllers0 "github.com/revel/revel/modules/testrunner/app/controllers"
12
	_ "github.com/revolvingcow/grassfed/app"
12
	_ "github.com/revolvingcow/grassfed/app"
13
	controllers "github.com/revolvingcow/grassfed/app/controllers"
13
	controllers "github.com/revolvingcow/grassfed/app/controllers"
14
	tests "github.com/revolvingcow/grassfed/tests"
14
	tests "github.com/revolvingcow/grassfed/tests"
55
			
55
			
56
		})
56
		})
57
	
57
	
58
	revel.RegisterController((*controllers0.Static)(nil),
58
	revel.RegisterController((*controllers0.TestRunner)(nil),
59
		[]*revel.MethodType{
60
			&revel.MethodType{
61
				Name: "Index",
62
				Args: []*revel.MethodArg{ 
63
				},
64
				RenderArgNames: map[int][]string{ 
65
					46: []string{ 
66
						"testSuites",
67
					},
68
				},
69
			},
70
			&revel.MethodType{
71
				Name: "Run",
72
				Args: []*revel.MethodArg{ 
73
					&revel.MethodArg{Name: "suite", Type: reflect.TypeOf((*string)(nil)) },
74
					&revel.MethodArg{Name: "test", Type: reflect.TypeOf((*string)(nil)) },
75
				},
76
				RenderArgNames: map[int][]string{ 
77
					69: []string{ 
78
						"error",
79
					},
80
				},
81
			},
82
			&revel.MethodType{
83
				Name: "List",
84
				Args: []*revel.MethodArg{ 
85
				},
86
				RenderArgNames: map[int][]string{ 
87
				},
88
			},
89
			
90
		})
91
	
92
	revel.RegisterController((*controllers1.Static)(nil),
59
		[]*revel.MethodType{
93
		[]*revel.MethodType{
60
			&revel.MethodType{
94
			&revel.MethodType{
61
				Name: "Serve",
95
				Name: "Serve",
79
			
113
			
80
		})
114
		})
81
	
115
	
82
	revel.RegisterController((*controllers1.TestRunner)(nil),
116
	revel.RegisterController((*controllers.Application)(nil),
117
		[]*revel.MethodType{
118
			
119
		})
120
	
121
	revel.RegisterController((*controllers.Home)(nil),
83
		[]*revel.MethodType{
122
		[]*revel.MethodType{
84
			&revel.MethodType{
123
			&revel.MethodType{
85
				Name: "Index",
124
				Name: "Index",
86
				Args: []*revel.MethodArg{ 
125
				Args: []*revel.MethodArg{ 
87
				},
126
				},
88
				RenderArgNames: map[int][]string{ 
127
				RenderArgNames: map[int][]string{ 
89
					46: []string{ 
90
						"testSuites",
128
					31: []string{ 
91
					},
129
					},
92
				},
130
				},
93
			},
131
			},
94
			&revel.MethodType{
132
			&revel.MethodType{
95
				Name: "Run",
133
				Name: "About",
96
				Args: []*revel.MethodArg{ 
134
				Args: []*revel.MethodArg{ 
97
					&revel.MethodArg{Name: "suite", Type: reflect.TypeOf((*string)(nil)) },
98
					&revel.MethodArg{Name: "test", Type: reflect.TypeOf((*string)(nil)) },
99
				},
135
				},
100
				RenderArgNames: map[int][]string{ 
136
				RenderArgNames: map[int][]string{ 
101
					69: []string{ 
102
						"error",
137
					35: []string{ 
103
					},
138
					},
104
				},
139
				},
105
			},
140
			},
106
			&revel.MethodType{
141
			&revel.MethodType{
107
				Name: "List",
142
				Name: "Overview",
108
				Args: []*revel.MethodArg{ 
143
				Args: []*revel.MethodArg{ 
109
				},
144
				},
110
				RenderArgNames: map[int][]string{ 
145
				RenderArgNames: map[int][]string{ 
113
			
148
			
114
		})
149
		})
115
	
150
	
116
	revel.RegisterController((*controllers.Application)(nil),
117
		[]*revel.MethodType{
118
			
119
		})
120
	
121
	revel.RegisterController((*controllers.Profile)(nil),
151
	revel.RegisterController((*controllers.Profile)(nil),
122
		[]*revel.MethodType{
152
		[]*revel.MethodType{
123
			&revel.MethodType{
153
			&revel.MethodType{
125
				Args: []*revel.MethodArg{ 
155
				Args: []*revel.MethodArg{ 
126
				},
156
				},
127
				RenderArgNames: map[int][]string{ 
157
				RenderArgNames: map[int][]string{ 
128
					497: []string{ 
158
					1449: []string{ 
159
						"account",
129
					},
160
					},
130
				},
161
				},
131
			},
162
			},
179
			
210
			
180
		})
211
		})
181
	
212
	
182
	revel.RegisterController((*controllers.Home)(nil),
183
		[]*revel.MethodType{
184
			&revel.MethodType{
185
				Name: "Index",
186
				Args: []*revel.MethodArg{ 
187
				},
188
				RenderArgNames: map[int][]string{ 
189
					31: []string{ 
190
					},
191
				},
192
			},
193
			&revel.MethodType{
194
				Name: "About",
195
				Args: []*revel.MethodArg{ 
196
				},
197
				RenderArgNames: map[int][]string{ 
198
					35: []string{ 
199
					},
200
				},
201
			},
202
			&revel.MethodType{
203
				Name: "Overview",
204
				Args: []*revel.MethodArg{ 
205
				},
206
				RenderArgNames: map[int][]string{ 
207
				},
208
			},
209
			
210
		})
211
	
212
	revel.DefaultValidationKeys = map[string]map[int]string{ 
213
	revel.DefaultValidationKeys = map[string]map[int]string{ 
213
		"github.com/revolvingcow/grassfed/app/controllers.Profile.Add": { 
214
		"github.com/revolvingcow/grassfed/app/controllers.Profile.Add": { 
214
			168: "product",
215
			169: "calories",
215
			220: "product",
216
			221: "calories",
216
		},
217
		},
217
		"github.com/revolvingcow/grassfed/app/controllers.Profile.Logon": { 
218
		"github.com/revolvingcow/grassfed/app/controllers.Profile.Logon": { 
218
			102: "id",
219
			154: "id",
219
		},
220
		},
220
	}
221
	}
221
	revel.TestSuites = []interface{}{ 
222
	revel.TestSuites = []interface{}{ 

+ 2 - 2
app/views/Home/Index.html

7
    <h1></h1>
7
    <h1></h1>
8
    
8
    
9
    <div class="row">
9
    <div class="row">
10
        <div class="col-md-4 text-center">
10
        <div class="col-md-6 text-center">
11
            <h3 class="accounts">0</h3>
11
            <h3 class="accounts">0</h3>
12
            <h3>Accounts</h3>
12
            <h3>Accounts</h3>
13
        </div>
13
        </div>
14
        <div class="col-md-4 text-center">
14
        <div class="col-md-6 text-center">
15
            <h3 class="calories">0</h3>
15
            <h3 class="calories">0</h3>
16
            <h3>Calories</h3>
16
            <h3>Calories</h3>
17
        </div>
17
        </div>

+ 7 - 1
app/views/Profile/Index.html

6
{{append . "moreScripts" "/public/js/googleplus.js"}}
6
{{append . "moreScripts" "/public/js/googleplus.js"}}
7
{{append . "moreScripts" "/public/js/grassfed.js"}}
7
{{append . "moreScripts" "/public/js/grassfed.js"}}
8
8
9
<div id="authentication" class="row" style="padding: 50px 15px; display: none;">
9
{{if not .account}}
10
<div id="authentication" class="row" style="padding: 50px 15px;">
10
    <div class="col-md-offset-4 col-md-4 text-center">
11
    <div class="col-md-offset-4 col-md-4 text-center">
11
        <h1></h1>
12
        <h1></h1>
12
13
26
        </p>
27
        </p>
27
    </div>
28
    </div>
28
</div>
29
</div>
30
{{end}}
29
31
32
{{if not .account}}
30
<div id="profile" class="row" style="padding: 50px 15px; display: none;">
33
<div id="profile" class="row" style="padding: 50px 15px; display: none;">
34
{{else}}
35
<div id="profile" class="row" style="padding: 50px 15px;">
36
{{end}}
31
    <h1></h1>
37
    <h1></h1>
32
38
33
    <div class="col-md-8">
39
    <div class="col-md-8">

+ 8 - 6
public/js/grassfed.js

9
})(jQuery);
9
})(jQuery);
10
10
11
$(function () {
11
$(function () {
12
    // Bring the authentication down slowly...
13
    // this is in case they already have a valid sign-on.
14
    $('div#authentication').delay(1000).fadeIn();
15
16
    var chart = $("#goalChart")[0];
12
    var chart = $("#goalChart")[0];
17
    var doughnutChart;
13
    var doughnutChart;
18
14
25
        $('input[name="products"][type="text"]').focus();
21
        $('input[name="products"][type="text"]').focus();
26
    }
22
    }
27
23
28
    $('div#profile').on('show', function () {
24
    // Check to see if the profile is not hidden. If it is not then start your engines!
25
    if (!$('div#profile').is(':hidden')) {
29
        startEngine();
26
        startEngine();
30
    });
27
    }
28
    else {
29
        $('div#profile').on('show', function () {
30
            startEngine();
31
        });
32
    }
31
33
32
    $('form#entry').on('submit', function (e) {
34
    $('form#entry').on('submit', function (e) {
33
        e.preventDefault();
35
        e.preventDefault();