This repository has been archived by the owner on Jul 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworksheet.rb
50 lines (42 loc) · 1.49 KB
/
worksheet.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
require 'rubygems'
require 'sinatra'
require 'data_mapper'
require "bundler/setup"
#Carrega arquivos de configurações:
config = YAML.load_file('config.yml') if File.exist?('config.yml')
#Configurações do Sinatra
set :server, %w[thin mongrel webrick]
set :bind, config ? config['bind'] : "localhost"
#Load Database Configuration
require_relative "database_setup.rb"
#Mapeamento das rotas
get '/' do
@users = User.get_all_users
erb :index
end
post '/worksheet' do
@to_date = DateTime.strptime(params[:to],'%Y-%m-%d').strftime('%d/%m/%Y')
@from_date = DateTime.strptime(params[:from],'%Y-%m-%d').strftime('%d/%m/%Y')
@user = User.first({login: params[:login]})
user_login = params[:user_login] == "myself" ? @user.login : params[:user_login]
@time_entries = []
if @user.authenticate(params[:password]) #TODO: Pesquisar como evitar SQL Injection no Sinatra
if @user.login == user_login || @user.admin
DataMapper::Repository.adapters.keys.each do |repository|
DataMapper.repository(repository){
@time_entries += TimeEntry.get_users_time_entry(user_login, params[:from], params[:to])
}
end
@time_entries = @time_entries.group_by{ |te| te.spent_on}
erb :worksheet
else
params[:alert] = "Você nâ tem permissâo para visualizar as horas do usuàio #{user_login}"
@users = User.get_all_users
erb :index
end
else
params[:alert] = "Usuário e senha inválidos"
@users = User.get_all_users
erb :index
end
end