22301138 | 5e9c35a | 2025-06-04 15:52:45 +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 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 36 | @PostMapping("/{user_id}/feeds/{dynamic_id}/comments") |
| 37 | public ResponseEntity<Map<String, Object>> addComment( |
| 38 | @PathVariable("user_id") Long userId, |
| 39 | @PathVariable("dynamic_id") Long dynamicId, |
JinGe | fe5140c | 2025-06-06 20:07:42 +0800 | [diff] [blame^] | 40 | @RequestBody Map<String, Object> content) { |
| 41 | String commentContent = (String) content.get("content"); |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 42 | |
JinGe | fe5140c | 2025-06-06 20:07:42 +0800 | [diff] [blame^] | 43 | Long parentCommentId = content.containsKey("parent_comment_id") && content.get("parent_comment_id") != null ? |
| 44 | Long.parseLong(content.get("parent_comment_id").toString()) : null; |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 45 | |
JinGe | fe5140c | 2025-06-06 20:07:42 +0800 | [diff] [blame^] | 46 | Map<String, Object> response = dynamicService.addComment(userId, dynamicId, commentContent, parentCommentId); |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 47 | return ResponseEntity.ok(response); |
| 48 | } |
| 49 | |
JinGe | fe5140c | 2025-06-06 20:07:42 +0800 | [diff] [blame^] | 50 | |
| 51 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 52 | //获取某个好友的所有动态 |
| 53 | @GetMapping("/{user_id}/getAdynamic") |
| 54 | public ResponseEntity<Map<String, Object>> getAllUserDynamics(@PathVariable("user_id") Long userId) { |
| 55 | Map<String, Object> response = dynamicService.getAllUserDynamics(userId); |
| 56 | |
| 57 | if (response == null || response.isEmpty()) { |
| 58 | return ResponseEntity.noContent().build(); |
| 59 | } |
| 60 | |
| 61 | // 返回响应 |
| 62 | return ResponseEntity.ok(response); |
| 63 | } |
| 64 | |
| 65 | @GetMapping("/{user_id}/getAllDynamics") |
| 66 | public ResponseEntity<Map<String, Object>> getAllUserAndFriendsDynamics(@PathVariable("user_id") Long userId) { |
| 67 | // 获取当前用户所有好友的ID |
| 68 | List<Long> friendIds = dynamicService.getAllFriendIds(userId); |
| 69 | friendIds.add(userId); |
| 70 | Map<String, Object> response = dynamicService.getAllUserAndFriendsDynamics(friendIds); |
| 71 | |
| 72 | if (response == null || response.isEmpty()) { |
| 73 | return ResponseEntity.noContent().build(); |
| 74 | } |
| 75 | |
| 76 | return ResponseEntity.ok(response); |
| 77 | } |
| 78 | |
| 79 | //点赞好友动态 |
| 80 | @PostMapping("/like") |
| 81 | public ResponseEntity<String> likeDynamic(@RequestBody Map<String, Long> request) { |
| 82 | Long userId = request.get("userId"); |
| 83 | Long dynamicId = request.get("dynamicId"); |
| 84 | // 执行点赞 |
| 85 | boolean success = dynamicService.likeDynamic(userId, dynamicId); |
| 86 | |
| 87 | if (success) { |
| 88 | return ResponseEntity.ok("Like successful"); |
| 89 | } else { |
| 90 | return ResponseEntity.badRequest().body("Like failed"); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | @DeleteMapping("/unlike") |
| 95 | public ResponseEntity<String> unlikeDynamic(@RequestBody Map<String, Long> request) { |
| 96 | Long userId = request.get("userId"); |
| 97 | Long dynamicId = request.get("dynamicId"); |
| 98 | |
| 99 | // 执行取消点赞 |
| 100 | boolean success = dynamicService.unlikeDynamic(userId, dynamicId); |
| 101 | |
| 102 | if (success) { |
| 103 | return ResponseEntity.ok("Unlike successful"); |
| 104 | } else { |
| 105 | return ResponseEntity.badRequest().body("Unlike failed"); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | |
| 110 | } |