QuestionController.java 4.7 KB

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