first commit

This commit is contained in:
Swapnil
2024-12-06 01:01:25 +05:30
commit 8a60e520c6
29 changed files with 2595 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
import { NextFunction, Request, Response } from 'express';
import { IProductInteractor } from '../interfaces/IProductInteractor';
import { AsyncHandler } from '../utils/handler/AsyncHandler';
import ApiResponse from '../utils/helper/ApiResponse';
export class ProductController {
private interactor: IProductInteractor;
constructor(interactor: IProductInteractor) {
this.interactor = interactor;
}
// @AsyncHandler()
async onCreateProduct(req: Request, res: Response, next: NextFunction) {
const data = await this.interactor.createProduct(req.body);
res.status(201).json(new ApiResponse(201, data, 'Successfully created'));
}
// @AsyncHandler()
async onGetProducts(req: Request, res: Response, next: NextFunction) {
const offset = parseInt(`${req.query.offset}`) || 0;
const limit = parseInt(`${req.query.limit}`) || 10;
const data = await this.interactor.getProducts(limit, offset);
if (data.length === 0) {
res.status(204).json(new ApiResponse(204, null, 'No products found'));
}
res.status(200).json(new ApiResponse(200, data, 'Products retrieved successfully'));
}
// @AsyncHandler()
async onUpdateStock(req: Request, res: Response, next: NextFunction) {
const data = await this.interactor.updateStock(parseInt(`${req.params.id}`), parseInt(`${req.body.stock}`));
res.status(200).json(new ApiResponse(200, data, 'Successfully updated'));
}
}