jpablog : join user
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package com.example.jpablog.controller.api;
|
||||
|
||||
import com.example.jpablog.dto.ResponseDto;
|
||||
import com.example.jpablog.model.RoleType;
|
||||
import com.example.jpablog.model.User;
|
||||
import com.example.jpablog.service.UserService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
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;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api")
|
||||
public class UserApiController {
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
@PostMapping("/user")
|
||||
public ResponseDto<Integer> save(@RequestBody User user) {
|
||||
user.setRole(RoleType.USER);
|
||||
int result = userService.회원가입(user);
|
||||
return new ResponseDto<>(result, HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.example.jpablog.dto;
|
||||
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class ResponseDto<T> {
|
||||
|
||||
T data;
|
||||
HttpStatus status;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.example.jpablog.service;
|
||||
|
||||
import com.example.jpablog.model.User;
|
||||
import com.example.jpablog.repository.UserRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Transactional(readOnly = true)
|
||||
public class UserService {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
@Transactional
|
||||
public int 회원가입(User user) {
|
||||
userRepository.save(user);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
29
jpablog/src/main/resources/static/js/user.js
Normal file
29
jpablog/src/main/resources/static/js/user.js
Normal file
@@ -0,0 +1,29 @@
|
||||
let index = {
|
||||
init : function () {
|
||||
$("#btn-save").on("click", () => {
|
||||
this.save();
|
||||
})
|
||||
},
|
||||
save : function () {
|
||||
let data = {
|
||||
username: $("#username").val(),
|
||||
password: $("#pwd").val(),
|
||||
email: $("#email").val(),
|
||||
};
|
||||
$.ajax({
|
||||
type: "post",
|
||||
url: "/jpablog/api/user",
|
||||
data: JSON.stringify(data),
|
||||
contentType: "application/json; charset=utf-8",
|
||||
dataType: "json"
|
||||
}).done(function (resp){
|
||||
console.log(resp);
|
||||
alert("회원가입이 완료되었습니다.");
|
||||
location.href = "/jpablog";
|
||||
}).fail(function (error){
|
||||
alert(JSON.stringify(error));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
index.init();
|
||||
@@ -3,7 +3,7 @@
|
||||
<%@include file="../layout/header.jsp"%>
|
||||
|
||||
<div class="container">
|
||||
<form action="/action_page.php">
|
||||
<form>
|
||||
<div class="form-group">
|
||||
<label for="username">Username:</label>
|
||||
<input type="text" class="form-control" placeholder="Enter username" id="username">
|
||||
@@ -16,8 +16,9 @@
|
||||
<label for="email">Email:</label>
|
||||
<input type="email" class="form-control" placeholder="Enter email" id="email">
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">회원가입</button>
|
||||
<button id="btn-save" type="button" class="btn btn-primary">회원가입</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<script src="${pageContext.request.contextPath}/js/user.js"></script>
|
||||
<%@include file="../layout/footer.jsp"%>
|
||||
Reference in New Issue
Block a user