-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers_controller.rb
177 lines (158 loc) · 5.82 KB
/
users_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
require 'date'
class Api::UsersController < ApplicationController
def index
@users = User.all
if @users
render json: {
users: @users
}
else
render json: {
status: 500,
errors: ['no users found']
}
end
end
def show
@user = User.find(params[:email])
if @user
render json: {
user: @user
}
else
render json: {
status: 500,
errors: ['user not found']
}
end
end
def daily_analytics
activity_factors = {
1 => 1.2,
2 => 1.375,
3 => 1.55,
4 => 1.725,
5 => 1.9
}
multiplier = {
4 => -0.2,
5 => -0.1,
1 => 0,
2 => 0.1,
3 => 0.2
}
if (current_user.gender == "Male") then
bmr = 5 + (10 * (current_user.weight / 2.2046)) + (6.25 * current_user.height) - (5 * current_user.age)
else
bmr = -161 + (10 * (current_user.weight / 2.2046)) + (6.25 * current_user.height) - (5 * current_user.age)
end
tdee = activity_factors[current_user.activity_level.id] * bmr
calories_goal = (1 + multiplier[current_user.goal.id]) * tdee
protein_goal = (0.3 * calories_goal) / 4
carbs_goal = (0.35 * calories_goal) / 4
fats_goal = (0.35 * calories_goal) / 9
today = Date.today.strftime("%Y-%m-%d")
progress_code = <<-SQL
SELECT SUM(foods.calories * (food_histories.servings/100)) as calories,
SUM(foods.calories * (food_histories.servings/100)) / #{calories_goal} as caloric_progress,
SUM(ROUND(foods.protein * (food_histories.servings/100))) as protein, SUM(foods.protein * (food_histories.servings/100)) / #{protein_goal} as protein_progress,
SUM(ROUND(foods.carbs * (food_histories.servings/100))) as carbs, SUM(foods.carbs * (food_histories.servings/100)) / #{carbs_goal} as carbs_progress,
SUM(ROUND(foods.fat * (food_histories.servings/100))) as fats, SUM(foods.fat * (food_histories.servings/100)) / #{fats_goal} as fats_progress
FROM users, foods, food_histories
WHERE food_histories.created_at = to_date('#{today}', 'YYYY-MM-DD')
AND food_histories.user_id = users.id AND foods.id = food_histories.food_id
AND users.id = #{current_user.id}
SQL
daily_history_code = <<-SQL
SELECT foods.name as name,
SUM(ROUND(foods.calories * (food_histories.servings/100))) as calories,
SUM(ROUND(foods.carbs * (food_histories.servings/100))) as carbs,
SUM(ROUND(foods.protein * (food_histories.servings/100))) as protein,
SUM(ROUND(foods.fat * (food_histories.servings/100))) as fat,
SUM(ROUND(foods.fibre * (food_histories.servings/100))) as fibre,
SUM(food_histories.servings) as servings
FROM users, foods, food_histories
WHERE food_histories.created_at = to_date('#{today}', 'YYYY-MM-DD')
AND food_histories.user_id = users.id AND foods.id = food_histories.food_id
AND users.id = #{current_user.id}
GROUP BY foods.name
ORDER BY foods.name ASC
SQL
progress = ActiveRecord::Base.connection.execute(progress_code)
daily_history = ActiveRecord::Base.connection.execute(daily_history_code)
render json: {
daily_history: daily_history,
progress: progress,
tdee: calories_goal,
protein_goal: protein_goal,
fats_goal: fats_goal,
carbs_goal: carbs_goal
}
end
def monthly_analytics
calories_results = ActiveRecord::Base.connection.execute(daily_calculator_query("calories"))
carbs_results = ActiveRecord::Base.connection.execute(daily_calculator_query("carbs"))
protein_results = ActiveRecord::Base.connection.execute(daily_calculator_query("protein"))
fat_results = ActiveRecord::Base.connection.execute(daily_calculator_query("fat"))
fibre_results = ActiveRecord::Base.connection.execute(daily_calculator_query("fibre"))
render json: {
calories_results: calories_results,
carbs_results: carbs_results,
protein_results: protein_results,
fat_results: fat_results,
fibre_results: fibre_results
}
end
def update
@user = User.where(email: params[:user][:email]).take
if @user.update(user_params)
render json: {
user: @user
}
else
render json: { errors: @user.errors }
end
end
def create
@user = User.new(user_params)
if @user.save
login!
render json: { user: @user }
else
render json: { errors: @user.errors }
end
end
private
def user_params
h = params.require(:user).permit(:email, :password,
:first_name, :last_name, :age, :weight,
:height, :gender, :activity_level, :fitness_goal)
g = Goal.find_by(description: h[:fitness_goal])
a = ActivityLevel.find_by(description: h[:activity_level])
h.delete(:activity_level)
h.delete(:fitness_goal)
activity_goal = {}
if a
activity_goal[:activity_level_id] = a.id
end
if g
activity_goal[:goal_id] = g.id
end
h.merge!(activity_goal)
h.to_h
end
def daily_calculator_query(macro)
one_month_ago = (Date.today - 30).strftime("%Y-%m-%d")
today = Date.today.strftime("%Y-%m-%d")
code = <<-SQL
SELECT t.count, to_char(t.date, 'yyyy-mm-dd') as date FROM (
SELECT sum(foods.#{macro} * (food_histories.servings / 100)) as count, date_trunc('day', food_histories.created_at) as date
FROM users, foods, food_histories
WHERE users.id = #{current_user.id} AND food_histories.user_id = users.id AND food_histories.food_id = foods.id
GROUP BY date_trunc('day', food_histories.created_at)
HAVING date_trunc('day', food_histories.created_at) > to_date('#{one_month_ago}', 'YYYY-MM-DD')
AND date_trunc('day', food_histories.created_at) <= to_date('#{today}', 'YYYY-MM-DD')
) AS t;
SQL
end
end