File size: 1,401 Bytes
29a937c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const Order = require('../models/orderModel');
const catchAsync = require('../utils/catchAsync');

// GET ALL ORDERS
exports.getAllOrders = catchAsync(async (req, res) => {
  const orders = await Order.find().populate('user').populate('items.product');

  res.status(200).json({
    status: 'success',
    results: orders.length,
    data: { orders },
  });
});

// GET ONE ORDER
exports.getOrder = catchAsync(async (req, res) => {
  const order = await Order.findById(req.params.id)
    .populate('user')
    .populate('items.product');

  if (!order) {
    return res.status(404).json({
      status: 'fail',
      message: 'Order not found',
    });
  }

  res.status(200).json({
    status: 'success',
    data: { order },
  });
});

// CREATE ORDER
exports.createOrder = catchAsync(async (req, res) => {
  const order = await Order.create(req.body);

  res.status(201).json({
    status: 'success',
    data: { order },
  });
});

// UPDATE ORDER
exports.updateOrder = catchAsync(async (req, res) => {
  const order = await Order.findByIdAndUpdate(req.params.id, req.body, {
    new: true,
    runValidators: true,
  });

  res.status(200).json({
    status: 'success',
    data: { order },
  });
});

// DELETE ORDER
exports.deleteOrder = catchAsync(async (req, res) => {
  await Order.findByIdAndDelete(req.params.id);

  res.status(204).json({
    status: 'success',
    data: null,
  });
});