const express = require('express')
const router = express.Router()
const transactions = require('../../controllers/transactions')
const importData = require('../../config/import')
const moment = require('moment')

router.get('/', get)
router.get('/:category', get)
router.post('/new', post)
router.put('/update', put)
router.delete('/delete/:id', remove)

router.get('/totals/:start_date/:end_date', getTotals)
router.post('/import', importTransactions)

module.exports = router

function get(req, res, next) {
  transactions.get(req.params.category)
    .then(function(results) {
      res.send(results)
    }).catch(next)
}

function post(req, res, next) {
  transactions.create(req.body)
    .then(function() {
      res.redirect('/')
    }).catch(next)
}

function put(req, res, next) {
  transactions.edit(req.body)
    .then(function(transaction) {
      res.send(transaction)
    }).catch(next)
}

function remove(req, res, next) {
  const id = req.params.id
  transactions.remove(id)
    .then(function() {
      res.send({
        status: 200,
        message: 'Successfully removed transaction' + id
      })
    }).catch(next)
}

function getTotals(req, res, next) {
  const startDate = moment(req.params.start_date).toDate()
  const endDate = moment(req.params.end_date).toDate()

  transactions.totals(startDate, endDate)
    .then(res.send)
    .catch(next)
}

function importTransactions(req, res, next) {
  const separator = req.body.separator
  const data = req.body.data

  importData(separator, data)
    .catch(next)
    .then(function() {
      res.redirect('/')
    })
}

@@ -558,33 +558,18 @@
 hen(
-function(results) %7B%0A     
+results =%3E
  res
@@ -582,23 +582,22 @@
 results)
+)
 %0A    
-%7D)
 .catch(n
@@ -678,34 +678,21 @@
   .then(
-function() %7B%0A     
+() =%3E
  res.red
@@ -701,23 +701,22 @@
 ect('/')
+)
 %0A    
-%7D)
 .catch(n
@@ -794,25 +794,16 @@
   .then(
-function(
 transact
@@ -805,25 +805,19 @@
 nsaction
-) %7B%0A     
+ =%3E
  res.sen
@@ -830,23 +830,22 @@
 saction)
+)
 %0A    
-%7D)
 .catch(n
@@ -949,34 +949,21 @@
   .then(
-function() %7B%0A     
+() =%3E
  res.sen
@@ -966,18 +966,16 @@
 .send(%7B%0A
-  
       st
@@ -985,18 +985,16 @@
 s: 200,%0A
-  
       me
@@ -1048,19 +1048,16 @@
     
-  
 %7D
+)
 )%0A    
-%7D)
 .cat
@@ -1478,26 +1478,13 @@
 hen(
-function() %7B%0A     
+() =%3E
  res
@@ -1497,18 +1497,12 @@
 ect('/')
-%0A    %7D
 )%0A%7D%0A

use arrow functions in transactions api router