Skip to content

Commit

Permalink
Refactoring comments for the controllers in the backend _improve docu…
Browse files Browse the repository at this point in the history
…mentation_
  • Loading branch information
DreamEcho100 committed May 5, 2022
1 parent e5357bf commit 183cdbf
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 57 deletions.
48 changes: 30 additions & 18 deletions backend/controllers/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { Request, Response } from 'express';
import asyncHandler from 'express-async-handler';
// import expressAsyncHandler from '../utils/core/express-async-handler';

// @desc Create new order
// @route POST /api/orders
// @access Private
/**
* @desc Create new order
* @route `POST` `/api/orders`
* @access Private
*/
const addOrderItems = asyncHandler(async (req: Request, res: Response) => {
const {
orderItems,
Expand Down Expand Up @@ -42,9 +44,11 @@ const addOrderItems = asyncHandler(async (req: Request, res: Response) => {
}
});

// @desc Get order by id
// @route GET /api/orders/:id
// @access Private
/**
* @desc Get order by id
* @route `GET` `/api/orders/:id`
* @access Private
*/
const getOrderById = asyncHandler(async (req: Request, res: Response) => {
const order = await OrderModel.findById(req.params.id).populate({
path: 'userRef',
Expand All @@ -59,9 +63,11 @@ const getOrderById = asyncHandler(async (req: Request, res: Response) => {
}
});

// @desc Update order to paid
// @route GET /api/orders/:id/pay
// @access Private
/**
* @desc Update order to paid
* @route `GET` `/api/orders/:id/pay`
* @access Private
*/
const updateOrderToPaid = asyncHandler(async (req: Request, res: Response) => {
const order = await OrderModel.findById(req.params.id);

Expand All @@ -84,19 +90,23 @@ const updateOrderToPaid = asyncHandler(async (req: Request, res: Response) => {
}
});

// @desc get user orders
// @route GET /api/orders/myOrders
// @access Private
/**
* @desc Get user orders
* @route `GET` `/api/orders/myOrders`
* @access Private
*/
const getMyOrders = asyncHandler(async (req: Request, res: Response) => {
const orders = await OrderModel.find({
userRef: req.user._id as unknown as typeof Schema.Types.ObjectId,
}).populate([{ path: 'userRef', select: 'name email' }]);
res.json(orders);
});

// @desc Get orders
// @route GET /api/orders
// @access Private/Admin
/**
* @desc Get orders
* @route `GET` `/api/orders`
* @access Private/Admin
*/
const getOrders = asyncHandler(async (req: Request, res: Response) => {
try {
const orders = await OrderModel.find({}).populate({
Expand All @@ -113,9 +123,11 @@ const getOrders = asyncHandler(async (req: Request, res: Response) => {
}
});

// @desc Delete order to paid
// @route GET /api/orders/:id/delivery
// @access Private/Admin
/**
* @desc Update order to paid
* @route `PUT` `/api/orders/:id/delivery`
* @access Private/Admin
*/
const updateOrderToDelivered = asyncHandler(
async (req: Request, res: Response) => {
const orderUpdated = await OrderModel.findByIdAndUpdate(
Expand Down
40 changes: 25 additions & 15 deletions backend/controllers/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import asyncHandler from 'express-async-handler';
import { itemsInObject } from '../utils/Object';
// import expressAsyncHandler from '../utils/core/express-async-handler';

// @desc Get products
// @route GET /api/products
// @access Public
/**
* @desc Get products
* @route `GET` `/api/products`
* @access Public
*/
const getProducts = asyncHandler(async (req: Request, res: Response) => {
try {
const products = await ProductModel.find({});
Expand All @@ -21,9 +23,11 @@ const getProducts = asyncHandler(async (req: Request, res: Response) => {
}
});

// @desc Get product by id
// @route GET /api/products/:id
// @access Public
/**
* @desc Get product by id
* @route `GET` `/api/products/:id`
* @access Public
*/
const getProductById = asyncHandler(async (req: Request, res: Response) => {
try {
const product = await ProductModel.findById(req.params.id);
Expand All @@ -42,9 +46,11 @@ const getProductById = asyncHandler(async (req: Request, res: Response) => {
}
});

// @desc Delete a product
// @route DELETE /api/products/:id
// @access Private/Admin
/**
* @desc Delete a product
* @route `DELETE` `/api/products/:id`
* @access Private/Admin
*/
const deleteProduct = asyncHandler(async (req: Request, res: Response) => {
const product = await ProductModel.findByIdAndDelete(req.params.id);

Expand All @@ -56,9 +62,11 @@ const deleteProduct = asyncHandler(async (req: Request, res: Response) => {
}
});

// @desc Create a product
// @route POST /api/products
// @access Private/Admin
/**
* @desc Create a product
* @route `POST` `/api/products`
* @access Private/Admin
*/
const createProduct = asyncHandler(async (req: Request, res: Response) => {
const productData = {} as {
name: string;
Expand Down Expand Up @@ -109,9 +117,11 @@ const createProduct = asyncHandler(async (req: Request, res: Response) => {
}
});

// @desc Update a product
// @route PUT /api/products/:id
// @access Private/Admin
/**
* @desc Update a product
* @route `PUT` `/api/products/:id`
* @access Private/Admin
*/
const updateProduct = asyncHandler(async (req, res) => {
const { existingItems, atLeastOneItemExist } = itemsInObject<{
name?: string;
Expand Down
64 changes: 40 additions & 24 deletions backend/controllers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import asyncHandler from 'express-async-handler';
import hashPassword from '../utils/hashPassword';
// import expressAsyncHandler from '../utils/core/express-async-handler';

// @desc Auth user & get token
// @route POST /api/users/login
// @access Public
/**
* @desc Auth user & get token
* @route `POST` `/api/users/login`
* @access Public
*/
const authUser = asyncHandler(async (req: Request, res: Response) => {
const { email, password } = req.body;

Expand All @@ -29,9 +31,11 @@ const authUser = asyncHandler(async (req: Request, res: Response) => {
}
});

// @desc Register a new user
// @route POST /api/users
// @access Public
/**
* @desc Register a new user
* @route `POST` `/api/users`
* @access Public
*/
const registerUser = asyncHandler(async (req: Request, res: Response) => {
const { name, email, password } = req.body;

Expand Down Expand Up @@ -62,9 +66,11 @@ const registerUser = asyncHandler(async (req: Request, res: Response) => {
}
});

// @desc Get user profile
// @route GET /api/users/profile
// @access Private
/**
* @desc Get user profile
* @route `GET` `/api/users/profile`
* @access Private
*/
const getUserProfile = asyncHandler(async (req: Request, res: Response) => {
// if (!req.user || !req.user._id) throw new Error('User not found');

Expand All @@ -83,9 +89,11 @@ const getUserProfile = asyncHandler(async (req: Request, res: Response) => {
}
});

// @desc Update user profile
// @route PUT /api/users/profile
// @access Private
/**
* @desc Update user profile
* @route `PUT` `/api/users/profile`
* @access Private
*/
const updateUserProfile = asyncHandler(async (req: Request, res: Response) => {
// res.send('successful calling')
// if (!req.user || !req.user._id) throw new Error('User not found');
Expand Down Expand Up @@ -159,17 +167,21 @@ const updateUserProfile = asyncHandler(async (req: Request, res: Response) => {
}
});

// @desc Get all users
// @route GET /api/users
// @access Private
/**
* @desc Get all users
* @route `GET` `/api/users`
* @access Private
*/
const getUsers = asyncHandler(async (req: Request, res: Response) => {
const users = await UserModel.find({});
res.json(users);
});

// @desc Admin delete user with id
// @route DELETE /api/users/:id
// @access Private/admin
/**
* @desc Admin delete user with id
* @route `DELETE` `/api/users/:id`
* @access Private/admin
*/
const deleteUser = asyncHandler(async (req: Request, res: Response) => {
// const user = await UserModel.findById(req.params.id);
// // findByIdAndDelete vs findByIdAndRemove
Expand All @@ -188,9 +200,11 @@ const deleteUser = asyncHandler(async (req: Request, res: Response) => {
} else res.json({ succuss: true });
});

// @desc Get user by ID
// @route GET /api/users/:id
// @access Private/Admin
/**
* @desc Get user by ID
* @route `GET` `/api/users/:id`
* @access Private/Admin
*/
const getUserById = asyncHandler(async (req, res) => {
const user = await UserModel.findById(req.params.id).select('-password');

Expand All @@ -202,9 +216,11 @@ const getUserById = asyncHandler(async (req, res) => {
}
});

// @desc Update user
// @route PUT /api/users/:id
// @access Private/Admin
/**
* @desc Update user
* @route `PUT` `/api/users/:id`
* @access Private/Admin
*/
const updateUser = asyncHandler(async (req, res) => {
// const user = await UserModel.findById(req.params.id);

Expand Down

0 comments on commit 183cdbf

Please sign in to comment.