0

I'd like to find a way to not repeat myself.

Most of my /post/ commands should update something then display the same contents as the /get/ command.

At present I am repeating myself like:

.get(function(req, res) {
  act({
    role: 'earnings',
    cmd: 'get',
    wc: req.params.wc,
    earner_id: req.params.earner_id,
  })
  .then(function(earnings) {
    return act({
      role: 'earnings',
      cmd: 'format',
      earnings: earnings,
      date: true,
      time: true,
      note: true,
      total: true
    })
  })
  .then(function(formatted_earnings) {
    if (formatted_earnings.length > 0)
      res.render( 'earnings-wc-earner', {
        formatted_earnings: formatted_earnings
      })
    else
      res.render('error', {
        message: "No earnings found."
      })
  })
  .catch(function (err) {
    res.send(err);
  })
})

.post(function(req, res) {
  act({
    role: 'earnings',
    cmd: 'add',
    wc: req.body.wc,
    earner_id: req.body.earner_id,
    datetime: req.body.datetime,
    earnings: req.body.earnings
  })
  .then(function(earnings) {
    return act({
      role: 'earnings',
      cmd: 'get',
      wc: req.params.wc,
      earner_id: req.params.earner_id,
    })
  .then(function(earnings) {
    // would love to redirect to get right about now
    // especially with a success message
    return act({
      role: 'earnings',
      cmd: 'format',
      earnings: earnings,
      date: true,
      time: true,
      note: true,
      total: true
    })
  })
  .then(function(formatted_earnings) {
    if (formatted_earnings.length > 0)
      res.render( 'earnings-wc-earner', {
        message: "Successfully added manual payment.",
        formatted_earnings: formatted_earnings
      })
    else
      res.render('error', {
        message: "No earnings found."
      })
  })
  .catch(function (err) {
    res.send(err);
  })
})

Is there anyway I can do something like redirect to get in the first .then chain of the post function?

Jack Robson
  • 2,184
  • 4
  • 27
  • 50
  • Coding everything in the same file? How about splitting your app in an established structure, such as Rails-like MVC. This would allow you to re-use model methods and keep things DRY – NodeNodeNode Mar 29 '17 at 15:32
  • No, I am splitting it into modules. This is one of the the routes file. – Jack Robson Mar 29 '17 at 17:23
  • Still, your flow logic is contained in your controllers (route handlers), whereas the body of a controller method should only include input/output formatting and calls to models. – NodeNodeNode Mar 29 '17 at 17:52
  • So if I abstract these functions out further, i.e. I put the flow logic into a seperate module would that work? – Jack Robson Mar 29 '17 at 18:08
  • If you create, for instance, a Model that implements a `create` method and a `find` method, you can chain those there. Your controller expects to receive the full payload as a result of `create`. Less logic in your controller – NodeNodeNode Mar 29 '17 at 19:05

1 Answers1

0

If its Express you can use .all() instead of .get() and .post().

In general you can just put that code in a separate module also. And I would switch to async/await and use other new JS features to make your code a bit cleaner.

Jason Livesay
  • 6,317
  • 3
  • 25
  • 31