package mtsar.resources;

import mtsar.api.Answer;
import mtsar.api.Process;
import mtsar.api.sql.AnswerDAO;
import mtsar.api.sql.TaskDAO;
import mtsar.api.sql.WorkerDAO;

import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

@Produces(MediaType.APPLICATION_JSON)
public class AnswerResource {
    protected final Process process;
    protected final TaskDAO taskDAO;
    protected final WorkerDAO workerDAO;
    protected final AnswerDAO answerDAO;

    public AnswerResource(Process process, TaskDAO taskDAO, WorkerDAO workerDAO, AnswerDAO answerDAO) {
        this.process = process;
        this.taskDAO = taskDAO;
        this.workerDAO = workerDAO;
        this.answerDAO = answerDAO;
    }

    @GET
    public List<Answer> getAnswers() {
        return answerDAO.listForProcess(process.getId());
    }

    @POST
    public Answer postAnswer(@FormParam("worker") String worker, @FormParam("task") String task, @FormParam("processors") String answerParam, @FormParam("timestamp") String timestampParam) {
        final Timestamp timestamp = (timestampParam == null) ?
                Timestamp.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()) :
                Timestamp.valueOf(timestampParam);
        final Answer answer = Answer.builder().
                setProcess(process.getId()).
                setAnswer(answerParam).
                setDateTime(timestamp).
                build();
        int answerId = answerDAO.insert(answer);
        return answerDAO.find(answerId, process.getId());
    }
}

@@ -188,214 +188,78 @@
 .rs.
-FormParam;%0Aimport javax.ws.rs.GET;%0Aimport javax.ws.rs.POST;%0Aimport javax.ws.rs.Produces;%0Aimport javax.ws.rs.core.MediaType;%0Aimport java.sql.Timestamp;%0Aimport java.time.LocalDateTime;%0Aimport java.time.ZoneId
+*;%0Aimport javax.ws.rs.core.MediaType;%0Aimport javax.ws.rs.core.Response
 ;%0Aim
@@ -874,12 +874,33 @@
    @
-POST
+GET%0A    @Path(%22%7Banswer%7D%22)
 %0A   
@@ -918,11 +918,10 @@
 wer 
-pos
+ge
 tAns
@@ -929,231 +929,126 @@
 er(@
-Form
+Path
 Param(%22
-work
+answ
 er%22) 
-String worker, @FormParam(%22task%22) String task, @FormParam(%22processors%22) String answerParam, @FormParam(%22timestamp%22) String timestampParam) %7B%0A        final Timestamp timestamp = (timestampParam == null) ?
+Integer id) %7B%0A        return fetchAnswer(id);%0A    %7D%0A%0A    @DELETE%0A    public void deleteAnswers() %7B
 %0A   
@@ -1056,147 +1056,97 @@
     
-        Timestamp.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()) :
+answerDAO.deleteAll(process.getId());
 %0A    
+%7D%0A%0A
     
-        Timestamp.valueOf(timestampParam);
+private Answer fetchAnswer(Integer id) %7B
 %0A   
@@ -1176,53 +1176,27 @@
 r = 
-A
+a
 nswer
-.builder().%0A                setProcess(
+DAO.find(id, 
 proc
@@ -1207,30 +1207,26 @@
 getId())
-.%0A    
+;%0A
         
     setA
@@ -1221,206 +1221,111 @@
     
-    setAnswer(answerParam).%0A                setDateTime(timestamp).%0A                build();%0A        int answerId = answerDAO.insert(answer);%0A        return answerDAO.find(answerId, process.getId())
+if (answer == null) throw new WebApplicationException(Response.Status.NOT_FOUND);%0A        return answer
 ;%0A  

Remove odd methods from AnswerResource