{"size": 3619, "ext": "js", "max_stars_repo_path": "model/products/editProductDesc.js", "max_stars_repo_name": "mczal/Sidekeek-Server", "max_stars_repo_stars_event_min_datetime": "2021-04-27T02:37:05.000Z", "max_stars_repo_stars_event_max_datetime": "2021-04-27T02:37:05.000Z", "max_issues_repo_path": "model/products/editProductDesc.js", "max_issues_repo_name": "mczal/Sidekeek-Server", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "model/products/editProductDesc.js", "max_forks_repo_name": "mczal/Sidekeek-Server", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 45.2375, "max_line_length": 175, "alphanum_fraction": 0.5037303122}
var mysql = require('mysql');
//TESTED 27 FEBRUARI 2016
function editProductDesc(router,connection){
  var self=this;
  self.handleRoutes(router,connection);
}

var self=this;

editProductDesc.prototype.handleRoutes = function(router,connection){
  router.post('/editProductDesc',function(req,res){
    var sessionCode = connection.escape(req.body.sessionCode);
    var idProduct = connection.escape(req.body.idProduct);
    if(sessionCode==null || sessionCode==undefined || sessionCode==''){
      res.json({"message":"err.. no params rec","error":"error"});
    }else{
      if(idProduct==null || idProduct==undefined || idProduct==''){
        res.json({"message":"err.. no params rec","error":"error"});
      }else{
        connection.query("select id_host from `session_host` where session_code="+sessionCode,function(err,rows){
          if(err){
            res.json({"message":"err.. error on selecting host","error":"error"});
          }else{
            if(rows.length>0){
              var idHost = rows[0].id_host;
              var namaProduk = connection.escape(req.body.namaProduk);
              var harga = connection.escape(req.body.harga);
              var productDesc = connection.escape(req.body.productDesc);
              connection.beginTransaction(function(err){
                if (err) {
                  res.json({"message":"err.. error on beginTransaction","error":"error","objErr":err});
                  return;
                }
                var query = "update `product` set product_name="+namaProduk+",product_desc="+productDesc+",price="+harga+" where id_host="+idHost+" and id_product="+idProduct;
                connection.query(query,function(err,rows){
                  if(err){
                    connection.rollback(function(){
                      res.json({"message":"err.. error on updating","query":query,"error":"error","objErr":err});
                      return;
                    });
                  }else{
                    //updating timestamp on session_host
                    var myDate = new Date();
                    var myTimestamp = myDate.getFullYear()+"-"+(myDate.getMonth()+1)+
                    "-"+myDate.getDate()+" "+myDate.getHours()+
                    ":"+myDate.getMinutes()+":"+myDate.getSeconds();
                    connection.query("update `session_host` set last_activity='"+myTimestamp+"' where session_code="+sessionCode,function(err,rows){
                      if(err){
                        connection.rollback(function(){
                          res.json({"message":"err.. error on updating session","error":"error","objErr":err});
                          return;
                        });
                      }else{
                        connection.commit(function(err) {
                          if (err) {
                            connection.rollback(function() {
                              res.json({"message":"err.. error commiting transaction","error":"error","objErr":err});
                              return;
                            });
                          }else{
                            res.json({"message":"success updating your product and session last activity, happy sunday","error":"success"});
                          }
                        });
                      }
                    });
                  }
                });
              });
            }else{
              res.json({"message":"err.. no session registered","error":"invalidSession"});
            }
          }
        });
      }
    }
  });
}

module.exports = editProductDesc;
