jpablog : write form(summernote), api
This commit is contained in:
@@ -1,12 +1,14 @@
|
|||||||
package com.example.jpablog.config.auth;
|
package com.example.jpablog.config.auth;
|
||||||
|
|
||||||
import com.example.jpablog.model.User;
|
import com.example.jpablog.model.User;
|
||||||
|
import lombok.Getter;
|
||||||
import org.springframework.security.core.GrantedAuthority;
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
|
||||||
import java.util.ArrayDeque;
|
import java.util.ArrayDeque;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
|
@Getter
|
||||||
public class PrincipalDetail implements UserDetails {
|
public class PrincipalDetail implements UserDetails {
|
||||||
private final User user;
|
private final User user;
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package com.example.jpablog.controller;
|
package com.example.jpablog.controller;
|
||||||
|
|
||||||
import com.example.jpablog.config.auth.PrincipalDetail;
|
|
||||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
|
||||||
@@ -13,4 +11,9 @@ public class BoardController {
|
|||||||
public String index() {
|
public String index() {
|
||||||
return "index";
|
return "index";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/board/saveForm")
|
||||||
|
public String save() {
|
||||||
|
return "board/saveForm";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package com.example.jpablog.controller.api;
|
||||||
|
|
||||||
|
import com.example.jpablog.config.auth.PrincipalDetail;
|
||||||
|
import com.example.jpablog.dto.ResponseDto;
|
||||||
|
import com.example.jpablog.model.Board;
|
||||||
|
import com.example.jpablog.model.User;
|
||||||
|
import com.example.jpablog.service.BoardService;
|
||||||
|
import com.example.jpablog.service.UserService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.security.Principal;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class BoardApiController {
|
||||||
|
|
||||||
|
private final BoardService boardService;
|
||||||
|
|
||||||
|
@PostMapping("/api/board")
|
||||||
|
public ResponseDto<Integer> save(@RequestBody Board board, @AuthenticationPrincipal PrincipalDetail principal, Principal p) {
|
||||||
|
System.out.println(p.getName());
|
||||||
|
boardService.글쓰기(board, principal.getUser());
|
||||||
|
return new ResponseDto<>(1, HttpStatus.OK.value());
|
||||||
|
}
|
||||||
|
|
||||||
|
/*// 기본 로그인
|
||||||
|
@PostMapping("/user/login")
|
||||||
|
public ResponseDto<Integer> login(@RequestBody User user, HttpSession session) {
|
||||||
|
User principal = userService.로그인(user);
|
||||||
|
if (principal != null) {
|
||||||
|
session.setAttribute("principal", principal);
|
||||||
|
}
|
||||||
|
return new ResponseDto<>(1, HttpStatus.OK.value());
|
||||||
|
}*/
|
||||||
|
}
|
||||||
@@ -28,7 +28,6 @@ public class Board {
|
|||||||
@Lob // 대용량 데이터
|
@Lob // 대용량 데이터
|
||||||
private String content;
|
private String content;
|
||||||
|
|
||||||
@ColumnDefault("0")
|
|
||||||
private int count;
|
private int count;
|
||||||
|
|
||||||
@ManyToOne(fetch = FetchType.LAZY)
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.example.jpablog.repository;
|
||||||
|
|
||||||
|
import com.example.jpablog.model.Board;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface BoardRepository extends JpaRepository<Board, Long> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.example.jpablog.service;
|
||||||
|
|
||||||
|
import com.example.jpablog.config.auth.PrincipalDetail;
|
||||||
|
import com.example.jpablog.model.Board;
|
||||||
|
import com.example.jpablog.model.User;
|
||||||
|
import com.example.jpablog.repository.BoardRepository;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.security.Principal;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public class BoardService {
|
||||||
|
|
||||||
|
private final BoardRepository boardRepository;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void 글쓰기(Board board, User user) {
|
||||||
|
board.setUser(user);
|
||||||
|
board.setCount(0);
|
||||||
|
boardRepository.save(board);
|
||||||
|
}
|
||||||
|
}
|
||||||
27
jpablog/src/main/resources/static/js/board.js
Normal file
27
jpablog/src/main/resources/static/js/board.js
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
let index = {
|
||||||
|
init : function () {
|
||||||
|
$("#btn-save").on("click", () => {
|
||||||
|
this.save();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
save : function () {
|
||||||
|
let data = {
|
||||||
|
title: $("#title").val(),
|
||||||
|
content: $("#content").val(),
|
||||||
|
};
|
||||||
|
$.ajax({
|
||||||
|
type: "post",
|
||||||
|
url: "/api/board",
|
||||||
|
data: JSON.stringify(data),
|
||||||
|
contentType: "application/json; charset=utf-8",
|
||||||
|
dataType: "json"
|
||||||
|
}).done(function (resp){
|
||||||
|
alert("글쓰기 완료!");
|
||||||
|
location.href = "/";
|
||||||
|
}).fail(function (error){
|
||||||
|
alert(JSON.stringify(error));
|
||||||
|
});
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
index.init();
|
||||||
@@ -7,7 +7,7 @@ let index = {
|
|||||||
save : function () {
|
save : function () {
|
||||||
let data = {
|
let data = {
|
||||||
username: $("#username").val(),
|
username: $("#username").val(),
|
||||||
password: $("#pwd").val(),
|
password: $("#password").val(),
|
||||||
email: $("#email").val(),
|
email: $("#email").val(),
|
||||||
};
|
};
|
||||||
$.ajax({
|
$.ajax({
|
||||||
|
|||||||
26
jpablog/src/main/webapp/WEB-INF/views/board/saveForm.jsp
Normal file
26
jpablog/src/main/webapp/WEB-INF/views/board/saveForm.jsp
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
|
||||||
|
|
||||||
|
<%@include file="../layout/header.jsp"%>
|
||||||
|
|
||||||
|
<div class="container">
|
||||||
|
<form>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="title">Title:</label>
|
||||||
|
<input type="text" class="form-control" placeholder="Enter title" id="title">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="content">Content:</label>
|
||||||
|
<textarea class="form-control summernote" rows="5" id="content"></textarea>
|
||||||
|
</div>
|
||||||
|
<button id="btn-save" type="button" class="btn btn-primary">글쓰기</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<script>
|
||||||
|
$('.summernote').summernote({
|
||||||
|
placeholder: '글쓰기 ㅎㅎ',
|
||||||
|
tabsize: 2,
|
||||||
|
height: 300
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
<script src="/js/board.js"></script>
|
||||||
|
<%@include file="../layout/footer.jsp"%>
|
||||||
@@ -14,6 +14,9 @@
|
|||||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
|
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
|
||||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
|
||||||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
|
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
|
||||||
|
|
||||||
|
<link href="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote-bs4.min.css" rel="stylesheet">
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote-bs4.min.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<nav class="navbar navbar-expand-md bg-dark navbar-dark">
|
<nav class="navbar navbar-expand-md bg-dark navbar-dark">
|
||||||
@@ -31,8 +34,8 @@
|
|||||||
</c:when>
|
</c:when>
|
||||||
<c:otherwise>
|
<c:otherwise>
|
||||||
<ul class="navbar-nav">
|
<ul class="navbar-nav">
|
||||||
<li class="nav-item"><a class="nav-link" href="/board/form">글쓰기</a></li>
|
<li class="nav-item"><a class="nav-link" href="/board/saveForm">글쓰기</a></li>
|
||||||
<li class="nav-item"><a class="nav-link" href="/user/form">회원정보</a></li>
|
<li class="nav-item"><a class="nav-link" href="/user/updateForm">회원정보</a></li>
|
||||||
<li class="nav-item"><a class="nav-link" href="/logout">로그아웃</a></li>
|
<li class="nav-item"><a class="nav-link" href="/logout">로그아웃</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</c:otherwise>
|
</c:otherwise>
|
||||||
|
|||||||
@@ -9,8 +9,8 @@
|
|||||||
<input type="text" class="form-control" placeholder="Enter username" id="username">
|
<input type="text" class="form-control" placeholder="Enter username" id="username">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="pwd">Password:</label>
|
<label for="password">Password:</label>
|
||||||
<input type="password" class="form-control" placeholder="Enter password" id="pwd">
|
<input type="password" class="form-control" placeholder="Enter password" id="password">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="email">Email:</label>
|
<label for="email">Email:</label>
|
||||||
|
|||||||
Reference in New Issue
Block a user