Permalink
Browse files

Finished implementation of edit run

  • Loading branch information...
1 parent ad9d537 commit 8976363182b965c03e8e67cc979888032074810f @JanVanRyswyck committed Jan 8, 2012
View
@@ -29,16 +29,15 @@ bootstrapExpress = (application) ->
bootstrapRoutes = (application) ->
application.get('/', routes.index)
- application.get('/runs/new', routes.runs.new)
application.post('/runs', validators.run.validate, routes.runs.create)
+ application.get('/runs/new', routes.runs.new)
application.get('/runs/:year([0-9]{4})?', routes.runs.index)
application.put('/runs/:id([a-z0-9]{32})', validators.run.validate, routes.runs.update)
application.get('/runs/:id([a-z0-9]{32})', routes.runs.edit)
-
bootstrapCouchDB = ->
configuration.couchDBSettings((error, couchDB) ->
- if(error)
+ if error
throw new errors.ConfigurationError('An error occured while reading the configuration settings for the CouchDB database.', error)
cradle.setup(
View
@@ -1,4 +1,4 @@
-module.exports = class RunMapper
+module.exports = class DocumentToRunMapper
mapFrom: (document) ->
id: document._id
revision: document._rev
View
@@ -1,7 +1,7 @@
cradle = require('cradle')
connectionManager = require('./connectionmanager')
-errors = require('./../errors')
-RunMapper = require('./runmapper')
+errors = require('../errors')
+DocumentToRunMapper = require('./runmapper')
_ = require('underscore')
module.exports = class Runs
@@ -11,11 +11,11 @@ module.exports = class Runs
constructor: ->
connection = connectionManager.getConnection()
_database = connection.database('trackmyrun')
- _runMapper = new RunMapper()
+ _runMapper = new DocumentToRunMapper()
getById: (id, callback) ->
_database.get(id, (error, response) ->
- if(error)
+ if error
return callback(error)
run = _runMapper.mapFrom(response)
@@ -40,7 +40,7 @@ module.exports = class Runs
getRunsByYear: (year, callback) ->
_database.view('runs/runsByYear', { key: year }
(error, response) ->
- if(error)
+ if error
return callback(error)
runs = _.map(response,
@@ -53,7 +53,7 @@ module.exports = class Runs
save: (run, callback) ->
_database.save(run.id, run.revision, run,
(error, response) ->
- if(error)
+ if error
callback(error)
callback(error,
@@ -0,0 +1,14 @@
+_ = require('underscore')
+
+module.exports = class RunFactory
+ createDefault: () ->
+ type: 'run'
+ date: _.getCurrentDate()
+ distance: ''
+ duration:
+ hours: ''
+ minutes: ''
+ seconds: ''
+ averageHeartRate: ''
+ shoes: -1
+ comments: ''
View
@@ -0,0 +1,14 @@
+module.exports = class FormDataToRunMapper
+ mapFrom: (formData) ->
+ type: 'run'
+ date: formData.date
+ distance: formData.distance
+ duration: @.mapDurationFrom(formData)
+ averageHeartRate: formData.averageHeartRate
+ shoes: formData.shoes
+ comments: formData.comments
+
+ mapDurationFrom: (formData) ->
+ hours: formData.durationHours
+ minutes: formData.durationMinutes
+ seconds: formData.durationSeconds
View
@@ -1,7 +1,7 @@
step = require('step')
-errors = require('./../errors')
-Runs = require('./../data/runs')
-Shoes = require('./../data/shoes')
+errors = require('../errors')
+Runs = require('../data/runs')
+Shoes = require('../data/shoes')
exports.index = (request, response) ->
step(
@@ -13,7 +13,7 @@ exports.index = (request, response) ->
shoes.getShoesInUse(@.parallel())
renderView = (error, numberOfRunsPerYear, shoesInUse) ->
- if(error)
+ if error
throw new errors.DataError('An error occured while loading data for the main index page.', error)
response.render('index',
View
@@ -1,7 +1,9 @@
step = require('step')
-errors = require('./../../errors')
-Runs = require('./../../data/runs')
-Shoes = require('./../../data/shoes')
+errors = require('../../errors')
+FormDataToRunMapper = require('../../mappers/runmapper')
+Runs = require('../../data/runs')
+Shoes = require('../../data/shoes')
+Calculator = require('../../services/calculator')
exports.edit = (request, response) ->
runId = request.params.id
@@ -25,7 +27,7 @@ exports.update = (request, response) ->
throw new errors.DataError('An error occured while loading the data for updating a run.', error)
applyChangesTo(run, request.form)
- run.speed = calculateSpeedFor(run)
+ run.speed = new Calculator().calculateSpeedFor(run)
runs.save(run, @)
@@ -39,7 +41,6 @@ exports.update = (request, response) ->
renderViewForEditRun = (runId, response, validationErrors) ->
step(
loadData = ->
- # TODO: Retrieve ALL shoes !!!!
shoes = new Shoes()
shoes.getShoesInUse(@.parallel())
@@ -52,7 +53,8 @@ renderViewForEditRun = (runId, response, validationErrors) ->
throw new errors.DataError('An error occured while loading data for the edit run page.', error)
if validationErrors
- run = mapRunFromErroneousInput(response.locals(), runId)
+ run = new FormDataToRunMapper().mapFrom(response.locals())
+ run['id'] = runId
response.render('runs/edit',
run: run
@@ -64,28 +66,7 @@ renderViewForEditRun = (runId, response, validationErrors) ->
applyChangesTo = (run, formData) ->
run.date = formData.date
run.distance = formData.distance
- run.duration = createDurationFrom(formData)
+ run.duration = new FormDataToRunMapper().mapDurationFrom(formData)
run.averageHeartRate = formData.averageHeartRate
run.shoes = formData.shoes
- run.comments = formData.comments
-
-#TODO: refactor into seperate module!!
-mapRunFromErroneousInput = (formData, runId) ->
- date: formData.date
- distance: formData.distance
- duration: createDurationFrom(formData)
- id: runId
- averageHeartRate : formData.averageHeartRate
- shoes: formData.shoes
- comments: formData.comments
-
-createDurationFrom = (formData) ->
- hours: formData.durationHours
- minutes: formData.durationMinutes
- seconds: formData.durationSeconds
-
-# TODO: refactor into domain object??
-calculateSpeedFor = (newRun) ->
- durationInSeconds = (newRun.duration.hours * 3600) + (newRun.duration.minutes * 60) + newRun.duration.seconds
- speed = (newRun.distance / durationInSeconds) * 3600
- return parseFloat(speed.toFixed(2))
+ run.comments = formData.comments
View
@@ -1,6 +1,6 @@
step = require('step')
-errors = require('./../../errors')
-Runs = require('./../../data/runs')
+errors = require('../../errors')
+Runs = require('../../data/runs')
exports.index = (request, response) ->
year = determineYearFrom(request)
@@ -12,7 +12,7 @@ exports.index = (request, response) ->
runs.getRunsByYear(year, @.parallel())
renderView = (error, numberOfRunsPerYear, runs) ->
- if(error)
+ if error
throw new errors.DataError('An error occured while loading data for the index page (runs).', error)
response.render('runs/index',
@@ -24,7 +24,7 @@ exports.index = (request, response) ->
determineYearFrom = (request) ->
year = request.params.year
- if(year)
+ if year
parseInt(year, 0)
else
new Date().getFullYear()
View
@@ -1,8 +1,10 @@
step = require('step')
-errors = require('./../../errors')
-_ = require('underscore')
-Runs = require('./../../data/runs')
-Shoes = require('./../../data/shoes')
+errors = require('../../errors')
+RunFactory = require('../../factories/runfactory')
+FormDataToRunMapper = require('../../mappers/runmapper')
+Runs = require('../../data/runs')
+Shoes = require('../../data/shoes')
+Calculator = require('../../services/calculator')
exports.new = (request, response) ->
renderViewForNewRun(response)
@@ -14,8 +16,8 @@ exports.create = (request, response) ->
step(
createRun = () ->
- newRun = mapRunFromInput(request.form)
- newRun.speed = calculateSpeedFor(newRun)
+ newRun = new FormDataToRunMapper().mapFrom(request.form)
+ newRun.speed = new Calculator().calculateSpeedFor(newRun)
runs = new Runs()
runs.save(newRun, @)
@@ -37,45 +39,13 @@ renderViewForNewRun = (response, validationErrors) ->
if error
throw new errors.DataError('An error occured while loading data for the new run page.', error)
- run = createDefaultRun()
+ run = new RunFactory().createDefault()
if validationErrors
- run = mapRunFromInput(response.locals())
+ run = new FormDataToRunMapper().mapFrom(response.locals())
response.render('runs/new',
run: run
shoesInUse: shoesInUse or []
validationErrors: validationErrors or {}
)
)
-
-# TODO: Create factory
-createDefaultRun = () ->
- type: 'run'
- date: _.getCurrentDate()
- distance: ''
- duration:
- hours: ''
- minutes: ''
- seconds: ''
- averageHeartRate: ''
- shoes: -1
- comments: ''
-
-mapRunFromInput = (formData) ->
- type: 'run'
- date: formData.date
- distance: formData.distance
- duration: createDurationFrom(formData)
- averageHeartRate : formData.averageHeartRate
- shoes: formData.shoes
- comments: formData.comments
-
-createDurationFrom = (formData) ->
- hours: formData.durationHours
- minutes: formData.durationMinutes
- seconds: formData.durationSeconds
-
-calculateSpeedFor = (newRun) ->
- durationInSeconds = (newRun.duration.hours * 3600) + (newRun.duration.minutes * 60) + newRun.duration.seconds
- speed = (newRun.distance / durationInSeconds) * 3600
- return parseFloat(speed.toFixed(2))
@@ -0,0 +1,5 @@
+module.exports = class Calculator
+ calculateSpeedFor: (run) ->
+ durationInSeconds = (run.duration.hours * 3600) + (run.duration.minutes * 60) + run.duration.seconds
+ speed = (run.distance / durationInSeconds) * 3600
+ return parseFloat(speed.toFixed(2))

0 comments on commit 8976363

Please sign in to comment.