-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
101 lines (77 loc) · 2.12 KB
/
app.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
require 'sinatra'
require 'json'
require_relative 'mongo'
require_relative 'lib/weight_helper'
require_relative 'lib/user_helper'
include WeightHelper
include UserHelper
# require './lib/syslotem_info'
set :server, 'thin'
get '/' do
@cards = [
{title: 'Users', color: 'red', icon: 'fa-child'},
{title: 'History', color: 'green', icon: 'fa-bar-chart-o'}
]
erb :index
end
get '/history' do
@data = history_last_week
erb :history
end
get '/users' do
@users = %w.roo dan.
erb :users
end
get '/users/:name' do |name|
u = user(name)
weights = user_weights(name).desc(:date).to_a
change = weights.last[:weight] - weights.first[:weight]
week_weights = weights_for_between(name, Time.now - 7.days, Time.now).to_a
week_change = week_weights.last[:weight] - week_weights.first[:weight]
@user = {
name: u[:full_name],
slug: u[:user],
weights: weights,
change: change,
week_change: week_change
}
erb :user
end
post '/weight/:name' do |name|
body = request.body.rewind
payload = JSON.parse request.body.read
puts payload
date = payload['date']
weight = payload['mass']
test = payload['test']
user_create({
user: name,
date: date,
weight: weight,
test: test
})
end
delete '/weight/:id' do |id|
# Weight.where(_id: id).delete
# 201
puts "Tried to delete #{id}"
201
end
get '/weight/all', :provides => 'application/json' do
all_weights.to_json
end
get '/weight/all/from/:from/til/:til', :provides => 'application/json' do |from, til|
# Weight.where(:date.gte => from, :date.lte => til).to_json
weights_for_between(nil, from, til).to_json
end
get '/weight/all/from/:from', :provides => 'application/json' do |from|
# Weight.where(:date.gte => from, :date.lte => Date.today).to_json
weights_for_between(nil, from, Date.today).to_json
end
get '/weight/:name/all', :provides => 'application/json' do |name|
user_weights(name).to_json
end
get '/weight/:name/from/:from/til/:til', :provides => 'application/json' do |name, from, til|
# Weight.where(user: name,:date.gte => from, :date.lte => til).to_json
weights_for_between(name, from, til).to_json
end