1+ import { ProductsModelRepository } from "../models/productsModel.js" ;
12import { ProductsService } from "../services/productsService.js" ;
23import { Middleware } from "../utils/auth/middleware.js" ;
34import { decode } from "jsonwebtoken"
45import { once } from "node:events" ;
56
67export 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}
0 commit comments