@@ -8,21 +8,21 @@ const appRouter = async function(app, connection) {
88 /**************************** API USER ***************************************/
99 /*****************************************************************************/
1010
11- /*********************** Check if user with this email already exist *************************/
12- await app . use ( "/users/sign-up" , ( req , res , next ) => {
13- console . log ( req . body . email ) ;
14- connection . query (
15- `SELECT * FROM users WHERE email = '${ req . body . email } '` ,
16- ( err , results ) => {
17- if ( err ) throw err ;
18- if ( results . length > 0 ) {
19- res . status ( 200 ) . send ( "this EMAIL already exist" ) ;
20- } else {
21- next ( ) ;
22- }
11+ /*********************** Check if user with this email already exist *************************/
12+ await app . use ( "/users/sign-up" , ( req , res , next ) => {
13+ console . log ( req . body . email ) ;
14+ connection . query (
15+ `SELECT * FROM users WHERE email = '${ req . body . email } '` ,
16+ ( err , results ) => {
17+ if ( err ) throw err ;
18+ if ( results . length > 0 ) {
19+ res . status ( 200 ) . send ( "this EMAIL already exist" ) ;
20+ } else {
21+ next ( ) ;
2322 }
24- ) ;
25- } ) ;
23+ }
24+ ) ;
25+ } ) ;
2626
2727 // - POST /users/sign-up ⇒ Will add a user in the Users table (of course the
2828 // password will be encrypted...)
@@ -143,21 +143,32 @@ const appRouter = async function(app, connection) {
143143 await app . get ( "/products/" , function ( req , res ) {
144144 let getProductsInfo = "SELECT * FROM products" ;
145145 connection . query ( getProductsInfo , function ( err , results ) {
146+ results . forEach ( ( element ) => {
147+ element . url = element . url . split ( "," ) ;
148+ } ) ;
146149 if ( err ) throw err ;
147150 res . send ( results ) ;
148151 } ) ;
149152 } ) ;
150153
154+ // POST /products/ ⇒ Will add a product in the Products table (only if the user who create the product has a good JWT...)
151155 // POST /products/ ⇒ Will add a product in the Products table (only if the user who create the product has a good JWT...)
152156 await app . post ( "/products/" , auth , function ( req , res ) {
153- let category =
154- req . body . category . charAt ( 0 ) . toUpperCase ( ) + req . body . category . slice ( 1 ) ;
157+ let category = req . body . category ;
155158 let prices = req . body . prices ;
156- let name = req . body . name . charAt ( 0 ) . toUpperCase ( ) + req . body . name . slice ( 1 ) ;
159+ let name = req . body . name ;
157160 let description = req . body . description ;
158- let url = req . body . url ;
161+ let url = "" ;
159162 let id_user_affiliate = req . body . id_user_affiliate ;
160163
164+ for ( let i = 0 ; i < req . body . url . length ; i ++ ) {
165+ if ( i === 0 ) {
166+ url = req . body . url [ i ] ;
167+ } else {
168+ url = url + "," + req . body . url [ i ] ;
169+ }
170+ }
171+
161172 const productObject = {
162173 category : category ,
163174 name : name ,
@@ -175,14 +186,17 @@ const appRouter = async function(app, connection) {
175186 } ) ;
176187 } ) ;
177188
178- //GET /products/:id ⇒ Return all the datas of this specific Product
189+ //GET /products/:id ⇒ Return all the datas of this specific Product
179190 //(including the name of the user who created it, the category, the description etc...)
180191 await app . get ( "/products/:id" , function ( req , res ) {
181192 let id = req . params . id ;
182193 let productInfo = `SELECT users.lastName,users.firstName,products.id, products.name, products.category, products.description, products.prices, products.url, products.promotion, products.promotionIsActive
183194 FROM users INNER JOIN products ON products.id = ${ id } && products.id_user_affiliate = users.id` ;
184195
185196 connection . query ( productInfo , function ( err , results ) {
197+ results . forEach ( ( element ) => {
198+ element . url = element . url . split ( "," ) ;
199+ } ) ;
186200 if ( err ) throw err ;
187201 res . send ( results ) ;
188202 } ) ;
0 commit comments