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