Skip to content

Commit

Permalink
move grouped pomodoros to presenter
Browse files Browse the repository at this point in the history
  • Loading branch information
sizief committed Jul 24, 2020
1 parent fe1e04c commit 2c33d5d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
17 changes: 2 additions & 15 deletions api/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,8 @@ def permit(params, allowed)
# == Returns
# Array<{created_at: Date, project_name: number of pomodoro, project_color: color_string }>
get '/pomodoros_grouped' do
result = {pomodoros: [], projects: []}
(-21..0).each do |offset|
date = Date.today+offset
item = {}
item[:created_at] = date.strftime("%a %d")
Pomodoro.where(project_id: @user.projects)
.where(created_at: date.beginning_of_day..date.end_of_day).each do |pmd|
project_key = pmd.project.name.to_sym
item[project_key] = item.key?(project_key) ? item[project_key]+1 : 1
item["#{pmd.project.name}Color".to_sym] = 'hsl(228, 70%, 50%)' #pmd.project.color
end
result[:pomodoros].push item
end
result[:projects] = @user.projects.map(&:name)
result.to_json
max_number_of_days = 21
PomodoroGroupedPresenter.new(@user, max_number_of_days).call.to_json
end

get '/health-check' do
Expand Down
1 change: 1 addition & 0 deletions api/lib/pomodoro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require_relative './user'
require_relative './project'
require_relative './pomodoro_presenter'
require_relative './pomodoro_grouped_presenter'

class Pomodoro < ActiveRecord::Base
validates_presence_of :project
Expand Down
24 changes: 24 additions & 0 deletions api/lib/pomodoro_grouped_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class PomodoroGroupedPresenter
def initialize(user, max_days)
@user= user
@max_days = max_days
end

def call
result = {pomodoros: [], projects: []}
(-@max_days..0).each do |offset|
date = Date.today+offset
item = {}
item[:created_at] = date.strftime("%a %d")
Pomodoro.where(project_id: @user.projects)
.where(created_at: date.beginning_of_day..date.end_of_day).each do |pmd|
project_key = pmd.project.name.to_sym
item[project_key] = item.key?(project_key) ? item[project_key]+1 : 1
item["#{pmd.project.name}Color".to_sym] = 'hsl(228, 70%, 50%)' #pmd.project.color
end
result[:pomodoros].push item
end
result[:projects] = @user.projects.map(&:name)
result
end
end

0 comments on commit 2c33d5d

Please sign in to comment.