스터디를 통해 스프링부트와 AWS로 혼자 구현하는 웹 서비스(저자 이동욱) 서적을 공부하는 중입니다.
공부/실습한 내용을 정리한 포스팅입니다.
책에 모르는 부분이 있으면 구글링하거나 챗gpt에 물어봐서 보충하였습니다.
(아직 초보라 모르는 부분이 많아 이것저것 다 적었습니다.)
참고한 사이트 출처는 포스팅 맨 하단에 적었습니다.
templates
디렉토리에 posts-update.mustache
생성 후 아래와 같이 작성
{{>layout/header}}
<h1>게시글 수정</h1>
<div class="col-md-12">
<div class="col-md-4">
<form>
<div class="form-group">
<label for="id">글 번호</label>
<input type="text" class="form-control" id="id" value="{{post.id}}" readonly>
</div>
<div class="form-group">
<label for="title">제목</label>
<input type="text" class="form-control" id="title" value="{{post.title}}">
</div>
<div class="form-group">
<label for="author">작성자</label>
<input type="text" class="form-control" id="author" value="{{post.author}}" readonly>
</div>
<div class="form-group">
<label for="content">내용</label>
<textarea class="form-control" id="content">{{post.content}}</textarea>
</div>
</form>
<a href="/" role="button" class="btn btn-secondary">취소</a>
<button type="button" class="btn btn-primary" id="btn-update">수정 완료</button>
</div>
</div>
{{>layout/footer}}
{{post.id}}
: 객체의 필드 접근 시 .으로 구분(객체명.필드명)posts-update.mustache
에 아래의 코드 추가
<button type="button" class="btn btn-danger" id="btn-delete">삭제</button>
index.js
에 update function 추가var main = {
init : function () {
var _this = this;
$('#btn-save').on('click', function () {
_this.save();
});
$('#btn-update').on('click', function () {
_this.update();
});
},
.
.
.
,
update : function () {
var data = {
title:$('#title').val(),
content: $('#content').val()
};
var id = $('#id').val();
$.ajax({
type:'PUT',
url:'/api/v1/posts/'+id,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data : JSON.stringify(data)
}).done(function () {
alert('글이 수정되었습니다.');
window.location.href='/';
}).fail(function (error) {
alert(JSON.stringify(error))
});
}
}
index.mustache
코드 아래와 같이 수정 <tr>
<td>{{id}}</td>
<td><a href="/posts/update/{{id}}">{{title}}</a></td>
<td>{{author}}</td>
<td>{{modifiedDate}}</td>
</tr>
IndexController
클래스 메서드 추가 @GetMapping("/posts/update/{id}")
public String postsUpdate(@PathVariable Long id, Model model){
PostsResponseDto dto = postsService.findById(id);
model.addAttribute("post", dto);
return "posts-update";
}
index.js
에 아래의 코드 추가var main = {
init : function () {
.
.
.
$('#btn-delete').on('click', function () {
_this.delete();
})
},
.
.
.
,
delete : function () {
var id=$('#id').val();
$.ajax({
type:'DELETE',
url:'/api/v1/posts/'+id,
dataType:'json',
contentType:'application/json; charset=utf-8'
}).done(function () {
alert('글이 삭제되었습니다.');
window.location.href='/';
}).fail(function (error) {
alert(JSON.stringify(error))
})
}
}
PostsService
클래스 아래의 코드 추가 @Transactional
public void delete(Long id){
Posts posts = postsRepository.findById(id)
.orElseThrow(()->new IllegalArgumentException("해당 게시글이 없습니다. id="+id));
postsRepository.delete(posts);
}
PostsApiController
클래스 아래의 코드 추가 @DeleteMapping("/api/v1/posts/{id}")
public Long delete(@PathVariable Long id){
postsService.delete(id);
return id;
}