blob: c9b0ea7d8fcfefc7f51df9c6dac53aedec982d92 [file] [log] [blame]
223011385e9c35a2025-06-04 15:52:45 +08001package com.example.myproject.controller;
2
Jinf50fba62025-06-09 22:47:24 +08003import cn.dev33.satoken.annotation.SaCheckLogin;
4import cn.dev33.satoken.annotation.SaCheckRole;
5import cn.dev33.satoken.annotation.SaMode;
223011385e9c35a2025-06-04 15:52:45 +08006import com.example.myproject.entity.Group;
7import com.example.myproject.entity.GroupComments;
8import com.example.myproject.service.GroupService;
9import org.springframework.beans.factory.annotation.Autowired;
10import org.springframework.http.ResponseEntity;
11import org.springframework.web.bind.annotation.*;
12import org.springframework.web.multipart.MultipartFile;
13
14import java.util.HashMap;
15import java.util.Map;
16
17@RestController
18@RequestMapping("/echo/groups")
19public class GroupController {
20
21 @Autowired
22 private GroupService groupService;
23
24 @PostMapping("/createGroup")
Jinf50fba62025-06-09 22:47:24 +080025 @SaCheckLogin
26 @SaCheckRole(value = { "chocolate", "ice-cream"}, mode = SaMode.OR)
223011385e9c35a2025-06-04 15:52:45 +080027 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")
Jinf50fba62025-06-09 22:47:24 +080036 @SaCheckLogin
37 @SaCheckRole(value = {"chocolate", "ice-cream"}, mode = SaMode.OR)
223011385e9c35a2025-06-04 15:52:45 +080038 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")
Jinf50fba62025-06-09 22:47:24 +080046 @SaCheckLogin
47 @SaCheckRole(value = { "chocolate", "ice-cream"}, mode = SaMode.OR)
223011385e9c35a2025-06-04 15:52:45 +080048 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")
Jinf50fba62025-06-09 22:47:24 +080062 @SaCheckLogin
63 @SaCheckRole(value = { "chocolate", "ice-cream"}, mode = SaMode.OR)
223011385e9c35a2025-06-04 15:52:45 +080064 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")
Jinf50fba62025-06-09 22:47:24 +080091 @SaCheckLogin
92 @SaCheckRole(value = { "chocolate", "ice-cream"}, mode = SaMode.OR)
223011385e9c35a2025-06-04 15:52:45 +080093 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")
Jinf50fba62025-06-09 22:47:24 +0800101 @SaCheckLogin
102 @SaCheckRole(value = {"chocolate", "ice-cream"}, mode = SaMode.OR)
223011385e9c35a2025-06-04 15:52:45 +0800103 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")
Jinf50fba62025-06-09 22:47:24 +0800111 @SaCheckLogin
112 @SaCheckRole(value = { "chocolate", "ice-cream"}, mode = SaMode.OR)
223011385e9c35a2025-06-04 15:52:45 +0800113 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}