-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct.controller.ts
65 lines (59 loc) · 1.66 KB
/
product.controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { CreateProduct, GetProduct, PrismaProduct } from "../types";
import ProductProvider from "../abstracts/product.abstract";
import { Request, Response, NextFunction } from "express";
class ProductController {
constructor(private service: ProductProvider) {
this.service = service;
}
public getProducts = async (
req: Request,
res: Response,
next: NextFunction
): Promise<Response<any, Record<string, any>> | void> => {
try {
const products = await this.service.getProducts();
return res.status(200).json(products);
} catch (e) {
next(e);
}
};
public getProductById = async (
req: Request<GetProduct>,
res: Response,
next: NextFunction
): Promise<Response<any, Record<string, any>> | void> => {
try {
const product: PrismaProduct = await this.service.getProductById(
req.params
);
return res.status(200).json(product);
} catch (e) {
next(e);
}
};
public postProduct = async (
req: Request<unknown, unknown, CreateProduct>,
res: Response,
next: NextFunction
): Promise<Response<any, Record<string, any>> | void> => {
try {
const newProduct = await this.service.createProduct(req.body);
return res.status(201).json(newProduct);
} catch (e) {
next(e);
}
};
public deleteProduct = async (
req: Request<GetProduct>,
res: Response,
next: NextFunction
): Promise<Response<any, Record<string, any>> | void> => {
try {
const product = await this.service.deleteProduct(req.params);
return res.status(200).json(product);
} catch (e) {
next(e);
}
};
}
export default ProductController;