22301138 | f682451 | 2025-06-04 02:03:13 +0800 | [diff] [blame] | 1 | package com.example.myproject.controller; |
| 2 | |
| 3 | import com.example.myproject.entity.UserDynamic; |
| 4 | import com.example.myproject.service.DynamicService; |
| 5 | import org.springframework.beans.factory.annotation.Autowired; |
| 6 | import org.springframework.http.ResponseEntity; |
| 7 | import org.springframework.web.bind.annotation.*; |
| 8 | import org.springframework.web.multipart.MultipartFile; |
| 9 | |
| 10 | import java.util.List; |
| 11 | import java.util.Map; |
| 12 | |
| 13 | @RestController |
| 14 | @RequestMapping("/echo/dynamic") |
| 15 | public class DynamicController { |
| 16 | |
| 17 | @Autowired |
| 18 | private DynamicService dynamicService; |
| 19 | |
| 20 | // 创建好友动态接口 |
| 21 | @PostMapping("/{user_id}/createDynamic") |
| 22 | public Map<String, Object> createDynamic(@PathVariable("user_id") Long userId, |
| 23 | @RequestParam(value = "title", required = false) String title, |
| 24 | @RequestParam(value = "content") String content, |
| 25 | @RequestParam(value = "image_url", required = false) MultipartFile[] imageFiles) { |
| 26 | return dynamicService.createDynamic(userId, title, content, imageFiles); |
| 27 | } |
| 28 | |
| 29 | //删除好友动态 |
| 30 | @DeleteMapping("/me/deleteDynamic/{dynamic_id}") |
| 31 | public ResponseEntity<String> deleteDynamic(@PathVariable("dynamic_id") Long dynamicId) { |
| 32 | dynamicService.deleteDynamic(dynamicId); |
| 33 | return ResponseEntity.ok("动态删除成功"); |
| 34 | } |
| 35 | |
| 36 | //好友动态评论 |
| 37 | @PostMapping("/{user_id}/feeds/{dynamic_id}/comments") |
| 38 | public ResponseEntity<Map<String, Object>> addComment( |
| 39 | @PathVariable("user_id") Long userId, |
| 40 | @PathVariable("dynamic_id") Long dynamicId, |
| 41 | @RequestBody Map<String, String> content) { |
| 42 | |
| 43 | String commentContent = content.get("content"); // 获取评论内容 |
| 44 | Map<String, Object> response = dynamicService.addComment(userId, dynamicId, commentContent); |
| 45 | |
| 46 | return ResponseEntity.ok(response); |
| 47 | } |
| 48 | |
| 49 | //获取某个好友的所有动态 |
| 50 | @GetMapping("/{user_id}/getAdynamic") |
| 51 | public ResponseEntity<Map<String, Object>> getAllUserDynamics(@PathVariable("user_id") Long userId) { |
| 52 | Map<String, Object> response = dynamicService.getAllUserDynamics(userId); |
| 53 | |
| 54 | if (response == null || response.isEmpty()) { |
| 55 | return ResponseEntity.noContent().build(); |
| 56 | } |
| 57 | |
| 58 | // 返回响应 |
| 59 | return ResponseEntity.ok(response); |
| 60 | } |
| 61 | |
| 62 | @GetMapping("/{user_id}/getAllDynamics") |
| 63 | public ResponseEntity<Map<String, Object>> getAllUserAndFriendsDynamics(@PathVariable("user_id") Long userId) { |
| 64 | // 获取当前用户所有好友的ID |
| 65 | List<Long> friendIds = dynamicService.getAllFriendIds(userId); |
| 66 | friendIds.add(userId); |
| 67 | Map<String, Object> response = dynamicService.getAllUserAndFriendsDynamics(friendIds); |
| 68 | |
| 69 | if (response == null || response.isEmpty()) { |
| 70 | return ResponseEntity.noContent().build(); |
| 71 | } |
| 72 | |
| 73 | return ResponseEntity.ok(response); |
| 74 | } |
| 75 | |
| 76 | //点赞好友动态 |
| 77 | @PostMapping("/like") |
| 78 | public ResponseEntity<String> likeDynamic(@RequestBody Map<String, Long> request) { |
| 79 | Long userId = request.get("userId"); |
| 80 | Long dynamicId = request.get("dynamicId"); |
| 81 | // 执行点赞 |
| 82 | boolean success = dynamicService.likeDynamic(userId, dynamicId); |
| 83 | |
| 84 | if (success) { |
| 85 | return ResponseEntity.ok("Like successful"); |
| 86 | } else { |
| 87 | return ResponseEntity.badRequest().body("Like failed"); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | @DeleteMapping("/unlike") |
| 92 | public ResponseEntity<String> unlikeDynamic(@RequestBody Map<String, Long> request) { |
| 93 | Long userId = request.get("userId"); |
| 94 | Long dynamicId = request.get("dynamicId"); |
| 95 | |
| 96 | // 执行取消点赞 |
| 97 | boolean success = dynamicService.unlikeDynamic(userId, dynamicId); |
| 98 | |
| 99 | if (success) { |
| 100 | return ResponseEntity.ok("Unlike successful"); |
| 101 | } else { |
| 102 | return ResponseEntity.badRequest().body("Unlike failed"); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | |
| 107 | } |