xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 1 | package com.g9.g9backend.controller; |
| 2 | |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 3 | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| 4 | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| 5 | import com.g9.g9backend.pojo.*; |
| 6 | import com.g9.g9backend.pojo.DTO.*; |
| 7 | import com.g9.g9backend.service.*; |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 8 | import org.slf4j.Logger; |
| 9 | import org.slf4j.LoggerFactory; |
xiukira | 29f8d37 | 2025-06-06 18:07:13 +0800 | [diff] [blame] | 10 | import org.springframework.http.HttpStatus; |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 11 | import org.springframework.http.ResponseEntity; |
| 12 | import org.springframework.web.bind.annotation.*; |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 13 | |
| 14 | /** |
| 15 | * UserController 用户控制器类,处理与用户相关的请求 |
| 16 | * |
| 17 | * @author hcy |
| 18 | */ |
| 19 | @RestController |
| 20 | @RequestMapping("/user") |
| 21 | public class UserController { |
| 22 | |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 23 | private final UserService userService; |
| 24 | |
| 25 | private final InvitationService invitationService; |
| 26 | |
| 27 | private final SubscriptionService subscriptionService; |
| 28 | |
xiukira | 29f8d37 | 2025-06-06 18:07:13 +0800 | [diff] [blame] | 29 | private final SearchHistoryService searchHistoryService; |
| 30 | |
| 31 | public UserController(UserService userService, InvitationService invitationService, SubscriptionService subscriptionService, SearchHistoryService searchHistoryService) { |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 32 | this.userService = userService; |
| 33 | this.invitationService = invitationService; |
| 34 | this.subscriptionService = subscriptionService; |
xiukira | 29f8d37 | 2025-06-06 18:07:13 +0800 | [diff] [blame] | 35 | this.searchHistoryService = searchHistoryService; |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 36 | } |
| 37 | |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 38 | private final Logger logger = LoggerFactory.getLogger(UserController.class); |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 39 | |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 40 | /** |
| 41 | * 用户注册 |
| 42 | * |
| 43 | * @param registerDTO 用户注册 |
| 44 | * @return 注册结果 |
| 45 | */ |
| 46 | @PostMapping("/register") |
| 47 | public ResponseEntity<String> register(@RequestBody RegisterDTO registerDTO) { |
| 48 | String username = registerDTO.getUsername(); |
| 49 | String password = registerDTO.getPassword(); |
| 50 | String invitationCode = registerDTO.getInvitationCode(); |
| 51 | logger.info("Register request received for account: {}", username); |
xiukira | 687b9cb | 2025-05-29 15:15:02 +0800 | [diff] [blame] | 52 | |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 53 | // 根据用户名查询该用户名是否已存在 |
| 54 | QueryWrapper<User> userQuery = new QueryWrapper<>(); |
| 55 | userQuery.eq("username", username); |
| 56 | User userCheck = userService.getOne(userQuery); |
| 57 | |
| 58 | if (userCheck != null) { |
| 59 | // 用户名重复 |
| 60 | logger.warn("Registration attempt failed. Account already exists: {}", username); |
| 61 | return ResponseEntity.status(407).body(""); |
| 62 | } |
| 63 | |
| 64 | // 查询邀请码是否存在 |
| 65 | QueryWrapper<Invitation> invitationQuery = new QueryWrapper<>(); |
| 66 | invitationQuery.eq("invitation_code", invitationCode); |
| 67 | Invitation invitation = invitationService.getOne(invitationQuery); |
| 68 | |
| 69 | if (invitation == null) { |
| 70 | // 邀请码不存在 |
| 71 | logger.info("The invitation code does not exist: {}", invitationCode); |
| 72 | return ResponseEntity.status(409).body(""); |
| 73 | } else if (invitation.getInviteeId() != 0) { |
| 74 | // 邀请码已被使用 |
| 75 | logger.info("The invitation code has been used: {}", invitationCode); |
| 76 | return ResponseEntity.status(410).body(""); |
| 77 | } |
| 78 | // 注册 |
| 79 | // 添加新用户 |
| 80 | User user = new User(); |
| 81 | user.setUsername(username); |
| 82 | user.setPassword(password); |
| 83 | userService.save(user); |
| 84 | |
| 85 | // 设置该邀请码已被使用 |
| 86 | User userGetId = userService.getOne(userQuery); |
| 87 | int newUserId = userGetId.getUserId(); |
| 88 | |
xiukira | 29f8d37 | 2025-06-06 18:07:13 +0800 | [diff] [blame] | 89 | UpdateWrapper<Invitation> invitationUpdate = new UpdateWrapper<>(); |
| 90 | invitationUpdate.eq("invitation_code", invitationCode).set("invitee_id", newUserId); |
| 91 | invitationService.update(invitationUpdate); |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 92 | |
| 93 | // 生成五个邀请码并分配给新用户 |
| 94 | String[] invitationCodes = invitationService.generateInvitationCode(); |
| 95 | |
| 96 | for (String code : invitationCodes) { |
| 97 | Invitation newInvitation = new Invitation(); |
| 98 | newInvitation.setInvitationCode(code); |
| 99 | newInvitation.setUserId(newUserId); |
| 100 | invitationService.save(newInvitation); |
| 101 | } |
| 102 | |
| 103 | logger.info("User registered successfully: {}", username); |
| 104 | return ResponseEntity.ok(""); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * 用户登录 |
| 109 | * |
| 110 | * @param user 登录信息 |
| 111 | * @return 登录结果 |
| 112 | */ |
| 113 | @PostMapping("/login") |
| 114 | public ResponseEntity<String> login(@RequestBody User user) { |
| 115 | String username = user.getUsername(); |
| 116 | String password = user.getPassword(); |
| 117 | logger.info("Login attempt for account: {}", username); |
| 118 | |
| 119 | // 根据用户名查询该用户名是否已存在 |
| 120 | QueryWrapper<User> userQuery = new QueryWrapper<>(); |
| 121 | userQuery.eq("username", username); |
| 122 | User userCheck = userService.getOne(userQuery); |
| 123 | |
| 124 | if (userCheck == null) { |
| 125 | // 用户名不存在 |
| 126 | logger.warn("Login failed. User not found: {}", username); |
| 127 | return ResponseEntity.status(406).body(""); |
| 128 | } else { |
| 129 | if (userCheck.getPassword().equals(password)) { |
| 130 | return ResponseEntity.ok(""); |
| 131 | } else { |
| 132 | // 密码错误 |
| 133 | logger.warn("Login failed. Incorrect password for account: {}", username); |
| 134 | return ResponseEntity.status(408).body(""); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * 关注 |
| 141 | * |
| 142 | * @param subscription 关注信息 |
| 143 | * @return 关注结果 |
| 144 | */ |
| 145 | @PostMapping("/subscription") |
| 146 | public ResponseEntity<String> subscription(@RequestBody Subscription subscription) { |
| 147 | subscriptionService.save(subscription); |
| 148 | return ResponseEntity.ok(""); |
| 149 | } |
xiukira | 29f8d37 | 2025-06-06 18:07:13 +0800 | [diff] [blame] | 150 | |
| 151 | /** |
| 152 | * 注销账户 |
| 153 | * |
| 154 | * @param userId 用户id |
| 155 | * @param password 密码 |
| 156 | * @return 注销结果 |
| 157 | */ |
| 158 | @DeleteMapping |
| 159 | @ResponseStatus(HttpStatus.NO_CONTENT) |
| 160 | public ResponseEntity<String> userDelete(@RequestParam int userId, @RequestParam String password) { |
| 161 | logger.warn("Delete user with id: {}", userId); |
| 162 | // 根据用户id查询该用户 |
| 163 | QueryWrapper<User> userQuery = new QueryWrapper<>(); |
| 164 | userQuery.eq("user_id", userId); |
| 165 | User userCheck = userService.getOne(userQuery); |
| 166 | |
| 167 | if (userCheck.getPassword().equals(password)) { |
| 168 | // 注销账户(自动清除与该用户相关的记录,注意会回收分配给该用户的邀请码) |
| 169 | userService.remove(userQuery); |
| 170 | |
| 171 | return ResponseEntity.noContent().build(); |
| 172 | } else { |
| 173 | logger.warn("Delete failed. Incorrect password for account: {}", password); |
| 174 | return ResponseEntity.status(408).body(""); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * 删除用户搜索历史 |
| 180 | * |
| 181 | * @param searchHistoryId 搜索历史id |
| 182 | * @return 删除用户搜索历史结果 |
| 183 | */ |
| 184 | @DeleteMapping("/search") |
| 185 | @ResponseStatus(HttpStatus.NO_CONTENT) |
| 186 | public ResponseEntity<String> searchDelete(@RequestParam int searchHistoryId) { |
| 187 | searchHistoryService.removeById(searchHistoryId); |
| 188 | return ResponseEntity.noContent().build(); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * 取消关注 |
| 193 | * |
| 194 | * @param userId 被关注者id |
| 195 | * @param followerId 关注被人者id |
| 196 | * @return 取消关注结果 |
| 197 | */ |
| 198 | @DeleteMapping("/subscription") |
| 199 | @ResponseStatus(HttpStatus.NO_CONTENT) |
| 200 | public ResponseEntity<String> subscriptionDelete(@RequestParam int userId, @RequestParam int followerId) { |
| 201 | QueryWrapper<Subscription> subscriptionQuery = new QueryWrapper<>(); |
| 202 | subscriptionQuery.eq("user_id", userId).eq("follower_id", followerId); |
| 203 | subscriptionService.remove(subscriptionQuery); |
| 204 | return ResponseEntity.noContent().build(); |
| 205 | } |
xiukira | d0a7a08 | 2025-06-05 16:28:08 +0800 | [diff] [blame] | 206 | } |