{"size": 1568, "ext": "js", "max_stars_repo_path": "server/controllers/updatepostedprice.js", "max_stars_repo_name": "crispy1996/Andela_AutoMart", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/controllers/updatepostedprice.js", "max_issues_repo_name": "crispy1996/Andela_AutoMart", "max_issues_repo_issues_event_min_datetime": "2020-09-04T23:45:53.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T11:31:59.000Z", "max_forks_repo_path": "server/controllers/updatepostedprice.js", "max_forks_repo_name": "Crispy-rw/Andela_AutoMart", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 8, "max_forks_count": null, "avg_line_length": 28.0, "max_line_length": 134, "alphanum_fraction": 0.5911989796}
import updatepostedprice from '../helpers/updatepostcarprice';
import pool from '../helpers/db/pool';
 

const newPostedPrice = async (req, res) => {
    const { error } = updatepostedprice.validation(req.body);

    if( error ) {
        return res.status(400).json({
            status: 400,
            error: error.details[0].message.split('"').join(' ')
        });
    }


    const paramId = parseInt(req.params.id);

    const checkPostedOrder = await pool.query("SELECT *from cars WHERE id = $1",[paramId]);

    if(!checkPostedOrder.rows.length){
        return res.status(404).json({
            status:404,
            error: "Id not found for the car"
        });
    }


    if(checkPostedOrder.rows[0].owner !== req.user.id){
        return res.status(403).json({
            status:403,
            error: "Access Forbidden"
        });
    }

    const saveNewPrice = await pool.query("UPDATE cars SET price = $2 WHERE id = $1 RETURNING price,id",[paramId,req.body.new_price]);

    return res.status(200).json({
        status:200,
        message:"Price updated successfully",
        data:{
            id: saveNewPrice.rows[0].id,
            email: req.user.email,
            created_on: checkPostedOrder.rows[0].created_on,
            manufacturer: checkPostedOrder.rows[0].manufacturer,
            model: checkPostedOrder.rows[0].model,
            price: saveNewPrice.rows[0].price,
            state: checkPostedOrder.rows[0].state,
            status: checkPostedOrder.rows[0].status
        }
    });


}

export default newPostedPrice;
