Add workouts
This commit is contained in:
27
app/actions/workouts/create.rb
Normal file
27
app/actions/workouts/create.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
module Adamantium
|
||||
module Actions
|
||||
module Workouts
|
||||
class Create < Action
|
||||
include Deps["geo.gpx_parser", "commands.workouts.create"]
|
||||
|
||||
def handle(req, res)
|
||||
tempfile = Tempfile.new(%w/path .gpx/)
|
||||
tempfile.write req.params[:file]
|
||||
tempfile.rewind
|
||||
|
||||
gpxfile = gpx_parser.call(file: tempfile)
|
||||
|
||||
if gpxfile.success?
|
||||
create.call(**gpxfile.value!)
|
||||
res.status = 201
|
||||
else
|
||||
res.status = 500
|
||||
end
|
||||
ensure
|
||||
tempfile.close
|
||||
tempfile.unlink
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
13
app/actions/workouts/index.rb
Normal file
13
app/actions/workouts/index.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
module Adamantium
|
||||
module Actions
|
||||
module Workouts
|
||||
class Index < Action
|
||||
include Deps["views.workouts.index"]
|
||||
|
||||
def handle(req, res)
|
||||
res.render index
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
19
app/commands/workouts/create.rb
Normal file
19
app/commands/workouts/create.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
|
||||
require "securerandom"
|
||||
require "dry/monads"
|
||||
require "filemagic"
|
||||
|
||||
module Adamantium
|
||||
module Commands
|
||||
module Workouts
|
||||
class Create < Command
|
||||
include Deps["repos.workout_repo"]
|
||||
include Dry::Monads[:result]
|
||||
|
||||
def call(svg:, distance:)
|
||||
workout_repo.create(path: svg, distance: distance, published_at: Time.now)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
11
app/repos/workout_repo.rb
Normal file
11
app/repos/workout_repo.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
module Adamantium
|
||||
module Repos
|
||||
class WorkoutRepo < Adamantium::Repo[:workouts]
|
||||
commands :create, update: :by_pk
|
||||
|
||||
def list
|
||||
workouts.order(:published_at).to_a
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
12
app/templates/workouts/index.html.slim
Normal file
12
app/templates/workouts/index.html.slim
Normal file
@@ -0,0 +1,12 @@
|
||||
div class="mb-12 prose dark:prose-invert max-w-prose mx-auto text-gray-800 dark:text-gray-200"
|
||||
h1 Hikes
|
||||
|
||||
div class="max-w-prose mx-auto"
|
||||
- workouts_by_year.each do |year, workouts|
|
||||
h3= year
|
||||
h1= "#{(workouts.map{|w| w.distance }.sum / 1000).round} km"
|
||||
div class="grid grid-cols-3 gap-4 mb-4 max-w-prose mx-auto"
|
||||
- workouts.each do |workout|
|
||||
== workout.path
|
||||
|
||||
div class="max-w-screen-md mx-auto border-t-4 border-solid border-gray-400 dark:border-gray-600"
|
18
app/views/workouts/index.rb
Normal file
18
app/views/workouts/index.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
module Adamantium
|
||||
module Views
|
||||
module Workouts
|
||||
class Index < View
|
||||
include Deps["repos.workout_repo"]
|
||||
|
||||
expose :workouts_by_year do
|
||||
workout_repo
|
||||
.list
|
||||
.group_by { |wo|
|
||||
wo.published_at.year
|
||||
}
|
||||
.sort
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user