node-programmeren4-studentenhuis-master/controllers/studentenhuis.controller.js
/**
     * Replace an existing object in the database.
     */
Replace an existing object in the database.
Replace an existing object in the database .
update(req, res, next) {
        try {
            assert(req.user && req.user.id, 'User ID is missing!')
            assert(typeof (req.body) === 'object', 'request body must have an object containing naam and adres.')
            assert(typeof (req.body.naam) === 'string', 'naam must be a string.')
            assert(typeof (req.body.adres) === 'string', 'adres must be a string.')
            assert(typeof (req.body.lat) === 'string', 'lat must be a string.')
            assert(typeof (req.body.long) === 'string', 'long must be a string.')
        } catch (ex) {
            const error = new ApiError(ex.toString(), 500)
            next(error)
            return
        }
        const ID = req.params.huisId
        try {
            pool.getConnection((err, connection) => {
                if (err) {
                    logger.error('Error getting connection from pool: ' + err.toString())
                    const error = new ApiError(err, 500)
                    next(error);
                    return
                }
                connection.query('SELECT * FROM view_studentenhuis WHERE ID = ?', [ ID ],
                (err, rows, fields) => {
                    connection.release()
                    if (err) {
                        const error = new ApiError(err, 412)
                        next(error);
                    } else {
                        if(rows.length !== 1) {
                            const error = new ApiError(err, 404)
                            next(error);
                        } else {
                            if(rows[0].UserID !== req.user.id) {
                                const error = new ApiError(err, 412)
                                next(error);
                            } else {
                                db.query(
                                    'UPDATE studentenhuis SET Naam = ? WHERE ID = ?', 
                                    [ req.body.naam, req.params.huisId], 
                                    (err, rows, fields) => {
                                        if(err) {
                                            const error = new ApiError(err, 412)
                                            next(error);
                                        } else {
                                            res.status(200).json({ result: rows }).end()
                                        }
                                })
                            }
                        }
                    }
                })
            })
        } catch (ex) {
            logger.error(ex)
            const error = new ApiError(ex, 500)
            next(error);
        }
    }
update ( req , res , next ) { try { assert ( req . user && req . user . id , 'User ID is missing!' ) assert ( typeof ( req . body ) === 'object' , 'request body must have an object containing naam and adres.' ) assert ( typeof ( req . body . naam ) === 'string' , 'naam must be a string.' ) assert ( typeof ( req . body . adres ) === 'string' , 'adres must be a string.' ) assert ( typeof ( req . body . lat ) === 'string' , 'lat must be a string.' ) assert ( typeof ( req . body . long ) === 'string' , 'long must be a string.' ) } catch ( ex ) { const error = new ApiError ( ex . toString ( ) , 500 ) next ( error ) return } const ID = req . params . huisId try { pool . getConnection ( ( err , connection ) => { if ( err ) { logger . error ( 'Error getting connection from pool: ' + err . toString ( ) ) const error = new ApiError ( err , 500 ) next ( error ) ; return } connection . query ( 'SELECT * FROM view_studentenhuis WHERE ID = ?' , [ ID ] , ( err , rows , fields ) => { connection . release ( ) if ( err ) { const error = new ApiError ( err , 412 ) next ( error ) ; } else { if ( rows . length !== 1 ) { const error = new ApiError ( err , 404 ) next ( error ) ; } else { if ( rows [ 0 ] . UserID !== req . user . id ) { const error = new ApiError ( err , 412 ) next ( error ) ; } else { db . query ( 'UPDATE studentenhuis SET Naam = ? WHERE ID = ?' , [ req . body . naam , req . params . huisId ] , ( err , rows , fields ) => { if ( err ) { const error = new ApiError ( err , 412 ) next ( error ) ; } else { res . status ( 200 ) . json ( { result : rows } ) . end ( ) } } ) } } } } ) } ) } catch ( ex ) { logger . error ( ex ) const error = new ApiError ( ex , 500 ) next ( error ) ; } }
Replace an existing object in the database.
Replace an existing object in the database .
// req moet de juiste attributen hebben - het nieuwe studentenhuis // Hier hebben we de juiste body als input. // 1. Zoek in db of studentenhuis met huisId bestaat // rows MOET hier 1 waarde bevatten - nl. het gevonden studentenhuis. // zo nee, dan error  // zo ja, dan // - check eerst of de huidige user de 'eigenaar' van het studentenhuis is //  - zo nee, error //  - zo ja, dan SQL query UPDATE // handle error // handle success