Skip to content

Commit 040d55d

Browse files
authored
Feat: listProduct and updateProduct routes (#17)
1 parent 1fec527 commit 040d55d

4 files changed

Lines changed: 146 additions & 1 deletion

File tree

src/controllers/productsController.js

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
import { ProductsModelRepository } from "../models/productsModel.js";
12
import { ProductsService } from "../services/productsService.js";
23
import { Middleware } from "../utils/auth/middleware.js";
34
import { decode } from "jsonwebtoken"
45
import { once } from "node:events";
56

67
export class ProductsController {
8+
_productsModelRepository;
79
_productsService;
810
_userMiddleware;
911

10-
constructor(productsService = new ProductsService, userMiddleware = new Middleware()) {
12+
constructor(productsService = new ProductsService, userMiddleware = new Middleware(), productsModelRepository = new ProductsModelRepository()) {
13+
this._productsModelRepository = productsModelRepository;
1114
this._productsService = productsService;
1215
this._userMiddleware = userMiddleware;
1316
}
@@ -61,4 +64,95 @@ export class ProductsController {
6164
return response.end(JSON.stringify({ message: checkToken }));
6265
}
6366

67+
async listProduct(request, response) {
68+
69+
const checkToken = await this._userMiddleware.ensureUserAuthenthicated(request, response);
70+
71+
if(checkToken === true) {
72+
73+
const url = request.url;
74+
const splitUrl = url.split("/");
75+
const productId = splitUrl[2];
76+
77+
if(productId === "") {
78+
response.writeHead(401);
79+
return response.end(JSON.stringify({ message: "Product ID must have a value !" }));
80+
}
81+
82+
const findProductById = await this._productsService.listProduct(productId);
83+
84+
if(!findProductById) {
85+
response.writeHead(404);
86+
return response.end(JSON.stringify({ message: "Product not found !" }));
87+
}
88+
89+
response.writeHead(200);
90+
return response.end(JSON.stringify({
91+
product: {
92+
product_id: findProductById.product_id,
93+
product_name: findProductById.product_name,
94+
price: findProductById.price,
95+
category: findProductById.category,
96+
createdAt: findProductById.createdAt
97+
}
98+
}));
99+
100+
}
101+
102+
response.writeHead(401);
103+
return response.end(JSON.stringify({ message: checkToken }));
104+
}
105+
106+
async updateProduct(request, response) {
107+
108+
const checkToken = await this._userMiddleware.ensureUserAuthenthicated(request, response);
109+
110+
if(checkToken === true) {
111+
112+
const url = request.url;
113+
const splitUrl = url.split("/");
114+
const product_id = splitUrl[2];
115+
116+
if(product_id === "") {
117+
response.writeHead(401);
118+
return response.end(JSON.stringify({ message: "Product ID must have a value !" }));
119+
}
120+
121+
const { price } = JSON.parse(await once(request, "data"));
122+
123+
if(price === "") {
124+
response.writeHead(401);
125+
return response.end(JSON.stringify({ message: "Price must have a value !" }));
126+
127+
}else if(typeof(price) !== "number") {
128+
response.writeHead(401);
129+
return response.end(JSON.stringify({ message: "Price must be a float/decimal number !" }));
130+
}
131+
132+
const findProductById = await this._productsModelRepository.findProductById(product_id);
133+
134+
if(!findProductById) {
135+
response.writeHead(404);
136+
return response.end(JSON.stringify({ message: "User not found !" }));
137+
}
138+
139+
const updateProduct = await this._productsService.updateProduct(product_id, price);
140+
141+
response.writeHead(201);
142+
return response.end(JSON.stringify({
143+
product: {
144+
product_id: updateProduct.product_id,
145+
product_name: updateProduct.product_name,
146+
price: updateProduct.price,
147+
category: updateProduct.category,
148+
createdAt: updateProduct.createdAt
149+
}
150+
}));
151+
152+
}
153+
154+
response.writeHead(401);
155+
return response.end(JSON.stringify({ message: checkToken }));
156+
}
157+
64158
}

src/models/productsModel.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,37 @@ export class ProductsModelRepository {
1515
return create;
1616
}
1717

18+
async listProduct(product_id) {
19+
const find = await this._database._prismaService.products.findUnique({
20+
where: {
21+
product_id: product_id
22+
}
23+
});
24+
25+
return find;
26+
}
27+
28+
async updateProduct(product_id, price) {
29+
const update = await this._database._prismaService.products.update({
30+
where: {
31+
product_id: product_id
32+
},
33+
data: {
34+
price: price
35+
}
36+
});
37+
38+
return update;
39+
}
40+
41+
async findProductById(product_id) {
42+
const find = await this._database._prismaService.products.findUnique({
43+
where: {
44+
product_id: product_id
45+
}
46+
});
47+
48+
return find;
49+
}
50+
1851
}

src/routes/user.routes.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ export class UserRoutes {
3838
case "/createProduct":
3939
await productController.createProduct(request, response);
4040
break;
41+
42+
case "/listProduct/":
43+
await productController.listProduct(request, response);
44+
break;
45+
46+
case "/updateProduct/":
47+
await productController.updateProduct(request, response);
48+
break;
4149

4250
default:
4351
response.writeHead(404);

src/services/productsService.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,14 @@ export class ProductsService {
1212
return create;
1313
}
1414

15+
async listProduct(product_id) {
16+
const find = await this._productsModelRepository.listProduct(product_id);
17+
return find;
18+
}
19+
20+
async updateProduct(product_id, price) {
21+
const update = await this._productsModelRepository.updateProduct(product_id, price);
22+
return update;
23+
}
24+
1525
}

0 commit comments

Comments
 (0)