From 618a07d435545c96f62a717da47efc2f2323a3c6 Mon Sep 17 00:00:00 2001 From: ronger Date: Tue, 8 Dec 2020 20:00:25 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=AF=8F=E6=97=A5=E4=B8=80?= =?UTF-8?q?=E9=A2=98=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rymcu/forest/answer/AnswerController.java | 48 +++++++++++++++++++ .../java/com/rymcu/forest/dto/AnswerDTO.java | 17 +++++++ .../java/com/rymcu/forest/util/HttpUtils.java | 34 +++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 src/main/java/com/rymcu/forest/answer/AnswerController.java create mode 100644 src/main/java/com/rymcu/forest/dto/AnswerDTO.java create mode 100644 src/main/java/com/rymcu/forest/util/HttpUtils.java 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; + } +}