Skip to content

Commit

Permalink
[블로그개발_11] 글 수정, 삭제... (+Date 부분 변경)
Browse files Browse the repository at this point in the history
  • Loading branch information
origoni committed Jun 13, 2015
1 parent b4fe15b commit 6ff2820
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
8 changes: 6 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.millky.blog</groupId>
<artifactId>spring-blog</artifactId>
<version>0.0.9-SNAPSHOT</version>
<version>0.0.11-SNAPSHOT</version>
<packaging>war</packaging>

<name>SpringBlog</name>
Expand Down Expand Up @@ -33,9 +33,13 @@
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<!-- <scope>runtime</scope> -->
</dependency>

<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-facebook</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/millky/blog/domain/model/entity/Post.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.millky.blog.domain.model.entity;

import java.util.Date;
import java.time.LocalDateTime;

import javax.persistence.Column;
import javax.persistence.Entity;
Expand All @@ -12,10 +12,14 @@
import lombok.Getter;
import lombok.Setter;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;

@Getter
@Setter
@Entity
public class Post {

@Id
@GeneratedValue
int id;
Expand All @@ -33,5 +37,6 @@ public class Post {
@Column(length = 100000000)
String content;

Date regDate;
@DateTimeFormat(iso = ISO.DATE_TIME)
LocalDateTime regDate;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.millky.blog.presentation.controller;

import java.util.Date;
import java.time.LocalDateTime;
import java.util.List;

import javax.validation.Valid;
Expand Down Expand Up @@ -33,7 +33,7 @@ public String write(@Valid Post post, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "form";
}
post.setRegDate(new Date());
post.setRegDate(LocalDateTime.now());
return "redirect:/post/" + postDao.save(post).getId();
}

Expand All @@ -50,4 +50,25 @@ public String view(Model model, @PathVariable int id) {
model.addAttribute("post", post);
return "post";
}
}

@RequestMapping("/{id}/delete")
public String delete(@PathVariable int id) {
postDao.delete(id);
return "redirect:/post/list";
}

@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
public String editor(Model model, @PathVariable int id) {
Post post = postDao.findOne(id);
model.addAttribute("post", post);
return "form";
}

@RequestMapping(value = "/{id}/edit", method = RequestMethod.POST)
public String edit(@Valid Post post, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "form";
}
return "redirect:/post/" + postDao.save(post).getId();
}
}
9 changes: 8 additions & 1 deletion src/main/webapp/WEB-INF/jsp/form.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@
<i class="pen-icon icon-createlink" data-action="createlink"></i>
</div>

<form:form action="/post/write" commandName="post" onsubmit="if($('#pen').html()!='<p><br></p>')$('#content').val($('#pen').html()); pen.destroy();" method="post">
<c:if test="${post.id == 0}"><c:url var="actionUrl" value="/post/write"/></c:if>
<c:if test="${post.id != 0}"><c:url var="actionUrl" value="/post/${post.id}/edit"/></c:if>

<form:form action="${actionUrl}" commandName="post" onsubmit="if($('#pen').html()!='<p><br></p>')$('#content').val($('#pen').html()); pen.destroy();" method="post">

<c:if test="${post.id != 0}"><form:input type="hidden" path="regDate" /></c:if>

<form:errors path="*" cssClass="errorblock" element="div" />

Expand Down Expand Up @@ -113,6 +118,8 @@
toolbar : document.getElementById('custom-toolbar'),
editor : document.querySelector('[data-toggle="pen"]')
};
$('#pen').html($('#content').val());
// create editor
var pen = window.pen = new Pen(options);
Expand Down
9 changes: 9 additions & 0 deletions src/main/webapp/WEB-INF/jsp/post.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@
${post.content}
</div>
</div>

<div class="pull-right">
<a href="/post/${post.id}/edit">
<button type="button" class="btn btn-warning">Edit</button>
</a>
<a href="/post/${post.id}/delete" onclick="if(!confirm('진심이에요?')){return false;}">
<button type="button" class="btn btn-danger">Delete</button>
</a>
</div>
</div>
</article>

Expand Down

0 comments on commit 6ff2820

Please sign in to comment.