QuestionController.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package com.free.controller;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import javax.validation.Valid;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.web.bind.annotation.DeleteMapping;
  8. import org.springframework.web.bind.annotation.GetMapping;
  9. import org.springframework.web.bind.annotation.PathVariable;
  10. import org.springframework.web.bind.annotation.PostMapping;
  11. import org.springframework.web.bind.annotation.RequestBody;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RequestParam;
  14. import org.springframework.web.bind.annotation.RestController;
  15. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  16. import com.baomidou.mybatisplus.core.metadata.IPage;
  17. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  18. import com.fasterxml.jackson.core.JsonProcessingException;
  19. import com.fasterxml.jackson.databind.ObjectMapper;
  20. import com.fasterxml.jackson.databind.type.CollectionType;
  21. import com.free.config.CustomizationException;
  22. import com.free.config.ExceptionEnum;
  23. import com.free.config.ResponseFormat;
  24. import com.free.entity.Question;
  25. import com.free.service.QuestionService;
  26. import com.free.utils.Utils;
  27. import io.swagger.annotations.Api;
  28. import io.swagger.annotations.ApiOperation;
  29. @RestController
  30. @RequestMapping("/question")
  31. @Api(tags = "问题")
  32. public class QuestionController {
  33. @Autowired
  34. private QuestionService service;
  35. /** 创建数据 */
  36. @ApiOperation("创建数据")
  37. @PostMapping("")
  38. public Object save(@RequestBody @Valid Question data) {
  39. this.service.save(data);
  40. QueryWrapper qw = new QueryWrapper<>();
  41. qw.eq("id", data.getId());
  42. Map returnData = this.service.getMap(qw);
  43. return ResponseFormat.success(returnData);
  44. // return ResponseFormat.success();
  45. }
  46. /** 修改数据 */
  47. @ApiOperation("修改数据")
  48. @PostMapping("/{id}")
  49. public Object update(@PathVariable String id, @RequestBody Question data) {
  50. QueryWrapper qw = new QueryWrapper<>();
  51. qw.eq("id", id);
  52. Long num = this.service.count(qw);
  53. if (num <= 0) {
  54. throw new CustomizationException(ExceptionEnum.NOT_FOUND);
  55. }
  56. data.setId(id);
  57. this.service.updateById(data);
  58. Object newData = this.service.getById(id);
  59. return ResponseFormat.success(newData);
  60. }
  61. /** 列表查询 */
  62. @ApiOperation("查询数据列表")
  63. @SuppressWarnings({ "unchecked" })
  64. @GetMapping()
  65. public Object list(@RequestParam Map<String, Object> allParams) {
  66. Long skip = null, limit = null;
  67. Map map = new HashMap();
  68. QueryWrapper qw = new QueryWrapper<>();
  69. qw.select("id", "title", "platform", "is_use");
  70. /** 参数处理处理 */
  71. for (String key : allParams.keySet()) {
  72. Object value = allParams.get(key);
  73. if (key.equals("skip")) {
  74. skip = Long.valueOf(String.valueOf(value));
  75. } else if (key.equals("limit")) {
  76. limit = Long.valueOf(String.valueOf(value));
  77. } else {
  78. if (key.equals("title") || key.equals("platform")) {
  79. qw.like(key, value);
  80. } else {
  81. // 其他为查询条件
  82. qw.eq(key, value);
  83. }
  84. }
  85. }
  86. /** 分页处理 */
  87. if (null != skip && null != limit) {
  88. IPage page = new Page<>(skip, limit);
  89. IPage pageResult = service.page(page, qw);
  90. List data = pageResult.getRecords();
  91. long total = pageResult.getTotal();
  92. map.put("data", data);
  93. map.put("total", total);
  94. } else {
  95. List list = service.list(qw);
  96. map.put("data", list);
  97. }
  98. return ResponseFormat.success(map);
  99. }
  100. /** 根据id查询 */
  101. @ApiOperation("查询数据")
  102. @GetMapping("/{id}")
  103. public Object fetch(@PathVariable String id) {
  104. Object newData = service.getById(id);
  105. return ResponseFormat.success(newData);
  106. }
  107. /** 根据id删除数据 */
  108. @ApiOperation("删除数据")
  109. @DeleteMapping("/{id}")
  110. public Object delete(@PathVariable String id) {
  111. QueryWrapper qw = new QueryWrapper<>();
  112. qw.eq("id", id);
  113. Long num = service.count(qw);
  114. if (num <= 0) {
  115. throw new CustomizationException(ExceptionEnum.NOT_FOUND);
  116. }
  117. service.removeById(id);
  118. return ResponseFormat.success();
  119. }
  120. @ApiOperation("初始化问题")
  121. @PostMapping("/initData")
  122. public Object initData() throws JsonProcessingException {
  123. String filePath = "db/question.json";
  124. ObjectMapper mapper = new ObjectMapper();
  125. String str = Utils.readJsonFile(filePath);
  126. CollectionType javaType = mapper.getTypeFactory().constructCollectionType(List.class, Map.class);
  127. List<Map> list = mapper.readValue(str, javaType);
  128. this.service.initData(list);
  129. return ResponseFormat.success();
  130. }
  131. }