feat-获取用户功能demo

This commit is contained in:
DC_DC 2024-04-21 22:18:00 +08:00
parent 9a9d2e7018
commit 66d02822f2
5 changed files with 39 additions and 3 deletions

View File

@ -24,7 +24,7 @@ public class BasicController {
/**
* 网站主页
*/
// @GetMapping("/**")
@GetMapping("/index")
public ResponseEntity<BaseResponse> index() {
log.info("访问主页");
BaseResponse response = new BaseResponse("欢迎", 200, "Success", "这是故事管理系统主页");
@ -40,4 +40,15 @@ public class BasicController {
return userService.login(username, password);
}
@GetMapping("/user/{userId}")
public ResponseEntity<BaseResponse> getUserCurrent(@PathVariable String userId) {
log.info("获取用户 {}", userId);
if (!userId.matches("^[0-9]+$")) {
return ResponseEntity.status(403).body(
new BaseResponse("PathValueError", 40301, "参数错误", null)
);
}
return userService.getUserInfo(userId);
}
}

View File

@ -21,4 +21,10 @@ public class UserDAO {
log.info("\t> Mysql 读取");
return userMapper.getPasswordByUsername(userName);
}
public UserDemoDO getUserById(String userId) {
log.info("[DAO] 执行 getUserById 方法");
log.info("\t> Mysql 读取");
return userMapper.getUserById(Long.parseLong(userId));
}
}

View File

@ -12,4 +12,7 @@ import org.apache.ibatis.annotations.Select;
public interface UserMapper {
@Select("SELECT * FROM stories_system.user_demo WHERE username = #{username}")
UserDemoDO getPasswordByUsername(String username);
@Select("SELECT * FROM stories_system.user_demo WHERE id = #{userId}")
UserDemoDO getUserById(long userId);
}

View File

@ -9,4 +9,6 @@ import org.springframework.http.ResponseEntity;
*/
public interface UserService {
ResponseEntity<BaseResponse> login(String username, String password);
ResponseEntity<BaseResponse> getUserInfo(String userId);
}

View File

@ -22,10 +22,10 @@ public class UserServiceImpl implements UserService {
@Override
public ResponseEntity<BaseResponse> login(String username, String password) {
UserDemoDO userDemoDO = userDAO.getPasswordByUserName(username);
log.info(String.valueOf(userDemoDO));
if (userDemoDO != null){
log.info(userDemoDO.toString());
if (userDemoDO.getPassword().equals(password)){
BaseResponse response = new BaseResponse("登录成功", 200, "Success", "用户已登录");
BaseResponse response = new BaseResponse("登录成功", 200, "Success", userDemoDO);
return ResponseEntity.ok(response);
} else {
BaseResponse response = new BaseResponse("登录失败", 404, "Error", "密码错误");
@ -36,4 +36,18 @@ public class UserServiceImpl implements UserService {
return ResponseEntity.status(404).body(response);
}
}
@Override
public ResponseEntity<BaseResponse> getUserInfo(String userId) {
UserDemoDO getUser = userDAO.getUserById(userId);
if (getUser != null) {
return ResponseEntity.ok().body(
new BaseResponse("Success", 200, "获取成功", getUser)
);
} else {
return ResponseEntity.status(404).body(
new BaseResponse("UserNotExist", 40401, "用户不存在", null)
);
}
}
}