22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame^] | 1 | package com.example.myproject.controller; |
| 2 | |
| 3 | import com.example.myproject.service.UserFollowService; |
| 4 | import org.springframework.beans.factory.annotation.Autowired; |
| 5 | import org.springframework.http.ResponseEntity; |
| 6 | import org.springframework.web.bind.annotation.*; |
| 7 | |
| 8 | import java.util.Map; |
| 9 | |
| 10 | @RestController |
| 11 | @RequestMapping("/echo/users") |
| 12 | public class UserFollowController { |
| 13 | |
| 14 | @Autowired |
| 15 | private UserFollowService userFollowService; |
| 16 | |
| 17 | // 用户关注接口 |
| 18 | @PostMapping("/{followed_id}/follow") |
| 19 | public ResponseEntity<Map<String, Object>> follow(@PathVariable("followed_id") Long followedId, |
| 20 | @RequestBody Map<String, Long> request) { |
| 21 | Long followerId = request.get("follower_id"); |
| 22 | Map<String, Object> response = userFollowService.follow(followerId, followedId); |
| 23 | return ResponseEntity.ok(response); |
| 24 | } |
| 25 | |
| 26 | // 取消关注接口 |
| 27 | @PostMapping("/{followed_id}/unfollow") |
| 28 | public ResponseEntity<Map<String, Object>> unfollow(@PathVariable("followed_id") Long followedId, |
| 29 | @RequestBody Map<String, Long> request) { |
| 30 | Long followerId = request.get("follower_id"); |
| 31 | Map<String, Object> response = userFollowService.unfollow(followerId, followedId); |
| 32 | return ResponseEntity.ok(response); |
| 33 | } |
| 34 | |
| 35 | // 获取某个用户的粉丝列表 |
| 36 | @GetMapping("/{user_id}/followers") |
| 37 | public ResponseEntity<Map<String, Object>> getFollowers(@PathVariable("user_id") Long userId) { |
| 38 | Map<String, Object> response = userFollowService.getFollowers(userId); |
| 39 | return ResponseEntity.ok(response); |
| 40 | } |
| 41 | |
| 42 | @GetMapping("/{user_id}/following") |
| 43 | public ResponseEntity<Map<String, Object>> getFollowing(@PathVariable("user_id") Long userId) { |
| 44 | Map<String, Object> response = userFollowService.getFollowing(userId); |
| 45 | return ResponseEntity.ok(response); |
| 46 | } |
| 47 | } |