QuestionController.java 4.6 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 long 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. /** 参数处理处理 */
  70. for (String key : allParams.keySet()) {
  71. Object value = allParams.get(key);
  72. if (key.equals("skip")) {
  73. skip = Long.valueOf(String.valueOf(value));
  74. } else if (key.equals("limit")) {
  75. limit = Long.valueOf(String.valueOf(value));
  76. } else {
  77. if(key.equals("title")||key.equals("platform")){
  78. qw.like(key,value);
  79. } else {
  80. // 其他为查询条件
  81. qw.eq(key, value);
  82. }
  83. }
  84. }
  85. /** 分页处理 */
  86. if (null != skip && null != limit) {
  87. IPage page = new Page<>(skip, limit);
  88. IPage pageResult = service.page(page, qw);
  89. List data = pageResult.getRecords();
  90. long total = pageResult.getTotal();
  91. map.put("data", data);
  92. map.put("total", total);
  93. } else {
  94. List list = service.list(qw);
  95. map.put("data", list);
  96. }
  97. return ResponseFormat.success(map);
  98. }
  99. /** 根据id查询 */
  100. @ApiOperation("查询数据")
  101. @GetMapping("/{id}")
  102. public Object fetch(@PathVariable long id) {
  103. Object newData = service.getById(id);
  104. return ResponseFormat.success(newData);
  105. }
  106. /** 根据id删除数据 */
  107. @ApiOperation("删除数据")
  108. @DeleteMapping("/{id}")
  109. public Object delete(@PathVariable long id) {
  110. QueryWrapper qw = new QueryWrapper<>();
  111. qw.eq("id", id);
  112. Long num = service.count(qw);
  113. if (num <= 0) {
  114. throw new CustomizationException(ExceptionEnum.NOT_FOUND);
  115. }
  116. service.removeById(id);
  117. return ResponseFormat.success();
  118. }
  119. @ApiOperation("初始化问题")
  120. @PostMapping("/initData")
  121. public Object initData() throws JsonProcessingException {
  122. String filePath = "db/question.json";
  123. ObjectMapper mapper = new ObjectMapper();
  124. String str = Utils.readJsonFile(filePath);
  125. CollectionType javaType = mapper.getTypeFactory().constructCollectionType(List.class, Map.class);
  126. List<Map> list = mapper.readValue(str, javaType);
  127. this.service.initData(list);
  128. return ResponseFormat.success();
  129. }
  130. }