{"size": 2503, "ext": "js", "max_stars_repo_path": "server/order.js", "max_stars_repo_name": "dlaveman/ReqDev", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/order.js", "max_issues_repo_name": "dlaveman/ReqDev", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/order.js", "max_forks_repo_name": "dlaveman/ReqDev", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.6276595745, "max_line_length": 74, "alphanum_fraction": 0.5169796245}
'use strict'

const db = require('APP/db')
// const User = db.model('users')
// const Cart = db.model('carts')
const Order = db.model('orders')
const OrderItem = db.model('order_item')
const Developer = db.model('developers')

module.exports = require('express').Router()
  .get('/:orderId', (req, res, next) => {
    OrderItem.findAll({
      include: [{
        model: Order,
        where: {
          id: req.params.orderId
        }
      }, {
        model: Developer
      }]
    })
    .then(orders => res.json(orders))
    .catch(next)
  })
  .get('/', (req, res, next) =>
    Order.findAll({
      where: {
        user_id: req.user.id
      }
    })
      .then(orders => res.json(orders))
      .catch(next))
// Pass in object with submit time, cart[]
  .post('/', (req, res, next) =>
    Order.create(
      req.body.submitTime
    )
      .then(order => {
        req.body.cart.forEach(item => item.order_id = order.dataValues.id)
        console.log('Order.create cart----', req.body.cart)
        //order.dataValues.id
        OrderItem.bulkCreate(req.body.cart)
      })
      .then(orderItem => {
        //console.log('orderItem======', orderItem)
        res.status(201).json(orderItem)
      })
      .catch(next))

  // .put('/:orderId', (req, res, next) =>
  //   Order.update(req.body, {
  //     where: { id: req.params.orderId },
  //     returning: true
  //   })
  //     .then(res => Order.findOne({
  //       where: { id: res[1][0].id },
  //       include: [{ model: Developer }],
  //     }))
  //     .then(order => { res.json(order) })
  //     .catch(next))

  // .delete('/', (req, res, next) => {
  //   Order.destroy({
  //     where: { user_id: req.query.user }
  //   })
  //     .catch(next)
  // })

  // .delete('/:orderId', (req, res, next) => {
  //   Order.destroy({
  //     where: { id: req.params.orderId }
  //   })
  //     .catch(next)
  // })

// 'use strict'

// const db = require('APP/db')
// const Order = db.model('orders')

// module.exports = require('express').Router()
//   .get('/:userId', (req, res, next) =>
//     Order.findAll({ where: { user_id: req.params.userId } })
//       .then(orders => res.json(orders))
//       .catch(next))
//   .post('/', (req, res, next) =>
//     Order.create(req.body)
//       .then(order => res.status(201).json(order))
//       .catch(next))
//   .get('/:userId/:orderId', (req, res, next) =>
//     Order.findById(req.params.orderId)
//       .then(order => res.json(order))
//       .catch(next))
