建立 Get

路由建立

// routes/index.js

const orderController = require('../controllers/orderController.js')

router.get('/orders', orderController.getOrders)

controller 建立

// controllers/orderController.js

const db = require('../models')
const Order = db.Order

const orderController = {
  // 取得所有訂單
  getOrders: async (req, res) => {
    try {
      const orders = await Order.findAll({ include: 'items' })
      return res.render('orders', {
        orders
      })
    } catch (e) {
      console.log(e)
    }
  }
}

module.exports = orderController

views 建立

// views/orders.hbs

{{#each orders}}
    <div class="row">
        <div class="col-md-5">
            <table class="table">
                <thead>
                <tr>
                    <th scope="col">Product</th>
                    <th scope="col">quantity</th>
                    <th scope="col">Subtotal</th>
                </tr>
                </thead>
                <tbody>
                {{#each this.items}}
                    <tr>
                        <td>{{this.name}}</td>
                        <td>{{this.OrderItem.quantity}}</td>
                        <td>{{this.price}}</td>
                    </tr>
                {{/each}}
                <tr>
                    <td></td>
                    <td style="text-align: right;"><h4>Total: </h4></td>
                    <td><h4>{{this.amount}}</h4></td>
                </tr>
                </tbody>
            </table>
        </div>

        <div>
            <ul>
                <li>{{this.id}}</li>
                <li>name: {{this.name}}</li>
                <li>address: {{this.address}}</li>
                <li>phone: {{this.phone}}</li>
                <li>shipping_status: {{this.shipping_status}}</li>
                <li>payment_status: {{this.payment_status}}</li>
            </ul>

            <div style="display: flex; justify-content: space-around;">
                <form action="/order/{{this.id}}/cancel" method="POST">
                    <button type="submit" class="btn btn-primary">取消訂單</button>
                </form>

                <a href="/order/{{this.id}}/payment"><button class="btn btn-primary">立即付款</button></a>
            </div>
        </div>
        
    </div>

    <hr/>
{{/each}}

建立 post cancel

建立路由

// routes/index.js

router.post('/order', orderController.postOrder)
router.post('/order/:id/cancel', orderController.cancelOrder)

建立 controller

// controllers/orderController.js

const OrderItem = db.OrderItem
const Cart = db.Cart

// 建立訂單
  postOrder: async (req, res) => {
    try {
      const cart = await Cart.findByPk(req.body.cartId, { include: 'items' })
      const order = await Order.create({
        name: req.body.name,
        address: req.body.address,
        phone: req.body.phone,
        shipping_status: req.body.shipping_status,
        payment_status: req.body.payment_status,
        amount: req.body.amount
      })
      const results = []
      for (let i = 0; i < cart.items.length; i++) {
        const orderItem = await OrderItem.create({
          OrderId: order.id,
          ProductId: cart.items[i].id,
          price: cart.items[i].price,
          quantity: cart.items[i].CartItem.quantity
        })
        results.push(orderItem)
      }
      return res.redirect('/orders')
    } catch (e) {
      console.log(e)
    }
  },
  // 刪除訂單
  cancelOrder: async (req, res) => {
    try {
      const order = await Order.findByPk(req.params.id)
      order.update({
        ...req.body,
        shipping_status: '-1',
        payment_status: '-1'
      })
      return res.redirect('back')
    } catch (e) {
      console.log(e)
    }
  }