@@ -9,7 +9,7 @@ const bcrypt = require('bcryptjs');
9
9
const jwt = require ( 'jsonwebtoken' ) ;
10
10
const CryptoJS = require ( 'crypto-js' ) ;
11
11
12
- const txtfs = require ( 'node: fs' ) ;
12
+ const txtfs = require ( 'fs' ) ;
13
13
14
14
const app = express ( ) ;
15
15
const port = 3000 ;
@@ -204,15 +204,43 @@ app.get('/api/parts', authenticateJWT, (req, res) => {
204
204
// Add a new part for the authenticated user
205
205
app . post ( '/api/parts' , authenticateJWT , ( req , res ) => {
206
206
const { name, value, footprint, description, quantity } = req . body ;
207
+ const get_sql = 'SELECT * FROM parts WHERE name = ? AND footprint = ? AND value = ? AND user_id = ?' ;
207
208
const sql = 'INSERT INTO parts (version, user_id, name, value, footprint, description, quantity) VALUES (?, ?, ?,?, ?, ?, ?)' ;
208
- db . run ( sql , [ version , req . user . id , name , value , footprint , description , quantity ] , function ( err ) {
209
+ const updatesql = 'UPDATE parts SET quantity = ? WHERE user_id = ? AND id = ?' ;
210
+ db . all ( get_sql , [ name , footprint , value , req . user . id ] , function ( err , rows ) {
209
211
if ( err ) {
210
- return res . status ( 400 ) . json ( { error : err . message } ) ;
212
+ console . log ( "couldn't add part..." ) ;
213
+ return res . status ( 400 ) . json ( { error : err . message } ) ;
211
214
}
212
- res . json ( {
213
- message : 'success' ,
214
- data : { id : this . lastID , name, value, footprint, description : description , quantity }
215
- } ) ;
215
+ console . log ( rows [ 0 ] ) ;
216
+ if ( rows [ 0 ] == null )
217
+ {
218
+ db . run ( sql , [ version , req . user . id , name , value , footprint , description , quantity ] , function ( err ) {
219
+ if ( err ) {
220
+ return res . status ( 400 ) . json ( { error : err . message } ) ;
221
+ }
222
+ res . json ( {
223
+ message : 'success' ,
224
+ data : { id : this . lastID , name, value, footprint, description : description , quantity }
225
+ } ) ;
226
+ } ) ;
227
+ }
228
+ else
229
+ {
230
+ console . log ( rows [ 0 ] ) ;
231
+ part_count = rows [ 0 ] . quantity
232
+ part_id = rows [ 0 ] . id
233
+ new_count = ( parseInt ( part_count ) + parseInt ( quantity ) ) ;
234
+ db . run ( updatesql , [ new_count , req . user . id , part_id ] , function ( err ) {
235
+ if ( err ) {
236
+ console . log ( "couldn't update count..." ) ;
237
+ return res . status ( 400 ) . json ( { error : err . message } ) ;
238
+ }
239
+ res . json ( { message : 'success' } ) ;
240
+ } ) ;
241
+
242
+ }
243
+
216
244
} ) ;
217
245
} ) ;
218
246
@@ -369,12 +397,43 @@ app.post('/api/upload', authenticateJWT, upload.single('file'), (req, res) => {
369
397
. on ( 'end' , ( ) => {
370
398
parts . forEach ( part => {
371
399
const { name, value, footprint, description, quantity } = part ;
372
- const sql = 'INSERT INTO partlist (version, user_id, name, value, footprint, description, quantity) VALUES (?,?, ?, ?, ?, ?, ?)' ;
373
- const params = [ version , req . user . id , name , value , footprint , description , quantity ] ;
374
- db . run ( sql , params , function ( err ) {
400
+ const get_sql = 'SELECT * FROM parts WHERE name = ? AND footprint = ? AND value = ? AND user_id = ?' ;
401
+ const sql = 'INSERT INTO parts (version, user_id, name, value, footprint, description, quantity) VALUES (?, ?, ?,?, ?, ?, ?)' ;
402
+ const updatesql = 'UPDATE parts SET quantity = ? WHERE user_id = ? AND id = ?' ;
403
+ db . all ( get_sql , [ name , footprint , value , req . user . id ] , function ( err , rows ) {
375
404
if ( err ) {
376
- console . error ( err . message ) ;
405
+ console . log ( "couldn't add part..." ) ;
406
+ return res . status ( 400 ) . json ( { error : err . message } ) ;
407
+ }
408
+ console . log ( rows [ 0 ] ) ;
409
+ if ( rows [ 0 ] == null )
410
+ {
411
+ db . run ( sql , [ version , req . user . id , name , value , footprint , description , quantity ] , function ( err ) {
412
+ if ( err ) {
413
+ return res . status ( 400 ) . json ( { error : err . message } ) ;
414
+ }
415
+
416
+ } ) ;
417
+ }
418
+ else
419
+ {
420
+ console . log ( rows [ 0 ] ) ;
421
+ part_count = rows [ 0 ] . quantity
422
+ part_id = rows [ 0 ] . id
423
+ console . log ( "part count: " , part_count ) ;
424
+ new_count = ( parseInt ( part_count ) + parseInt ( quantity ) ) ;
425
+ console . log ( "count being added: " , new_count ) ;
426
+ db . run ( updatesql , [ new_count , req . user . id , part_id ] , function ( err ) {
427
+ if ( err ) {
428
+ console . log ( "couldn't update count..." ) ;
429
+ return res . status ( 400 ) . json ( { error : err . message } ) ;
430
+ }
431
+ console . log ( "do where get to this point?" ) ;
432
+
433
+ } ) ;
434
+
377
435
}
436
+
378
437
} ) ;
379
438
} ) ;
380
439
res . json ( { message : 'CSV file successfully processed' } ) ;
0 commit comments