添加了用户信息修改功能,测试通过

This commit is contained in:
高子兴 2024-07-01 19:20:41 +08:00
parent c863f1023b
commit 58e456f3b3
3 changed files with 51 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.cmh.backend.Utils.JwtUtil; 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.model.UserHS;
import org.cmh.backend.authentication.service.UserService; import org.cmh.backend.authentication.service.UserService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -73,6 +74,18 @@ class AuthenticationController {
return new ResponseEntity<>(HttpStatus.NOT_FOUND); return new ResponseEntity<>(HttpStatus.NOT_FOUND);
} }
@PostMapping("/manageUserProfile")
public ResponseEntity<Object> 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);
}
} }

View File

@ -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;
}

View File

@ -1,5 +1,6 @@
package org.cmh.backend.authentication.service; 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.model.UserHS;
import org.cmh.backend.authentication.repository.UserRepository; import org.cmh.backend.authentication.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -57,6 +58,27 @@ public class UserService {
return user != null && passwordEncoder.matches(password, user.getPassword()); 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) { private boolean isValidUsername(String username) {