package main.java.servlet.task;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import main.java.bo.TaskBo;
import main.java.service.TaskService;
import main.java.util.APIObject;
import main.java.util.ServletUtil;

/**
 * Servlet implementation class AcceptTaskServlet
 */
@WebServlet(description = "领取任务", urlPatterns = { "/api/task/accept" })
public class AcceptTaskServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private static TaskService taskService = new TaskService();
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AcceptTaskServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String taskId = request.getParameter("id");
		TaskBo taskBo = taskService.queryTaskById(taskId);
		
		if (taskBo == null) {
			ServletUtil.outputJSON(response, "该任务不存在，请检查任务id");
			
			return;
		}
		
		if (!taskBo.isAcceptable()) {
			ServletUtil.outputJSON(response, "该任务不可领取，请检查任务id");
			
			return;
		}
		
		APIObject apiObject = new APIObject();
		
		if (!taskService.acceptTask(Integer.valueOf(taskId), ServletUtil.getLoginUserId(request))) {
			apiObject.setUnknowError();
		}
		else {
			apiObject.setMessage("领取任务成功");
		}
		
		ServletUtil.outputJSON(response, apiObject);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

@@ -1075,16 +1075,289 @@
 ption %7B%0A
+%09%09ServletUtil.outputJSON(response, %22%E8%AF%B7%E4%BD%BF%E7%94%A8 POST %E6%96%B9%E5%BC%8F%E6%8F%90%E4%BA%A4%22);%0A%09%7D%0A%0A%09/**%0A%09 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)%0A%09 */%0A%09protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException %7B%0A
 %09%09String
@@ -1941,254 +1941,6 @@
 %09%7D%0A%0A
-%09/**%0A%09 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)%0A%09 */%0A%09protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException %7B%0A%09%09doGet(request, response);%0A%09%7D%0A%0A
 %7D%0A

修正 领取任务接口 的请求方式权限，改为 仅支持POST请求