Skip to content

Commit

Permalink
[finsihes 187354259] added endpoint to get all products
Browse files Browse the repository at this point in the history
added end point to get one product

added end point for a seller to delete a product from a his collection
  • Loading branch information
Munezero Ange committed May 8, 2024
1 parent 608d497 commit 4f50cad
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 10 deletions.
45 changes: 44 additions & 1 deletion src/controllers/productsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const createProduct = async (req: Request, res: Response) => {
});
}
// handle images
const productImages = ['asdf', 'asdf', 'asdf', 'asdf'];
const productImages: string[] = [];
const images: unknown = req.files;
if (images instanceof Array && images.length > 3) {
for (const image of images) {
Expand Down Expand Up @@ -69,3 +69,46 @@ export const createSize = async (req: Request, res: Response) => {
sendInternalErrorResponse(res, error);
}
};

// a function to get all products available in the database
export const getAllProduct = async (req: Request, res: Response) => {
try {
// Fetch all products with their associated sizes
const products = await Product.findAll({
include: [{ model: Size, as: 'Sizes' }],
});

res.status(200).json({
ok: true,
message: 'All products retrieved successfully',
data: products,
});
} catch (error) {
sendInternalErrorResponse(res, error);
}
};

// a function to get a certain product by ID
export const getProductById = async (req: Request, res: Response) => {
try {
const { productId } = req.params;
const product = await Product.findByPk(productId, {
include: [{ model: Size, as: 'Sizes' }],
});

if (!product) {
return res.status(404).json({
ok: false,
message: 'Product not found',
});
}

res.status(200).json({
ok: true,
message: 'Product retrieved successfully',
data: product,
});
} catch (error) {
sendInternalErrorResponse(res, error);
}
};
3 changes: 1 addition & 2 deletions src/database/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ module.exports = {
username: process.env.DEV_DB_USER,
password: process.env.DEV_DB_PASSWORD,
database: process.env.DEV_DB_NAME,

dialect: process.env.DEV_DB_DRIVER,
port: process.env.DEV_DB_PORT,
host: process.env.DEV_DB_HOST,
host: 'localhost',
},
test: {
username: process.env.TEST_DB_USER,
Expand Down
2 changes: 1 addition & 1 deletion src/database/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const dbConfig = config[env];

const sequelize = new Sequelize(dbConfig.database!, dbConfig.username!, dbConfig.password!, {
dialect: dbConfig.dialect! as Dialect,
host: dbConfig.host || 'localhost',
host: 'localhost',
port: dbConfig.port,
});

Expand Down
2 changes: 1 addition & 1 deletion src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Router } from 'express';
import userRoute from './userRoute';
import authRoute from './authRoute';
import roleRoute from './roleRoute';
import { productRouter } from './productRoutes';
import productRouter from './productRoutes';
import { categoryRouter } from './categoryRouter';

const router = Router();
Expand Down
15 changes: 10 additions & 5 deletions src/routes/productRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
/* eslint-disable @typescript-eslint/no-misused-promises */
import express from 'express';
import { createProduct, createSize } from '../controllers/productsController';
import { Router } from 'express';
import { createProduct, createSize, getAllProduct, getProductById } from '../controllers/productsController';
import multerUpload from '../helpers/multer';
import { checkUserRoles, isAuthenticated } from '../middlewares/authMiddlewares';

export const productRouter = express.Router();
const router = Router();

productRouter.post(
router.post(
'/:categoryId/create-product/',
isAuthenticated,
checkUserRoles('seller'),
multerUpload.array('images', 8),
createProduct
);

productRouter.post('/:productId/add-size', isAuthenticated, checkUserRoles('seller'), createSize);
router.post('/:productId/add-size', isAuthenticated, checkUserRoles('seller'), createSize);

router.get('/', getAllProduct);
router.get('/:productId', getProductById);

export default router;

0 comments on commit 4f50cad

Please sign in to comment.