| package com.example.myproject.controller; |
| |
| import cn.dev33.satoken.annotation.SaCheckLogin; |
| import cn.dev33.satoken.annotation.SaCheckRole; |
| import cn.dev33.satoken.annotation.SaMode; |
| import com.example.myproject.entity.Group; |
| import com.example.myproject.entity.GroupComments; |
| import com.example.myproject.service.GroupService; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.http.ResponseEntity; |
| import org.springframework.web.bind.annotation.*; |
| import org.springframework.web.multipart.MultipartFile; |
| |
| import java.util.HashMap; |
| import java.util.Map; |
| |
| @RestController |
| @RequestMapping("/echo/groups") |
| public class GroupController { |
| |
| @Autowired |
| private GroupService groupService; |
| |
| @PostMapping("/createGroup") |
| @SaCheckLogin |
| @SaCheckRole(value = { "chocolate", "ice-cream"}, mode = SaMode.OR) |
| public ResponseEntity<Map<String, Object>> createGroup(@RequestBody Group groupRequest) { |
| // 调用服务层方法创建小组 |
| System.out.println("Received group name: " + groupRequest.getGroupName()); |
| return groupService.createGroup(groupRequest); |
| } |
| |
| |
| // 加入小组接口 |
| @PostMapping("/{group_id}/join") |
| @SaCheckLogin |
| @SaCheckRole(value = {"chocolate", "ice-cream"}, mode = SaMode.OR) |
| public ResponseEntity<Map<String, Object>> joinGroup(@PathVariable("group_id") Long groupId, |
| @RequestBody Map<String, Long> requestBody) { |
| Long userId = requestBody.get("user_id"); |
| return groupService.joinGroup(groupId, userId); |
| } |
| |
| // 退出小组接口 |
| @PostMapping("/{group_id}/leave") |
| @SaCheckLogin |
| @SaCheckRole(value = { "chocolate", "ice-cream"}, mode = SaMode.OR) |
| public ResponseEntity<Map<String, Object>> leaveGroup(@PathVariable("group_id") Long groupId, |
| @RequestBody Map<String, Long> requestBody) { |
| Long userId = requestBody.get("user_id"); |
| return groupService.leaveGroup(groupId, userId); |
| } |
| |
| // 获取小组成员接口 |
| @GetMapping("/{group_id}/members") |
| public ResponseEntity<Map<String, Object>> getGroupMembers(@PathVariable("group_id") Long groupId) { |
| return groupService.getGroupMembers(groupId); |
| } |
| |
| //发布帖子 |
| @PostMapping("/{group_id}/createPost") |
| @SaCheckLogin |
| @SaCheckRole(value = { "chocolate", "ice-cream"}, mode = SaMode.OR) |
| public ResponseEntity<Map<String, Object>> createPost( |
| @PathVariable("group_id") Long groupId, |
| @RequestParam("user_id") Long userId, |
| @RequestParam("content") String content, |
| @RequestParam("title") String title, |
| @RequestParam(value = "images", required = false) MultipartFile[] imageFiles) { |
| |
| Map<String, Object> response = groupService.createPost(groupId, userId, content, title, imageFiles); |
| return ResponseEntity.ok(response); |
| } |
| |
| //获取某个小组内的所有帖子 |
| @GetMapping("/{group_id}/getAllPosts") |
| public ResponseEntity<Map<String, Object>> getAllPosts(@PathVariable("group_id") Long groupId) { |
| Map<String, Object> response = groupService.getAllPosts(groupId); |
| return ResponseEntity.ok(response); |
| } |
| |
| //获取所有小组名称 |
| @PostMapping("/getAllGroups") |
| public ResponseEntity<Map<String, Object>> searchGroups(@RequestBody Map<String, Object> params) { |
| Map<String, Object> response = groupService.searchGroups(params); |
| return ResponseEntity.ok(response); |
| } |
| |
| // 点赞帖子 |
| @PostMapping("/{group_post_id}/like") |
| @SaCheckLogin |
| @SaCheckRole(value = { "chocolate", "ice-cream"}, mode = SaMode.OR) |
| public ResponseEntity<Map<String, Object>> likePost( |
| @PathVariable("group_post_id") Long groupPostId) { |
| Map<String, Object> response = groupService.likePost(groupPostId); |
| return ResponseEntity.ok(response); |
| } |
| |
| // 取消点赞 |
| @PostMapping("/{group_post_id}/unlike") |
| @SaCheckLogin |
| @SaCheckRole(value = {"chocolate", "ice-cream"}, mode = SaMode.OR) |
| public ResponseEntity<Map<String, Object>> unlikePost( |
| @PathVariable("group_post_id") Long groupPostId) { |
| Map<String, Object> response = groupService.unlikePost(groupPostId); |
| return ResponseEntity.ok(response); |
| } |
| |
| // 添加评论接口 |
| @PostMapping("/{group_post_id}/comment") |
| @SaCheckLogin |
| @SaCheckRole(value = { "chocolate", "ice-cream"}, mode = SaMode.OR) |
| public ResponseEntity<Map<String, Object>> addComment( |
| |
| @PathVariable("group_post_id") Long postId, |
| @RequestBody GroupComments comment) { |
| |
| Map<String, Object> response = groupService.addComment(postId, comment); |
| return ResponseEntity.ok(response); |
| } |
| |
| } |