Skip to content

Commit

Permalink
created route to add a product
Browse files Browse the repository at this point in the history
  • Loading branch information
JaberHPranto committed Jul 12, 2021
1 parent f19048d commit d751b14
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
Binary file added client/public/images/sample.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 51 additions & 2 deletions server/controller/productController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import User from '../models/userModel.js'
export const getProducts = asyncHandler(async (req, res) => {

// for pagination
const pageSize = 8
const pageSize = 12
const page = req.query.pageNumber || 1

const keyword = req.query.keyword ? {
Expand Down Expand Up @@ -80,6 +80,7 @@ export const createProductReview = asyncHandler(async (req, res) => {
}
})

/* ADMIN */

// getting a product @route -> api/products/:id
export const deleteProductById = (async (req, res) => {
Expand All @@ -88,6 +89,54 @@ export const deleteProductById = (async (req, res) => {
await product.remove()
res.status(200).json({message:'Product deleted successfully'})
} catch(err){
res.status(404).json({message:"No product found"})
res.status(404).json({error:"No product found"})
}
})

// Crating a product -> api/products (POST)
export const createProduct = (async (req, res) => {
try {
const product = new Product({
name: 'Sample Product',
description: 'Sample description',
user: req.userId,
image: '/images/sample.jpg',
category: 'Sample category',
price: 0,
countInStock: 0,
numReviews:0
})
const createdProduct = await product.save()
res.status(201).json(createdProduct)
} catch (err) {
console.log(error);
res.status(404).json({message:"Failed to create a product"})
}
})


// Update a product -> api/products/:id (PUT)
export const updateProduct = (async (req, res) => {
try {
const { name, description, image, category, price, countInStock } = req.body

const product = await Product.findById(req.params.id)
if (product) {
product.name= name,
product.description= description,
product.image= image,
product.category= category,
product.price= price,
product.countInStock=countInStock

const updatedProduct = await product.save()
return res.status(201).json(updatedProduct)

} else {
return res.status(401).json({message:"Product not found"})
}
} catch (err) {
console.log(error);
res.status(404).json({message:"Failed to create a product"})
}
})
6 changes: 4 additions & 2 deletions server/routes/productRoutes.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import express from 'express'
import { createProductReview, deleteProductById, getProductById, getProducts } from '../controller/productController.js'
import { createProduct, createProductReview, deleteProductById, getProductById, getProducts, updateProduct } from '../controller/productController.js'
import { isAdmin, isLoggedIn } from '../middlewares/authMiddleware.js'

const router = express.Router()

router.get("/",getProducts)
router.get("/:id", getProductById)
router.post("/:id/reviews",isLoggedIn,createProductReview)
router.post("/",isLoggedIn,isAdmin,createProduct)
router.put("/:id",isLoggedIn,isAdmin,updateProduct)
router.post("/:id/reviews", isLoggedIn, createProductReview)
router.delete("/:id",isLoggedIn, isAdmin,deleteProductById)

export default router

0 comments on commit d751b14

Please sign in to comment.