22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 1 | package com.example.myproject.controller; |
| 2 | |
| 3 | import com.example.myproject.entity.Group; |
| 4 | import com.example.myproject.entity.GroupComments; |
| 5 | import com.example.myproject.service.GroupService; |
| 6 | import org.springframework.beans.factory.annotation.Autowired; |
| 7 | import org.springframework.http.ResponseEntity; |
| 8 | import org.springframework.web.bind.annotation.*; |
| 9 | import org.springframework.web.multipart.MultipartFile; |
| 10 | |
| 11 | import java.util.HashMap; |
| 12 | import java.util.Map; |
| 13 | |
| 14 | @RestController |
| 15 | @RequestMapping("/echo/groups") |
| 16 | public class GroupController { |
| 17 | |
| 18 | @Autowired |
| 19 | private GroupService groupService; |
| 20 | |
| 21 | @PostMapping("/createGroup") |
| 22 | public ResponseEntity<Map<String, Object>> createGroup(@RequestBody Group groupRequest) { |
| 23 | // 调用服务层方法创建小组 |
| 24 | System.out.println("Received group name: " + groupRequest.getGroupName()); |
| 25 | return groupService.createGroup(groupRequest); |
| 26 | } |
| 27 | |
| 28 | |
| 29 | // 加入小组接口 |
| 30 | @PostMapping("/{group_id}/join") |
| 31 | public ResponseEntity<Map<String, Object>> joinGroup(@PathVariable("group_id") Long groupId, |
| 32 | @RequestBody Map<String, Long> requestBody) { |
| 33 | Long userId = requestBody.get("user_id"); |
| 34 | return groupService.joinGroup(groupId, userId); |
| 35 | } |
| 36 | |
| 37 | // 退出小组接口 |
| 38 | @PostMapping("/{group_id}/leave") |
| 39 | public ResponseEntity<Map<String, Object>> leaveGroup(@PathVariable("group_id") Long groupId, |
| 40 | @RequestBody Map<String, Long> requestBody) { |
| 41 | Long userId = requestBody.get("user_id"); |
| 42 | return groupService.leaveGroup(groupId, userId); |
| 43 | } |
| 44 | |
| 45 | // 获取小组成员接口 |
| 46 | @GetMapping("/{group_id}/members") |
| 47 | public ResponseEntity<Map<String, Object>> getGroupMembers(@PathVariable("group_id") Long groupId) { |
| 48 | return groupService.getGroupMembers(groupId); |
| 49 | } |
| 50 | |
| 51 | //发布帖子 |
| 52 | @PostMapping("/{group_id}/createPost") |
| 53 | public ResponseEntity<Map<String, Object>> createPost( |
| 54 | @PathVariable("group_id") Long groupId, |
| 55 | @RequestParam("user_id") Long userId, |
| 56 | @RequestParam("content") String content, |
| 57 | @RequestParam("title") String title, |
| 58 | @RequestParam(value = "images", required = false) MultipartFile[] imageFiles) { |
| 59 | |
| 60 | Map<String, Object> response = groupService.createPost(groupId, userId, content, title, imageFiles); |
| 61 | return ResponseEntity.ok(response); |
| 62 | } |
| 63 | |
| 64 | //获取某个小组内的所有帖子 |
| 65 | @GetMapping("/{group_id}/getAllPosts") |
| 66 | public ResponseEntity<Map<String, Object>> getAllPosts(@PathVariable("group_id") Long groupId) { |
| 67 | Map<String, Object> response = groupService.getAllPosts(groupId); |
| 68 | return ResponseEntity.ok(response); |
| 69 | } |
| 70 | |
| 71 | //获取所有小组名称 |
| 72 | @PostMapping("/getAllGroups") |
| 73 | public ResponseEntity<Map<String, Object>> searchGroups(@RequestBody Map<String, Object> params) { |
| 74 | Map<String, Object> response = groupService.searchGroups(params); |
| 75 | return ResponseEntity.ok(response); |
| 76 | } |
| 77 | |
| 78 | // 点赞帖子 |
| 79 | @PostMapping("/{group_post_id}/like") |
| 80 | public ResponseEntity<Map<String, Object>> likePost( |
| 81 | @PathVariable("group_post_id") Long groupPostId) { |
| 82 | Map<String, Object> response = groupService.likePost(groupPostId); |
| 83 | return ResponseEntity.ok(response); |
| 84 | } |
| 85 | |
| 86 | // 取消点赞 |
| 87 | @PostMapping("/{group_post_id}/unlike") |
| 88 | public ResponseEntity<Map<String, Object>> unlikePost( |
| 89 | @PathVariable("group_post_id") Long groupPostId) { |
| 90 | Map<String, Object> response = groupService.unlikePost(groupPostId); |
| 91 | return ResponseEntity.ok(response); |
| 92 | } |
| 93 | |
| 94 | // 添加评论接口 |
| 95 | @PostMapping("/{group_post_id}/comment") |
| 96 | public ResponseEntity<Map<String, Object>> addComment( |
| 97 | |
| 98 | @PathVariable("group_post_id") Long postId, |
| 99 | @RequestBody GroupComments comment) { |
| 100 | |
| 101 | Map<String, Object> response = groupService.addComment(postId, comment); |
| 102 | return ResponseEntity.ok(response); |
| 103 | } |
| 104 | |
| 105 | } |