728x90
출처:
https://www.youtube.com/watch?v=UKyviwswrP8&list=PL93mKxaRDidECgjOBjPgI3Dyo8ka6Ilqm&index=59
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="../layout/header.jsp"%>
<div class="container">
<button class="btn btn-secondary" onclick="history.back()">돌아가기</button>
<button id="btn-update" class="btn btn-warning">수정</button>
<c:if test="${board.user.id == principal.user.id }">
<button id="btn-delete" class="btn btn-danger">삭제</button>
</c:if>
<br> <br>
<div>
글 번호 : <span id="id"><i>${board.id} </i></span> 작성자 : <span><i>${board.user.username} </i></span>
</div>
<br>
<div>
<h3>${board.title}</h3>
</div>
<hr />
<div>
<div>${board.content}</div>
</div>
<hr />
<button id="btn-save" class="btn btn-primary">글 저장</button>
</div>
<script src="/js/board.js"></script>
<%@ include file="../layout/footer.jsp"%>
package com.cosblog.controller.api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.cosblog.config.auth.PrincipalDetail;
import com.cosblog.dto.ResponseDto;
import com.cosblog.model.Board;
import com.cosblog.service.BoardService;
@RestController
public class BoardApiController {
@Autowired
private BoardService boardService;
@PostMapping("/api/board")
public ResponseDto<Integer> save(@RequestBody Board board,@AuthenticationPrincipal PrincipalDetail principal) {
boardService.글쓰기(board, principal.getUser());
return new ResponseDto<Integer>(HttpStatus.OK.value(), 1);
}
@DeleteMapping("/api/board/{id}")
public ResponseDto<Integer> deleteById(@PathVariable int id){
boardService.글삭제하기(id);
return new ResponseDto<Integer>(HttpStatus.OK.value(), 1);
}
}
package com.cosblog.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.cosblog.model.Board;
import com.cosblog.model.User;
import com.cosblog.repository.BoardRepository;
//스프링이 컴포넌트 스캔을 통해서 bean에 등록을 해줌
@Service
public class BoardService {
@Autowired
private BoardRepository boardRepository;
@Transactional
public void 글쓰기(Board board, User user) {
board.setCount(0);
board.setUser(user);
boardRepository.save(board);
}
@Transactional(readOnly=true)
public Page<Board> 글목록(Pageable pageable) {
return boardRepository.findAll(pageable);
}
@Transactional(readOnly=true)
public Board 글상세보기(int id) {
return boardRepository.findById(id).orElseThrow(() -> {
return new IllegalArgumentException("글 상세보기 실패:아이디를 찾을 수 없습니다.");
});
}
@Transactional
public void 글삭제하기(int id) {
boardRepository.deleteById(id);
}
}
/*
* 이 로그인도 사용 안 할거라 삭제
*
* @Transactional(readOnly = true)//Select할 때 트랜잭션 시작, 서비스 종료시에 트랜잭션 종료(정합성)
* public User 로그인(User user) { return
* userRepository.findByUsernameAndPassword(user.getUsername(),
* user.getPassword()); }
*/
let index = {
init: function () {
$("#btn-save").on("click", () => {
this.save();
});
$("#btn-delete").on("click", () => {
this.deleteById();
});
},
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));
});
},
deleteById: function () {
var id = $("#id").text();
$.ajax({
type: "DELETE",
url: "/api/board/" + id,
dataType: "json",
})
.done(function (resp) {
alert("삭제가 완료되었습니다.");
location.href = "/";
})
.fail(function (error) {
alert(JSON.stringify(error));
});
},
};
index.init();
728x90
'Spring > Spring boot' 카테고리의 다른 글
스프링부트 강좌 59강(블로그 프로젝트) - 스프링작동원리 복습 (0) | 2022.01.25 |
---|---|
스프링부트 강좌 58강(블로그 프로젝트) - 글 수정하기 (0) | 2022.01.25 |
스프링부트 강좌 56강(블로그 프로젝트) - 글 상세보기 (0) | 2022.01.25 |
스프링부트 강좌 55강(블로그 프로젝트) - 글목록 페이징하기 (0) | 2022.01.25 |
스프링부트 강좌 54강(블로그 프로젝트) - 글목록보기 (0) | 2022.01.25 |
댓글