diff --git a/src/main/java/com/rymcu/forest/answer/AnswerController.java b/src/main/java/com/rymcu/forest/answer/AnswerController.java new file mode 100644 index 0000000..0ebfd5c --- /dev/null +++ b/src/main/java/com/rymcu/forest/answer/AnswerController.java @@ -0,0 +1,48 @@ +package com.rymcu.forest.answer; + +import com.alibaba.fastjson.JSONObject; +import com.rymcu.forest.core.result.GlobalResult; +import com.rymcu.forest.core.result.GlobalResultGenerator; +import com.rymcu.forest.dto.AnswerDTO; +import com.rymcu.forest.entity.User; +import com.rymcu.forest.util.HttpUtils; +import com.rymcu.forest.util.UserUtils; +import com.rymcu.forest.web.api.exception.BaseApiException; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author ronger + */ +@RestController +@RequestMapping("/api/v1/answer") +public class AnswerController { + + private final static String ANSWER_API_URL = "http://101.132.237.86:8089/question"; + + @GetMapping("/today") + public GlobalResult today() throws BaseApiException { + User user = UserUtils.getCurrentUserByToken(); + String result = HttpUtils.sendGet(ANSWER_API_URL + "/record/" + user.getIdUser() ); + return JSONObject.parseObject(result, GlobalResult.class); + } + + @PostMapping("/answer") + public GlobalResult answer(@RequestBody AnswerDTO answerDTO) throws BaseApiException { + User user = UserUtils.getCurrentUserByToken(); + Map params = new HashMap<>(3); + params.put("userId", user.getIdUser()); + params.put("answer", answerDTO.getAnswer()); + params.put("subjectQuestionId", answerDTO.getIdSubjectQuestion()); + String result = HttpUtils.sendPost(ANSWER_API_URL + "/answer/everyday", params); + return JSONObject.parseObject(result, GlobalResult.class); + } + + @GetMapping("/get-answer") + public GlobalResult getAnswer(Integer idSubjectQuestion) { + String result = HttpUtils.sendGet(ANSWER_API_URL + "/show-answer/" + idSubjectQuestion ); + return JSONObject.parseObject(result, GlobalResult.class); + } +} diff --git a/src/main/java/com/rymcu/forest/dto/AnswerDTO.java b/src/main/java/com/rymcu/forest/dto/AnswerDTO.java new file mode 100644 index 0000000..7c9a208 --- /dev/null +++ b/src/main/java/com/rymcu/forest/dto/AnswerDTO.java @@ -0,0 +1,17 @@ +package com.rymcu.forest.dto; + +import lombok.Data; + +/** + * @author ronger + */ +@Data +public class AnswerDTO { + + private Integer idSubjectQuestion; + + private String answer; + + private Integer idUser; + +} diff --git a/src/main/java/com/rymcu/forest/util/HttpUtils.java b/src/main/java/com/rymcu/forest/util/HttpUtils.java new file mode 100644 index 0000000..7425c32 --- /dev/null +++ b/src/main/java/com/rymcu/forest/util/HttpUtils.java @@ -0,0 +1,34 @@ +package com.rymcu.forest.util; + +import com.alibaba.fastjson.JSON; +import jodd.http.HttpRequest; +import jodd.http.HttpResponse; + +import java.util.Map; + +/** + * @author ronger + */ +public class HttpUtils { + + public static String sendGet(String url) { + HttpRequest request = HttpRequest.get(url); + HttpResponse response = request.send(); + return response.bodyText(); + } + + public static String sendPost(String url, Map params) { + HttpRequest request = HttpRequest.post(url); + request.contentType("application/json"); + request.charset("utf-8"); + + //参数详情 + if (params != null) { + request.body(JSON.toJSONString(params)); + } + + HttpResponse response = request.send(); + String respJson = response.bodyText(); + return respJson; + } +}