diff --git a/src/main/java/com/bupt/note/Controller/UserController.java b/src/main/java/com/bupt/note/Controller/UserController.java index 33e9350..7c3c2d5 100644 --- a/src/main/java/com/bupt/note/Controller/UserController.java +++ b/src/main/java/com/bupt/note/Controller/UserController.java @@ -5,12 +5,13 @@ import com.bupt.note.Repository.UserRepository; import com.bupt.note.ResponseData.ResponseData; import com.bupt.note.ResponseData.ResponseDataUtil; import com.bupt.note.ResponseData.ResultEnums; +import com.bupt.note.dto.UpdatePwd; import com.bupt.note.dto.UserForm; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; /** * 用户接口 @@ -22,6 +23,8 @@ public class UserController { @Autowired UserRepository userRepository; + private Logger logger= LoggerFactory.getLogger(UserController.class); + /** * 注册用户 * @param user 用户信息 @@ -29,8 +32,7 @@ public class UserController { */ @PostMapping("/sign_up") public ResponseData signUp(@RequestBody UserForm user) { - User exist = userRepository.findByUserName(user.getUserName()); - if (exist == null) { + if (!userRepository.existsByUserName(user.getUserName())) { userRepository.save(user.getUser()); User u = userRepository.findByUserName(user.getUserName()); return ResponseDataUtil.buildSuccess(u); @@ -45,11 +47,43 @@ public class UserController { */ @PostMapping("/sign_in") public ResponseData signIn(@RequestBody UserForm user){ - User exist = userRepository.findByUserName(user.getUserName()); - if(exist==null||!exist.getPassword().equals(user.getPassword())){ - return ResponseDataUtil.buildError(ResultEnums.ERROR); + if(StringUtils.isNoneEmpty(user.getUserName(),user.getPassword())&& + userRepository.existsByUserName(user.getUserName())){ + User exists= userRepository.findByUserName(user.getUserName()); + if(user.getPassword().equals(exists.getPassword())){ + return ResponseDataUtil.buildError(ResultEnums.SUCCESS); + }else{ + logger.error("账号或者密码错误"); + return ResponseDataUtil.buildError(ResultEnums.ERROR); + } }else { - return ResponseDataUtil.buildSuccess(ResultEnums.SUCCESS); + logger.error("登陆校验失败"); + return ResponseDataUtil.buildSuccess(ResultEnums.ERROR); + } + } + + /** + * 修改密码 + * @param user + * @return + */ + @PutMapping("update") + public ResponseData put(@RequestBody UpdatePwd user,@CookieValue("user") String username){ + if(StringUtils.isNoneEmpty(username,user.getOldpwd(),user.getNewpwd())&& + userRepository.existsByUserName(username)){ + User exists= userRepository.findByUserName(username); + if(user.getOldpwd().equals(exists.getPassword())){ + exists.setPassword(user.getNewpwd()); + userRepository.save(exists); + return ResponseDataUtil.buildSuccess(); + }else{ + logger.error("原密码错误"); + return ResponseDataUtil.buildError("原密码错误"); + } + }else{ + logger.error("修改密码表单校验失败"); + return ResponseDataUtil.buildError(); } + } } diff --git a/src/main/java/com/bupt/note/Repository/UserRepository.java b/src/main/java/com/bupt/note/Repository/UserRepository.java index bbe21cc..5960f40 100644 --- a/src/main/java/com/bupt/note/Repository/UserRepository.java +++ b/src/main/java/com/bupt/note/Repository/UserRepository.java @@ -4,6 +4,6 @@ import com.bupt.note.Model.User; import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository { + boolean existsByUserName(String username); User findByUserName(String userName); - void deleteByUserName(String userName); } diff --git a/src/main/java/com/bupt/note/dto/UpdatePwd.java b/src/main/java/com/bupt/note/dto/UpdatePwd.java new file mode 100644 index 0000000..943aaf8 --- /dev/null +++ b/src/main/java/com/bupt/note/dto/UpdatePwd.java @@ -0,0 +1,22 @@ +package com.bupt.note.dto; + +public class UpdatePwd { + private String oldpwd; + private String newpwd; + + public String getOldpwd() { + return oldpwd; + } + + public void setOldpwd(String oldpwd) { + this.oldpwd = oldpwd; + } + + public String getNewpwd() { + return newpwd; + } + + public void setNewpwd(String newpwd) { + this.newpwd = newpwd; + } +}