<commit_before>var express = require('express');
var router = express.Router();
var moment = require('moment');
var async = require('async');
var logger = require('../library/logger');

var IterationModel = require('../model/iteration_model');
var TaskModel = require('../model/task_model');
var VersionModel = require('../model/version_model');
var TaskFollowModel = require('../model/task_follow_model');
var UserModel = require('../model/user_model');
var TaskStatusModel = require('../model/task_status_model');
var TaskHistoryModel = require('../model/task_history_model');
var TaskConcernedModel = require('../model/task_concerned_model');
var StoryModel = require('../model/story_model');
var ProjectModel = require('../model/project_model');
var CsvModel = require('../model/csv_model');

var TaskService = require('../service/task_service');
var RouterService = require('../service/router_service');
var CsvService = require('../service/csv_service');

// 呈现列表
router.get('/', function (req, res, next) {
  if (req.query.iteration_id) {
    checkIterationId(req, res, next);
  } else {
    next();
  }
});
router.get('/', function (req, res, next) {
  if (req.query.user_id) {
    checkUserId(req, res, next);
  } else {
    next();
  }
});
router.get('/', function (req, res) {
  var where = {
    status: TaskModel.statusOnline // 在线的任务
  };
  if (req.query.user_id) { // 获取某个用户
    where.user_id = req.query.user_id;
  }
  if (req.query.iteration_id) { // 获取迭代对应的
    where.iteration_id = req.query.iteration_id;
  }

  TaskModel
    .findAll({
      where: where,
      include: [
        {model: UserModel},
        {model: TaskStatusModel},
        {model: TaskFollowModel},
        {model: TaskHistoryModel},
        {model: StoryModel},
        {model: IterationModel, where: {status: IterationModel.statusOnline}},
        {model: VersionModel},
        {model: ProjectModel},
        {model: TaskConcernedModel}
      ],
      order: 'status_id ASC',
      offset: req.query.offset || 0,
      limit: req.query.size || 10
    })
    .then(function (result) {
      res.json(result);
    });
});

// 新建任务
router.post('/', checkIterationId);
router.post('/', checkStoryId);
router.post('/', checkUserId);
router.post('/', checkPrevTaskIds);
router.post('/', checkTaskStausId);
router.post('/', checkVersionId);
router.post('/', function (req, res, next) { // 添加任务
  TaskModel
    .build({
      project_id: req.version.project_id,
      version_id: req.version.id,
      iteration_id: req.iteration.id,
      story_id: req.story.id,
      user_id: req.body.user_id,
      desc: req.body.desc,
      is_new: req.body.is_new,
      is_challenging: req.body.is_challenging,
      priority: req.body.priority,
      estimated_time: req.body.estimated_time,
      status_id: req.body.task_status_id,
      create_time: moment().unix(),
      remark: req.body.remark
    })
    .save()
    .then(function (task) {
      res.json({id: task.id});
      req.taskId = task.id;
      next();
    })
    .catch(function (err) {
      RouterService.json(err, res);
    });

});
router.post('/', function (req) { // 前置任务添加
  req.prevTaskIds.forEach(function (prevTaskId) {
    TaskFollowModel
      .build({
        task_id: req.taskId,
        prev_task_id: prevTaskId,
        create_time: moment().unix()
      })
      .save();
  });
});

// 编辑任务
router.put('/:id', checkTaskId);
router.put('/:id', checkIterationId);
router.put('/:id', checkVersionId);
router.put('/:id', checkUserId);
router.put('/:id', checkStoryId);
router.put('/:id', checkPrevTaskIds);
router.put('/:id', function (req, res, next) {
  req.task
    .update({
      project_id: req.version.project_id,
      version_id: req.version.id,
      iteration_id: req.iteration.id,
      story_id: req.story.id,
      user_id: req.body.user_id,
      desc: req.body.desc,
      is_new: req.body.is_new,
      is_challenging: req.body.is_challenging,<commit_msg>* 修复一个bug<commit_after>var express = require('express');
var router = express.Router();
var moment = require('moment');
var async = require('async');
var logger = require('../library/logger');

var IterationModel = require('../model/iteration_model');
var TaskModel = require('../model/task_model');
var VersionModel = require('../model/version_model');
var TaskFollowModel = require('../model/task_follow_model');
var UserModel = require('../model/user_model');
var TaskStatusModel = require('../model/task_status_model');
var TaskHistoryModel = require('../model/task_history_model');
var TaskConcernedModel = require('../model/task_concerned_model');
var StoryModel = require('../model/story_model');
var ProjectModel = require('../model/project_model');
var CsvModel = require('../model/csv_model');

var TaskService = require('../service/task_service');
var RouterService = require('../service/router_service');
var CsvService = require('../service/csv_service');

// 呈现列表
router.get('/', function (req, res, next) {
  if (req.query.iteration_id) {
    checkIterationId(req, res, next);
  } else {
    next();
  }
});
router.get('/', function (req, res, next) {
  if (req.query.user_id) {
    checkUserId(req, res, next);
  } else {
    next();
  }
});
router.get('/', function (req, res) {
  var where = {
    status: TaskModel.statusOnline // 在线的任务
  };
  if (req.query.user_id) { // 获取某个用户
    where.user_id = req.query.user_id;
  }
  if (req.query.iteration_id) { // 获取迭代对应的
    where.iteration_id = req.query.iteration_id;
  }

  TaskModel
    .findAll({
      where: where,
      include: [
        {model: UserModel},
        {model: TaskStatusModel},
        {model: TaskFollowModel},
        {model: TaskHistoryModel},
        {model: StoryModel},
        {model: IterationModel, where: {status: IterationModel.statusOnline}},
        {model: VersionModel},
        {model: ProjectModel},
        {model: TaskConcernedModel}
      ],
      order: 'status_id ASC',
      offset: req.query.offset || 0,
      limit: req.query.size || 10
    })
    .then(function (result) {
      res.json(result);
    });
});

// 新建任务
router.post('/', checkIterationId);
router.post('/', checkStoryId);
router.post('/', checkUserId);
router.post('/', checkPrevTaskIds);
router.post('/', checkTaskStausId);
router.post('/', checkVersionId);
router.post('/', function (req, res, next) { // 添加任务
  TaskModel
    .build({
      project_id: req.version.project_id,
      version_id: req.version.id,
      iteration_id: req.iteration.id,
      story_id: req.story.id,
      user_id: req.body.user_id,
      desc: req.body.desc,
      is_new: req.body.is_new,
      is_challenging: req.body.is_challenging,
      priority: req.body.priority,
      estimated_time: req.body.estimated_time,
      status_id: req.body.task_status_id,
      create_time: moment().unix(),
      remark: req.body.remark
    })
    .save()
    .then(function (task) {
      req.task = task;
      next();
    })
    .catch(function (err) {
      RouterService.json(err, res);
    });

});
router.post('/', function (req, res) { // 前置任务添加
  async.each(req.prevTaskIds, function (prevTaskId, callback) {
    TaskFollowModel
      .create({
        task_id: req.task.id,
        prev_task_id: prevTaskId,
        create_time: moment().unix()
      })
      .then(function () {
        callback();
      });
  }, function (err) {
    res.json({id: req.task.id});
  });
});

// 编辑任务
router.put('/:id', checkTaskId);
router.put('/:id', checkIterationId);
router.put('/:id', checkVersionId);
router.put('/:id', checkUserId);
router.put('/:id', checkStoryId);
router.put('/:id', checkPrevTaskIds);
router.put('/:id', function (req, res, next) {
  req.task
    .update({
      project_id: req.version.project_id,
      version_id: req.version.id,
      iteration_id: req.iteration.id,
      story_id: req.story.id,
      user_id: req.body.user_id,
      desc: req.body.desc,
      is_new: req.body.is_new,
      is_challenging: req.body.is_challenging,
{'fraction_non_alphanumeric': 0.17193601209220305, 'fraction_numerical': 0.0007557626905151782, 'mean_word_length': 3.161425576519916, 'pattern_counts': {'":': 0, '<': 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': '4527316', 'n_tokens_mistral': 2730, 'n_tokens_neox': 2717, 'n_words': 504}