<commit_before>module.exports = function(app, db, auth) {
  ////////////////////////////////////////////////////
  // Get active projects
  ////////////////////////////////////////////////////
  // Only returns active projects that are published!
  ////////////////////////////////////////////////////
  app.get('/api/project/active', function(req, res) {
    db.project.find({
      $and: [
        { publish: true },
        { $or: [{end_date: undefined}, {end_date: {"$gte" : new Date()}}] }
      ]
    }, function(err, list) {
      if (!err) { 
        res.json(list);
      } else {
        res.json(err);
      }
    });
  });

  ////////////////////////////////////////////////////
  // Get completed projects
  ////////////////////////////////////////////////////
  // Only returns completed projects that are published!
  ////////////////////////////////////////////////////
  app.get('/api/project/completed', function(req, res) {
    db.project.find({
      publish: true,
      end_date: {"$lt": new Date()}
    }, function(err, list) {
      if (!err) { 
        res.json(list);
      } else {
        res.json(err);
      }
    });
  });
  
  //Post to add new projects, auth only for admin
  app.post('/api/project', auth.middleware(true), function(req, res) {
    var data = req.body;

    //Ask the db to create a new Project
    //passing the data of the request  and a cb
    db.project.create({
      title: data.title,
      problem: data.problem,
      action: data.action,
      goal: data.goal,
      shortname: data.shortname,
      end_date: data.end_date,
      image: data.image, //we save the InkBLob of the image to delete it when the project is deleted
      InkBlob: (data.InkBlob && typeof data.InkBlob === "object")?  JSON.stringify(data.InkBlob) : data.InkBlob
    }, function(err, model) {
      if (!err) {
        res.json(model); //If went ok return the json of the model
      } else {
        res.json(err); // If could not be save send the json of the error (A better error strategy can be defined)
      }
    });
  });

  //Get of all project
  app.get('/api/project', function(req, res) {
    var cb = function(err, list) {
      if (!err) {
        res.json(list); //If went ok return the json of the query result
      } else {
        res.json(err);
      }
    };

    if (req.query) {
      db.project.find(req.query, cb);
    } else {
      db.project.findAll(cb);
    }
  });

  //Get of a single project
  app.get('/api/project/:projectID', function(req, res) {
    db.project.find({
        id: req.params.projectID
      },
      function(err, list) {
        if (!err) {
          //If went ok return the json of the query result
          if (list.length) {
            res.json(list[0]);
          } else {
            res.json({
              message: 'Object Not Found'
            });
          }
        } else {
          res.json(err);
        }
      });
  });

  //Update a project, ngResource use post not put
  app.post('/api/project/:projectID', auth.middleware(true), function(req, res) {
    if(req.body.InkBlob && typeof req.body.InkBlob === "object"){
      //Stringify InkBlob so we dont care if the db support JSON
      req.body.InkBlob = JSON.stringify(req.body.InkBlob);
    }
    db.project.update(req.params.projectID, req.body,
      function(err, project) {
        if (!err) {
          res.json(project);
        } else {
          res.send(err);
        }
      });
  });

  //Delete a project
  app.del('/api/project/:projectID', auth.middleware(true), function(req, res) {
    db.project.remove(req.params.projectID,
      function(err, project) {
        if (!err) {
          res.json(project);
        } else {
          res.send(err);
        }
      });
  });<commit_msg>Fix Project API<commit_after>module.exports = function(app, db, auth) {
  ////////////////////////////////////////////////////
  // Get active projects
  ////////////////////////////////////////////////////
  // Only returns active projects that are published!
  ////////////////////////////////////////////////////
  app.get('/api/project/active', function(req, res) {
    db.project.find({
      $and: [
        { publish: true },
        { $or: [{end_date: undefined}, {end_date: {"$gte" : new Date()}}] }
      ]
    }, function(err, list) {
      if (!err) { 
        res.json(list);
      } else {
        res.json(err);
      }
    });
  });

  ////////////////////////////////////////////////////
  // Get completed projects
  ////////////////////////////////////////////////////
  // Only returns completed projects that are published!
  ////////////////////////////////////////////////////
  app.get('/api/project/completed', function(req, res) {
    db.project.find({
      publish: true,
      end_date: {"$lt": new Date()}
    }, function(err, list) {
      if (!err) { 
        res.json(list);
      } else {
        res.json(err);
      }
    });
  });
  
  //Post to add new projects, auth only for admin
  app.post('/api/project', auth.middleware(true), function(req, res) {
    var data = req.body;

    //Ask the db to create a new Project
    //passing the data of the request  and a cb
    db.project.create({
      title: data.title,
      problem: data.problem,
      action: data.action,
      goal: data.goal,
      shortname: data.shortname,
      end_date: data.end_date,
      image: data.image, //we save the InkBLob of the image to delete it when the project is deleted
      InkBlob: (data.InkBlob && typeof data.InkBlob === "object")?  JSON.stringify(data.InkBlob) : data.InkBlob
    }, function(err, model) {
      if (!err) {
        res.json(model); //If went ok return the json of the model
      } else {
        res.json(err); // If could not be save send the json of the error (A better error strategy can be defined)
      }
    });
  });

  //Get of a single project
  app.get('/api/project/:projectID', function(req, res) {
    db.project.find({
        id: req.params.projectID
      },
      function(err, list) {
        if (!err) {
          //If went ok return the json of the query result
          if (list.length) {
            res.json(list[0]);
          } else {
            res.json({
              message: 'Object Not Found'
            });
          }
        } else {
          res.json(err);
        }
      });
  });

  //Get of all project
  app.get('/api/project', function(req, res) {
    console.log("THINGS ARE HAPPENING!!!");
    var cb = function(err, list) {
      if (!err) {
        res.json(list); //If went ok return the json of the query result
      } else {
        res.json(err);
      }
    };

    if (req.query) {
      db.project.find(req.query, cb);
    } else {
      db.project.findAll(cb);
    }
  });

  //Update a project, ngResource use post not put
  app.post('/api/project/:projectID', auth.middleware(true), function(req, res) {
    if(req.body.InkBlob && typeof req.body.InkBlob === "object"){
      //Stringify InkBlob so we dont care if the db support JSON
      req.body.InkBlob = JSON.stringify(req.body.InkBlob);
    }
    db.project.update(req.params.projectID, req.body,
      function(err, project) {
        if (!err) {
          res.json(project);
        } else {
          res.send(err);
        }
      });
  });

  //Delete a project
  app.del('/api/project/:projectID', auth.middleware(true), function(req, res) {
    db.project.remove(req.params.projectID,
      function(err, project) {
        if (!err) {
          res.json(project);
        } else {
          res.send(err);
        }
      });
  });
{'fraction_non_alphanumeric': 0.21739704329461457, 'fraction_numerical': 0.00026399155227032733, 'mean_word_length': 1.5281948615281948, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1174511', 'n_tokens_mistral': 2252, 'n_tokens_neox': 2231, 'n_words': 579}