jpablog : update user

This commit is contained in:
kim
2021-02-01 18:05:24 +09:00
parent 3f66ed663d
commit e46b30e17e
5 changed files with 74 additions and 4 deletions

View File

@@ -20,4 +20,9 @@ public class UserController {
public String loginForm() { public String loginForm() {
return "user/loginForm"; return "user/loginForm";
} }
@GetMapping("/user/updateForm")
public String updateForm() {
return "user/updateForm";
}
} }

View File

@@ -7,12 +7,10 @@ import com.example.jpablog.service.UserService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import java.security.Principal;
@RestController @RestController
@RequiredArgsConstructor @RequiredArgsConstructor
@@ -26,6 +24,15 @@ public class UserApiController {
return new ResponseDto<>(result, HttpStatus.OK.value()); return new ResponseDto<>(result, HttpStatus.OK.value());
} }
@PutMapping("/user/{id}")
public ResponseDto<Integer> update(@PathVariable Long id, @RequestBody User user, Principal principal) {
if (principal.getName().equals(user.getUsername())) {
userService.회원수정(id, user);
return new ResponseDto<>(1, HttpStatus.OK.value());
}
return new ResponseDto<>(-1, HttpStatus.BAD_REQUEST.value());
}
/*// 기본 로그인 /*// 기본 로그인
@PostMapping("/user/login") @PostMapping("/user/login")
public ResponseDto<Integer> login(@RequestBody User user, HttpSession session) { public ResponseDto<Integer> login(@RequestBody User user, HttpSession session) {

View File

@@ -27,6 +27,15 @@ public class UserService {
return 1; return 1;
} }
@Transactional
public void 회원수정(Long id, User user) {
User persistence = userRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("회원 찾기 실패"));
String encPassword = encoder.encode(user.getPassword());
persistence.setPassword(encPassword);
persistence.setEmail(user.getEmail());
}
/*// 기본 로그인 /*// 기본 로그인
public User 로그인(User user) { public User 로그인(User user) {
return userRepository.findByUsernameAndPassword(user.getUsername(), user.getPassword()); return userRepository.findByUsernameAndPassword(user.getUsername(), user.getPassword());

View File

@@ -3,6 +3,9 @@ let index = {
$("#btn-save").on("click", () => { $("#btn-save").on("click", () => {
this.save(); this.save();
}); });
$("#btn-update").on("click", () => {
this.update();
});
}, },
save : function () { save : function () {
let data = { let data = {
@@ -24,6 +27,27 @@ let index = {
alert(JSON.stringify(error)); alert(JSON.stringify(error));
}); });
}, },
update : function () {
const id = $("#id").val();
let data = {
username: $("#username").val(),
password: $("#password").val(),
email: $("#email").val(),
};
$.ajax({
type: "put",
url: "/user/" + id,
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json"
}).done(function (resp){
console.log(resp);
alert("회원수정이 완료되었습니다.");
location.href = "/";
}).fail(function (error){
alert(JSON.stringify(error));
});
},
} }
index.init(); index.init();

View File

@@ -0,0 +1,25 @@
<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@include file="../layout/header.jsp"%>
<div class="container">
<form>
<input type="hidden" id="id" value="${principal.user.id}">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" value="${principal.user.username}" id="username" readonly>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" placeholder="Enter password" id="password">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" value="${principal.user.email}" placeholder="Enter email" id="email">
</div>
<button id="btn-update" type="button" class="btn btn-primary">수정</button>
</form>
</div>
<script src="/js/user.js"></script>
<%@include file="../layout/footer.jsp"%>