forked from swapnil.bendal/TypeScript-Backend-Template
[update] - latest code
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { NextFunction, Request, Response } from 'express';
|
||||
import { IProductInteractor } from '../interfaces/IProductInteractor';
|
||||
import { AsyncHandler } from '../utils/handler/AsyncHandler';
|
||||
import ApiResponse from '../utils/helper/ApiResponse';
|
||||
import { AsyncHandler } from '../utils/handler/async.handler';
|
||||
|
||||
export class ProductController {
|
||||
private interactor: IProductInteractor;
|
||||
@@ -18,12 +18,13 @@ export class ProductController {
|
||||
|
||||
@AsyncHandler()
|
||||
async onGetProducts(req: Request, res: Response, next: NextFunction) {
|
||||
const offset = parseInt(`${req.query.offset}`) || 0;
|
||||
const limit = parseInt(`${req.query.limit}`) || 10;
|
||||
const offset = Number.isInteger(Number(req.query.offset)) ? parseInt(`${req.query.offset}`, 10) : 0;
|
||||
const limit = Number.isInteger(Number(req.query.limit)) ? parseInt(`(${req.query.limit}`, 10) : 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, null, 'No products found'));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -32,7 +33,7 @@ export class ProductController {
|
||||
|
||||
@AsyncHandler()
|
||||
async onUpdateStock(req: Request, res: Response, next: NextFunction) {
|
||||
const data = await this.interactor.updateStock(parseInt(`${req.params.id}`), parseInt(`${req.body.stock}`));
|
||||
const data = await this.interactor.updateStock(parseInt(`${req.params.id}`, 10), parseInt(`${req.body.stock}`, 10));
|
||||
res.status(200).json(new ApiResponse(200, data, 'Successfully updated'));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user