diff --git a/src/main/java/org/cmh/backend/authentication/controller/AuthenticationController.java b/src/main/java/org/cmh/backend/authentication/controller/AuthenticationController.java index 73d0590..5ec7e11 100644 --- a/src/main/java/org/cmh/backend/authentication/controller/AuthenticationController.java +++ b/src/main/java/org/cmh/backend/authentication/controller/AuthenticationController.java @@ -5,6 +5,7 @@ import lombok.AllArgsConstructor; import lombok.Getter; import lombok.Setter; import org.cmh.backend.Utils.JwtUtil; +import org.cmh.backend.authentication.dto.ManageUserProfileRequest; import org.cmh.backend.authentication.model.UserHS; import org.cmh.backend.authentication.service.UserService; import org.springframework.beans.factory.annotation.Autowired; @@ -73,6 +74,18 @@ class AuthenticationController { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } + @PostMapping("/manageUserProfile") + public ResponseEntity manageUserProfile(@RequestBody ManageUserProfileRequest userProfileRequest) { + String username = JwtUtil.extractUsername(userProfileRequest.getToken()); + if (username != null) { + boolean succeeded = userService.updateUserProfile(username, userProfileRequest); + if (succeeded) { + return new ResponseEntity<>(HttpStatus.OK); + } + } + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + } diff --git a/src/main/java/org/cmh/backend/authentication/dto/ManageUserProfileRequest.java b/src/main/java/org/cmh/backend/authentication/dto/ManageUserProfileRequest.java new file mode 100644 index 0000000..6c37924 --- /dev/null +++ b/src/main/java/org/cmh/backend/authentication/dto/ManageUserProfileRequest.java @@ -0,0 +1,16 @@ +package org.cmh.backend.authentication.dto; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class ManageUserProfileRequest { + private String token; + private String nickname; + private String gender; + private String phoneNumber; + private String email; + private String department; + private String role; +} diff --git a/src/main/java/org/cmh/backend/authentication/service/UserService.java b/src/main/java/org/cmh/backend/authentication/service/UserService.java index ac1620a..a901360 100644 --- a/src/main/java/org/cmh/backend/authentication/service/UserService.java +++ b/src/main/java/org/cmh/backend/authentication/service/UserService.java @@ -1,5 +1,6 @@ package org.cmh.backend.authentication.service; +import org.cmh.backend.authentication.dto.ManageUserProfileRequest; import org.cmh.backend.authentication.model.UserHS; import org.cmh.backend.authentication.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; @@ -57,6 +58,27 @@ public class UserService { return user != null && passwordEncoder.matches(password, user.getPassword()); } + public boolean updateUserProfile(String username, ManageUserProfileRequest request) { + UserHS user = userRepository.findByUsername(username); + if (user != null) { + user.setNickname(request.getNickname()); + user.setGender(request.getGender()); + user.setPhoneNumber(request.getPhoneNumber()); + user.setEmail(request.getEmail()); + user.setDepartment(request.getDepartment()); + user.setRole(request.getRole()); + try{ + userRepository.save(user); + } + catch (Exception e) { + return false; + } + return true; + } else { + return false; + } +} + // 验证用户名格式 private boolean isValidUsername(String username) {