✨ 每日一题系统
This commit is contained in:
parent
a8c27e29fd
commit
618a07d435
48
src/main/java/com/rymcu/forest/answer/AnswerController.java
Normal file
48
src/main/java/com/rymcu/forest/answer/AnswerController.java
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
17
src/main/java/com/rymcu/forest/dto/AnswerDTO.java
Normal file
17
src/main/java/com/rymcu/forest/dto/AnswerDTO.java
Normal file
@ -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;
|
||||||
|
|
||||||
|
}
|
34
src/main/java/com/rymcu/forest/util/HttpUtils.java
Normal file
34
src/main/java/com/rymcu/forest/util/HttpUtils.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user