Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use('/', routes);
app.use('/api/v1', routes);

module.exports = app;
2 changes: 1 addition & 1 deletion src/controllers/categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CategoriesController {
async deleteCategories(req, res) {
const { categoryIds } = req.body;
const result = await categoriesService.deleteCategories(categoryIds);
res.status(httpStatus.OK).send(result);
res.status(httpStatus.NO_CONTENT).send(result);
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/controllers/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const productService = new ProductService();

class ProductController {
async searchProducts(req, res) {
const { q, category, seller, page, limit } = req.query;
const result = await productService.searchProducts(q, category, seller, parseInt(page), parseInt(limit));
res.status(httpStatus.CREATED).send(result);
const { product, category, seller, page, limit } = req.query;
const result = await productService.searchProducts(product, category, seller, parseInt(page), parseInt(limit));
res.status(httpStatus.OK).send(result);
}
async getProduct(req, res) {
const { productId } = req.params;
Expand All @@ -23,7 +23,7 @@ class ProductController {
try {
// This service will always be failed
const result = await productService.createProduct();
res.status(httpStatus.CREATED).send(result);
res.status(httpStatus.OK).send(result);
} catch (error) {
res
.status(httpStatus.INTERNAL_SERVER_ERROR)
Expand All @@ -37,9 +37,9 @@ class ProductController {
async deleteProduct(req, res) {
try {
// This service will always be failed
const { productId } = req.body;
const { productId } = req.params;
const result = await productService.deleteProduct(productId);
res.status(httpStatus.OK).send(result);
res.status(httpStatus.NO_CONTENT).send(result);
} catch (error) {
res
.status(httpStatus.INTERNAL_SERVER_ERROR)
Expand Down
8 changes: 5 additions & 3 deletions src/controllers/sellers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ const sellerService = new SellerService();

class SellerController {
async blockSeller(req, res) {
const result = await sellerService.blockSeller();
res.status(httpStatus.NO_CONTENT).send(result);
const { sellerId } = req.params;
const result = await sellerService.blockSeller(sellerId);
res.status(httpStatus.OK).send(result);
}
async searchSeller(req, res) {
const result = await sellerService.searchSeller();
const { seller, page, limit } = req.query;
const result = await sellerService.searchSeller(seller, parseInt(page), parseInt(limit));
res.status(httpStatus.OK).send(result);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,31 @@ router.get('/products/:productId', productController.getProduct);
/*
* An endpoint to update the name of the product
*/
router.put('/products/:productId', productController.updateProduct);
router.patch('/products/:productId', productController.updateProduct);

/*
* An endpoint to create a product
*/
router.post('/create-products', productController.createProduct);
router.post('/products', productController.createProduct);

/*
* An endpoint to delete a product
*/
router.delete('/products', productController.deleteProduct);
router.delete('/products/productId', productController.deleteProduct);

/*
* An endpoint to bulk delete categories
*/
router.post('/categories/delete', categoriesController.deleteCategories);
router.delete('/categories', categoriesController.deleteCategories);

/*
* An endpoint to allow admin to blacklist a seller
*/
router.put('/update-seller-block-status', sellerController.blockSeller);
router.patch('/sellers/block', sellerController.blockSeller);

/*
* An endpoint to search sellers based on seller name and product name
*/
router.post('/sellers', sellerController.searchSeller);
router.get('/sellers', sellerController.searchSeller);

module.exports = router;
2 changes: 1 addition & 1 deletion src/services/products.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const products = require("../mocks/products")

class ProductService {
searchProducts(q, category, seller, page, limit) {
searchProducts(product, category, seller, page, limit) {
return {
items: products,
pagination: {
Expand Down
11 changes: 9 additions & 2 deletions src/services/sellers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ class SellerService {
blockSeller() {
return {};
}
searchSeller() {
return {};
searchSeller(seller, page, limit) {
return {
items: seller,
pagination: {
total: 10,
page: page,
limit: limit,
}
};
}
}

Expand Down
10 changes: 5 additions & 5 deletions tests/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ describe("products", () => {
});

it('should be able to update product name', async () => {
const { body, status } = await request(app).put(`/products/0`).send({ name: "Samsung S10" });
const { body, status } = await request(app).patch(`/products/0`).send({ name: "Samsung S10" });
expect(status).toBe(httpStatus.OK);
expect(body).toEqual(products[0]);
});

it('should be returning error when creating a product', async () => {
const { body, status } = await request(app).post(`/create-products`).send({
const { body, status } = await request(app).post(`/products`).send({
name: "Samsung S10",
categories: ["Phones", "Mobile Devices"],
seller: "Samsung"
Expand Down Expand Up @@ -60,7 +60,7 @@ describe("products", () => {

describe("categories", () => {
it('should be able to bulk delete categories', async () => {
const { body, status } = await request(app).post(`/categories/delete`).send({
const { body, status } = await request(app).delete(`/categories`).send({
categoryIds: [1, 2, 3, 4, 5]
});
expect(status).toBe(httpStatus.OK);
Expand All @@ -70,15 +70,15 @@ describe("categories", () => {

describe("sellers", () => {
it('should be able to blacklist a seller', async () => {
const { body, status } = await request(app).put(`/update-seller-block-status`).send({
const { body, status } = await request(app).patch(`/sellers/block`).send({
sellerId: 2,
blacklisted: true
});
expect(status).toBe(httpStatus.NO_CONTENT);
expect(body).toEqual({});
});
it('should be able to search sellers', async () => {
const { body, status } = await request(app).post(`/sellers`).send({
const { body, status } = await request(app).get(`/sellers`).send({
seller: "Samsung"
});
expect(status).toBe(httpStatus.OK);
Expand Down