Skip to content

Commit

Permalink
Merge fd787fb into 04d276d
Browse files Browse the repository at this point in the history
  • Loading branch information
akhilome committed Sep 7, 2018
2 parents 04d276d + fd787fb commit a80979f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 12 deletions.
23 changes: 23 additions & 0 deletions server/controllers/orderController.js
Expand Up @@ -33,6 +33,29 @@ class OrderController {
order,
});
}

static updateOrder(req, res) {
const { orderIndex } = req;
const { status } = req.body;

if (orderIndex === -1) {
return res.status(404).json({
error: 'no such order exists',
});
}

if (!status) {
return res.status(400).json({
error: 'no new status specified',
});
}

orders[orderIndex].status = status;
return res.status(201).json({
message: 'order status updated successfully',
order: orders[orderIndex],
});
}
}

export default OrderController;
1 change: 1 addition & 0 deletions server/middleware/findSingleOrder.js
Expand Up @@ -4,6 +4,7 @@ const findOrder = (req, res, next) => {
let { id } = req.params;
id = Number(id);
req.order = orders.find(order => order.id === id);
req.orderIndex = orders.findIndex(order => order.id === id);
next();
};

Expand Down
1 change: 1 addition & 0 deletions server/routes/routes.js
Expand Up @@ -13,5 +13,6 @@ router.get('/', (req, res) => {
router.get('/orders', OrderController.getAllOrders);
router.get('/orders/:id', findOrder, OrderController.getOrder);
router.post('/orders', OrderController.newOrder);
router.put('/orders/:id', findOrder, OrderController.updateOrder);

export default router;
60 changes: 48 additions & 12 deletions tests/routes/routes.spec.js
Expand Up @@ -8,6 +8,13 @@ import orders from '../../server/db/orders';
chai.use(chaiHttp);
chai.use(dirtyChai);

const id = {
valid: Math.ceil(Math.random() * orders.length),
invalid: null,
};

const keys = ['id', 'author', 'title', 'status', 'date'];

describe('GET /api/v1/', () => {
it('should respond with a 200 status code', (done) => {
chai.request(app)
Expand All @@ -29,7 +36,6 @@ describe('GET /api/v1/', () => {
});

describe('GET /api/v1/orders/', () => {
const keys = ['id', 'author', 'title', 'status', 'date'];
it('should respond with status 200', (done) => {
chai.request(app)
.get('/api/v1/orders/')
Expand Down Expand Up @@ -60,17 +66,6 @@ describe('GET /api/v1/orders/', () => {
});

describe('GET /api/v1/orders/<orderId>', () => {
const id = {
valid: undefined,
invalid: undefined,
};
const keys = ['id', 'author', 'title', 'status', 'date'];

before(() => {
id.valid = Math.ceil(Math.random() * orders.length);
id.invalid = orders.length + 1;
});

it('should respond with status 200 if order is found', (done) => {
chai.request(app)
.get(`/api/v1/orders/${id.valid}`)
Expand Down Expand Up @@ -126,3 +121,44 @@ describe('POST /api/v1/orders', () => {
});
});
});

describe('PUT /api/v1/orders/<orderId>', () => {
it('should respond with an error if no status is provided', (done) => {
chai.request(app)
.put(`/api/v1/orders/${id.valid}`)
.send({ })
.end((err, res) => {
res.should.have.status(400);
res.should.be.an('object').which.has.a.property('error');
done();
});
});

it('should respond with an error if no order with provided id exists', (done) => {
chai.request(app)
.put(`/api/v1/orders/${id.invalid}`)
.send({
status: 'done',
})
.end((err, res) => {
res.should.have.status(404);
res.should.be.an('object').which.has.a.property('error');
done();
});
});

it('should update the order and return a success message and the updated order', (done) => {
chai.request(app)
.put(`/api/v1/orders/${id.valid}`)
.send({
status: 'completed',
})
.end((err, res) => {
res.should.have.status(201);
res.body.should.be.an('object').with.all.keys('message', 'order');
res.body.order.should.have.property('status');
res.body.order.status.should.eql('completed');
done();
});
});
});

0 comments on commit a80979f

Please sign in to comment.