|
@@ -1,69 +1,97 @@
|
|
|
package com.free.controller;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.free.config.CustomizationException;
|
|
|
+import com.free.config.ExceptionEnum;
|
|
|
+import com.free.config.ResponseFormat;
|
|
|
import com.free.entity.Test;
|
|
|
-import com.free.frame.CustomizationException;
|
|
|
-import com.free.frame.ExceptionEnum;
|
|
|
-import com.free.frame.ResponseFormat;
|
|
|
+import com.free.mq.MqListeners;
|
|
|
import com.free.service.TestService;
|
|
|
+
|
|
|
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
@RestController
|
|
|
@RequestMapping("/test")
|
|
|
public class TestController {
|
|
|
- @Autowired
|
|
|
- private TestService testService;
|
|
|
+ @Autowired
|
|
|
+ private TestService testService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ RabbitTemplate rabbitTemplate; // 使用RabbitTemplate,这提供了接收/发送等等方法
|
|
|
|
|
|
- /**
|
|
|
- * 列表查询
|
|
|
- * @return
|
|
|
- */
|
|
|
- @GetMapping()
|
|
|
- public Object list() {
|
|
|
- List list = testService.list();
|
|
|
- return ResponseFormat.success(list);
|
|
|
- }
|
|
|
+ @GetMapping("/mq/send")
|
|
|
+ public Object mqSendTest() {
|
|
|
+ String messageId = String.valueOf(UUID.randomUUID());
|
|
|
+ String messageData = "message: M A N ";
|
|
|
+ String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
|
|
+ Map<String, Object> manMap = new HashMap<>();
|
|
|
+ manMap.put("messageId", messageId);
|
|
|
+ manMap.put("messageData", messageData);
|
|
|
+ manMap.put("createTime", createTime);
|
|
|
+ rabbitTemplate.convertAndSend(MqListeners.exName, MqListeners.queueName + "1", manMap);
|
|
|
+ return ResponseFormat.success();
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 创建数据
|
|
|
- * @param test
|
|
|
- * @return
|
|
|
- */
|
|
|
- @PostMapping()
|
|
|
- public Object save(@RequestBody Test test) {
|
|
|
- boolean result = testService.save(test);
|
|
|
- return ResponseFormat.success((result));
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 列表查询
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @GetMapping()
|
|
|
+ public Object list() {
|
|
|
+ List list = testService.list();
|
|
|
+ return ResponseFormat.success(list);
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 修改数据
|
|
|
- * @param id
|
|
|
- * @param test
|
|
|
- * @return
|
|
|
- */
|
|
|
- @PostMapping("/{id}")
|
|
|
- public Object update(@PathVariable long id, @RequestBody Test test) {
|
|
|
- QueryWrapper<Test> qw = new QueryWrapper<Test>();
|
|
|
- qw.eq("id", id);
|
|
|
- Long num = testService.count(qw);
|
|
|
- if (num <= 0) throw new CustomizationException(ExceptionEnum.NOT_FOUND);
|
|
|
- testService.updateById(test);
|
|
|
- Test newData = testService.getById(id);
|
|
|
- return ResponseFormat.success(newData);
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 创建数据
|
|
|
+ *
|
|
|
+ * @param test
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @PostMapping()
|
|
|
+ public Object save(@RequestBody Test test) {
|
|
|
+ boolean result = testService.save(test);
|
|
|
+ return ResponseFormat.success((result));
|
|
|
+ }
|
|
|
|
|
|
- @DeleteMapping("/{id}")
|
|
|
- public Object delete(@PathVariable long id) {
|
|
|
- QueryWrapper<Test> qw = new QueryWrapper<Test>();
|
|
|
- qw.eq("id", id);
|
|
|
- Long num = testService.count(qw);
|
|
|
- if (num <= 0) throw new CustomizationException(ExceptionEnum.NOT_FOUND);
|
|
|
- testService.removeById(id);
|
|
|
- return ResponseFormat.success();
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 修改数据
|
|
|
+ *
|
|
|
+ * @param id
|
|
|
+ * @param test
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @PostMapping("/{id}")
|
|
|
+ public Object update(@PathVariable long id, @RequestBody Test test) {
|
|
|
+ QueryWrapper<Test> qw = new QueryWrapper<Test>();
|
|
|
+ qw.eq("id", id);
|
|
|
+ Long num = testService.count(qw);
|
|
|
+ if (num <= 0)
|
|
|
+ throw new CustomizationException(ExceptionEnum.NOT_FOUND);
|
|
|
+ testService.updateById(test);
|
|
|
+ Test newData = testService.getById(id);
|
|
|
+ return ResponseFormat.success(newData);
|
|
|
+ }
|
|
|
|
|
|
+ @DeleteMapping("/{id}")
|
|
|
+ public Object delete(@PathVariable long id) {
|
|
|
+ QueryWrapper<Test> qw = new QueryWrapper<Test>();
|
|
|
+ qw.eq("id", id);
|
|
|
+ Long num = testService.count(qw);
|
|
|
+ if (num <= 0)
|
|
|
+ throw new CustomizationException(ExceptionEnum.NOT_FOUND);
|
|
|
+ testService.removeById(id);
|
|
|
+ return ResponseFormat.success();
|
|
|
+ }
|
|
|
|
|
|
}
|