Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Added create category functionality #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
120 changes: 120 additions & 0 deletions backend/controllers/categoryController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
const asyncHandler = require('express-async-handler')
const Category = require('../models/categoryModel')
const Product = require('../models/productModel')

// @desc Fetch all categories
// @route GET /api/categories
// @access Public
const getCategories = asyncHandler(async (req, res) => {
const categories = await Category.find({})
res.json(categories)
})

// @desc Fetch single category
// @route GET /api/categories/:id
// @access Public
const getCategoryById = asyncHandler(async (req, res) => {
const category = await Category.findById(req.params.id)
if (category) {
res.json(category)
} else {
res.status(404)
throw new Error('Category not found')
}
})

// @desc Delete a category
// @route DELETE /api/categories/:id
// @access Private/Admin
const deleteCategory = asyncHandler(async (req, res) => {
const category = await Category.findById(req.params.id)

if (category) {
await category.remove()
res.json({ message: 'Category removed' })
} else {
res.status(404)
throw new Error('Category not found')
}
})

// @desc Create a category
// @route POST /api/categories
// @access Private/Admin
const createCategory = asyncHandler(async (req, res) => {
const {
name,
imageSrc,
imageAlt,
} = req.body
const category = new Category({
user: req.user._id,
name,
imageSrc,
imageAlt,
href:"#"
})

try {
const createdCategory = await category.save()

res.status(201).json(createdCategory)
} catch (error) {
if (error.name === 'ValidationError') {
let errors = ''
Object.keys(error.errors).forEach((key) => {
errors += error.errors[key].message + '.\n'
})
res.status(500).json(errors)
}
}
})

// @desc Update a category
// @route PUT /api/categories/:id
// @access Private/Admin
const updateCategory = asyncHandler(async (req, res) => {
const {
name,
imageSrc,
imageAlt,
} = req.body
console.log(name)
const category = await Category.findById(req.params.id)

if (category) {
category.name = name
category.imageSrc = imageSrc
category.imageAlt = imageAlt

const updatedCategory = await category.save()
res.json(updatedCategory)
} else {
res.status(404)
throw new Error('Category not found')
}
})

// @desc Fetch products by category
// @route GET /api/categories/:category
// @access Public
const getProductsByCategory = asyncHandler(async (req, res) => {
const categorywiseProducts = await Product.find({
category: req.params.category,
})
if (categorywiseProducts) {
res.json(categorywiseProducts)
} else {
res.status(404)
throw new Error('Products not found')
}
})

module.exports = {
getCategories,
getCategoryById,
getProductsByCategory,
deleteCategory,
createCategory,
updateCategory,
}
46 changes: 21 additions & 25 deletions backend/routes/categoryRoutes.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
const express = require('express')
const asyncHandler = require('express-async-handler')
const router = express.Router()
const Product = require('../models/productModel')
const Category = require('../models/categoryModel')
const {
getCategories,
getCategoryById,
getProductsByCategory,
createCategory,
deleteCategory,
updateCategory,
} = require('../controllers/categoryController')
const { protect, admin } = require('../middleware/authMiddleware')

// @desc Fetch all categories
// @route GET /api/categories
// @access Public
router.route('/').get(getCategories).post(protect, admin, createCategory)

router.get(
'/',
asyncHandler(async (req, res) => {
const categories = await Category.find({})
res.json(categories)
}),
)
// @desc Fetch category by id
// @route GET /api/categories/category/:id
// @route PUT /api/categories/category/:id
// @route DELETE /api/categories/category/:id
// @access Private/Admin
router
.route('/category/:id')
.get(protect, admin, getCategoryById)
.delete(protect, admin, deleteCategory)
.put(protect, admin, updateCategory)

// @desc Fetch products based on category
// @route GET /api/categories/:category
// @access Public

router.get(
'/:category',
asyncHandler(async (req, res) => {
const categorywiseProducts = await Product.find({
category: req.params.category,
})
// const categorywiseProducts = products.filter((p) => p.category === req.params.category)
if (categorywiseProducts) {
res.json(categorywiseProducts)
} else {
res.status(404)
throw new Error('Products not found')
}
}),
)
router.route('/:category').get(getProductsByCategory)

module.exports = router
12 changes: 11 additions & 1 deletion frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ import OrderListScreen from './screens/OrderListScreen'
import SearchScreen from './screens/SearchScreen'
import CarouselListScreen from './screens/CarouselListScreen'
import CarouselEditScreen from './screens/CarouselEditScreen'
import CategoryEditScreen from './screens/CategoryEditScreen'
import CreateCarouselScreen from './screens/CreateCarouselScreen'
import { CategoryListScreen } from './screens/CategoryListScreen'
import CategoryListScreen from './screens/CategoryListScreen'
import CreateCategoryScreen from './screens/CreateCategoryScreen'
import Error404Screen from './screens/Error404Screen'
import Terms from './pages/Terms'
import Returns from './pages/Returns'
Expand Down Expand Up @@ -81,6 +83,14 @@ function App() {
path="/admin/carousel/:id/edit"
component={CarouselEditScreen}
/>
<Route
path="/admin/createcategory"
component={CreateCategoryScreen}
/>
<Route
path="/admin/category/:id/edit"
component={CategoryEditScreen}
/>
<Route path="/admin/categorylist" component={CategoryListScreen} />
<Route path="/terms" component={Terms} />
<Route path="/returns" component={Returns} />
Expand Down
148 changes: 148 additions & 0 deletions frontend/src/actions/categoryActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,22 @@ import {
CATEGORY_LIST_REQUEST,
CATEGORY_LIST_SUCCESS,
CATEGORY_LIST_FAIL,
CATEGORY_DELETE_REQUEST,
CATEGORY_DELETE_SUCCESS,
CATEGORY_DELETE_FAIL,
CATEGORY_CREATE_REQUEST,
CATEGORY_CREATE_SUCCESS,
CATEGORY_CREATE_FAIL,
CATEGORY_UPDATE_REQUEST,
CATEGORY_UPDATE_SUCCESS,
CATEGORY_UPDATE_FAIL,
CATEGORY_DETAILS_REQUEST,
CATEGORY_DETAILS_SUCCESS,
CATEGORY_DETAILS_FAIL
} from '../constants/categoryConstants'

import { logout } from './userActions'

export const listCategories = () => async (dispatch) => {
try {
dispatch({ type: CATEGORY_LIST_REQUEST })
Expand All @@ -21,3 +35,137 @@ export const listCategories = () => async (dispatch) => {
})
}
}

export const listCategoryDetails = (id) => async (dispatch) => {
try {
dispatch({ type: CATEGORY_DETAILS_REQUEST })
const { data } = await axios.get(`/api/categories/category/${id}`)
console.log(data);
dispatch({ type: CATEGORY_DETAILS_SUCCESS, payload: data })
} catch (error) {
dispatch({
type: CATEGORY_DETAILS_FAIL,
payload:
error.response && error.response.data.message
? error.response.data.message
: error.message,
})
}
}

export const deleteCategory = (id) => async (dispatch, getState) => {
try {
dispatch({
type: CATEGORY_DELETE_REQUEST,
})

const {
userLogin: { userInfo },
} = getState()

const config = {
headers: {
Authorization: `Bearer ${userInfo.token}`,
},
}

await axios.delete(`/api/categories/category/${id}`, config)

dispatch({
type: CATEGORY_DELETE_SUCCESS,
})
} catch (error) {
const message =
error.response && error.response.data.message
? error.response.data.message
: error.message
if (message === 'Not authorized, token failed') {
dispatch(logout())
}
dispatch({
type: CATEGORY_DELETE_FAIL,
payload: message,
})
}
}

export const createCategory = (category) => async (dispatch, getState) => {
try {
dispatch({
type: CATEGORY_CREATE_REQUEST,
})

const {
userLogin: { userInfo },
} = getState()

const config = {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${userInfo.token}`,
},
}
console.log(category);
const { data } = await axios.post(`/api/categories`, category, config)

dispatch({
type: CATEGORY_CREATE_SUCCESS,
payload: data,
})
} catch (error) {
const message =
error.response && error.response.data
? error.response.data
: error.message
if (message === 'Not authorized, token failed') {
dispatch(logout())
}
dispatch({
type: CATEGORY_CREATE_FAIL,
payload: message,
})
}
}

export const updateCategory = (category) => async (dispatch, getState) => {
try {
dispatch({
type: CATEGORY_UPDATE_REQUEST,
})

const {
userLogin: { userInfo },
} = getState()

const config = {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${userInfo.token}`,
},
}

const { data } = await axios.put(
`/api/categories/category/${category._id}`,
category,
config,
)

dispatch({
type: CATEGORY_UPDATE_SUCCESS,
payload: data,
})
dispatch({ type: CATEGORY_DETAILS_SUCCESS, payload: data })
} catch (error) {
const message =
error.response && error.response.data.message
? error.response.data.message
: error.message
if (message === 'Not authorized, token failed') {
dispatch(logout())
}
dispatch({
type: CATEGORY_UPDATE_FAIL,
payload: message,
})
}
}
20 changes: 20 additions & 0 deletions frontend/src/constants/categoryConstants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
export const CATEGORY_LIST_REQUEST = 'CATEGORY_LIST_REQUEST'
export const CATEGORY_LIST_SUCCESS = 'CATEGORY_LIST_SUCCESS'
export const CATEGORY_LIST_FAIL = 'CATEGORY_LIST_FAIL'

export const CATEGORY_DELETE_REQUEST = 'CATEGORY_DELETE_REQUEST'
export const CATEGORY_DELETE_SUCCESS = 'CATEGORY_DELETE_SUCCESS'
export const CATEGORY_DELETE_FAIL = 'CATEGORY_DELETE_FAIL'

export const CATEGORY_CREATE_REQUEST = 'CATEGORY_CREATE_REQUEST'
export const CATEGORY_CREATE_SUCCESS = 'CATEGORY_CREATE_SUCCESS'
export const CATEGORY_CREATE_FAIL = 'CATEGORY_CREATE_FAIL'
export const CATEGORY_CREATE_RESET = 'CATEGORY_CREATE_RESET'

export const CATEGORY_UPDATE_REQUEST = 'CATEGORY_UPDATE_REQUEST'
export const CATEGORY_UPDATE_SUCCESS = 'CATEGORY_UPDATE_SUCCESS'
export const CATEGORY_UPDATE_FAIL = 'CATEGORY_UPDATE_FAIL'
export const CATEGORY_UPDATE_RESET = 'CATEGORY_UPDATE_RESET'


export const CATEGORY_DETAILS_REQUEST = 'CATEGORY_DETAILS_REQUEST'
export const CATEGORY_DETAILS_SUCCESS = 'CATEGORY_DETAILS_SUCCESS'
export const CATEGORY_DETAILS_FAIL = 'CATEGORY_DETAILS_FAIL'
export const CATEGORY_DETAILS_RESET = 'CATEGORY_DETAILS_RESET'
Loading