Browse Source

增加answer的save和update

MengyangDuan 5 years ago
parent
commit
9d76e464ab

+ 45 - 0
src/main/java/edu/nju/controller/ItemController.java

@@ -87,6 +87,51 @@ public class ItemController {
 
     }
 
+    @RequestMapping(value = "/saveAnswer")
+    @ResponseBody
+    public void saveAnswer(String item_id, String worker_id,List<String>answers,String attachment_location, HttpServletResponse response){
+        JSONObject result = new JSONObject();
+        String id = iservice.saveAnswer(item_id, worker_id, answers, attachment_location);
+        if(id.equals("")) {
+            result.put("status", "200");
+            result.put("id", id);
+        }
+        else {
+            result.put("status", "500");
+        }
+        try {
+            PrintWriter out = response.getWriter();
+            out.print(result);
+            out.flush();
+            out.close();
+        }catch (IOException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+    }
+
+    @RequestMapping(value = "/updateAnswer")
+    @ResponseBody
+    public void saveAnswer(String id, String item_id, String worker_id,List<String>answers,String attachment_location, HttpServletResponse response){
+        JSONObject result = new JSONObject();
+        if(iservice.updateAnswer(id, item_id, worker_id, answers, attachment_location)){
+            result.put("status", "200");
+        } else {
+            result.put("status", "500");
+        }
+        try {
+            PrintWriter out = response.getWriter();
+            out.print(result);
+            out.flush();
+            out.close();
+        }catch (IOException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+    }
+
+
+
     
 
 }

+ 2 - 1
src/main/java/edu/nju/dao/AnswerDao.java

@@ -15,12 +15,13 @@ public class AnswerDao {
     @Autowired
     private MongoOperations mongoOperations;
 
-    public List<Answer> findAnswersByItenWorker(String item_id,String worker_id){
+    public List<Answer> findAnswersByItemWorker(String item_id,String worker_id){
         Query query = new Query();
         query.addCriteria(Criteria.where("item_id").is(item_id).and("worker_id").is(worker_id));
         return mongoOperations.find(query, Answer.class);
     }
 
+    //存在则更新,不存在则插入
     public String save(Answer answer){
         mongoOperations.save(answer);
         return answer.getId();

+ 12 - 2
src/main/java/edu/nju/entities/Answer.java

@@ -20,11 +20,13 @@ public class Answer implements java.io.Serializable{
 
     private String worker_id;
 
-    public Answer(String id, String item_id, List<String> answers, String worker_id) {
-        this.id = id;
+    private String attachment_location;
+
+    public Answer(String item_id, List<String> answers, String worker_id, String attachment_location) {
         this.item_id = item_id;
         this.answers = answers;
         this.worker_id = worker_id;
+        this.attachment_location = attachment_location;
     }
 
 
@@ -60,6 +62,14 @@ public class Answer implements java.io.Serializable{
         this.worker_id = worker_id;
     }
 
+    public String getAttachment_location() {
+        return attachment_location;
+    }
+
+    public void setAttachment_location(String attachment_location) {
+        this.attachment_location = attachment_location;
+    }
+
 
 
 }

+ 16 - 2
src/main/java/edu/nju/service/ItemService.java

@@ -33,13 +33,27 @@ public class ItemService {
     }
 
     public List<Answer>getAnswerByWorker(String item_id,String worker_id){
-        return answerDao.findAnswersByItenWorker(item_id, worker_id);
+        return answerDao.findAnswersByItemWorker(item_id, worker_id);
     }
 
-    public String saveAnswer(Answer answer){
+    public String saveAnswer(String item_id, String worker_id,List<String>answers,String attachment_location){
+        Answer answer=new Answer(item_id,answers,worker_id,attachment_location);
         return answerDao.save(answer);
     }
 
+    public boolean updateAnswer(String id, String item_id, String worker_id,List<String>answers,String attachment_location){
+        try {
+            if(id == null || id.equals("undefined")) { return false; }
+            Answer answer=new Answer(item_id,answers,worker_id,attachment_location);
+            answer.setId(id);
+            answerDao.save(answer);
+            return true;
+        } catch(Exception e) {
+            e.printStackTrace();
+            return false;
+        }
+    }
+
     public List<Item>getItemsByWorkerJob(String worker_id,String job_id){
         List<String>itemIds = userToItemDao.getItemsByUserJob(worker_id, job_id);
         List<Item>items=new ArrayList<>();