Skip to content

Commit 7a0b3bb

Browse files
committed
comment modified datetime
1 parent 7399619 commit 7a0b3bb

19 files changed

Lines changed: 46 additions & 17 deletions

src/main/java/alexp/blog/model/Comment.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public class Comment {
2727
@Convert(converter = LocalDateTimePersistenceConverter.class)
2828
private LocalDateTime dateTime;
2929

30+
@Column(nullable = true)
31+
@Convert(converter = LocalDateTimePersistenceConverter.class)
32+
private LocalDateTime modifiedDateTime;
33+
3034
@ManyToOne(fetch = FetchType.EAGER)
3135
@JoinColumn(name = "user_id", nullable = false)
3236
private User user;
@@ -107,6 +111,14 @@ public void setDateTime(LocalDateTime dateTime) {
107111
this.dateTime = dateTime;
108112
}
109113

114+
public LocalDateTime getModifiedDateTime() {
115+
return modifiedDateTime;
116+
}
117+
118+
public void setModifiedDateTime(LocalDateTime modifiedDateTime) {
119+
this.modifiedDateTime = modifiedDateTime;
120+
}
121+
110122
public void setCommentText(String commentText) {
111123
this.commentText = commentText;
112124
}

src/main/java/alexp/blog/service/CommentServiceImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ public void updateComment(Comment newCommentData, Long commentId) throws ActionE
8383

8484
comment.setCommentText(newCommentData.getCommentText());
8585

86+
comment.setModifiedDateTime(LocalDateTime.now());
87+
8688
commentRepository.saveAndFlush(comment);
8789
}
8890
}

src/main/resources/dummy-data.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1695,7 +1695,7 @@ INSERT INTO posts_tags(post_id, tag_id) VALUES (85, 5);
16951695

16961696

16971697
INSERT INTO comments(id, commentText, dateTime, post_id, user_id, deleted) VALUES(1, 'Great post! Keep it up!', '2015-04-03 14:30:58', 85, 2, 0);
1698-
INSERT INTO comments(id, commentText, dateTime, post_id, user_id, deleted) VALUES(2, 'Yeah, I learnt **so much** here.', '2015-04-03 16:35:58', 85, 3, 0);
1698+
INSERT INTO comments(id, commentText, dateTime, post_id, user_id, deleted, modifiedDateTime) VALUES(2, 'Yeah, I learnt **so much** here.', '2015-04-03 16:35:58', 85, 3, 0, '2015-04-03 19:30:58');
16991699
INSERT INTO comments(id, commentText, dateTime, post_id, user_id, deleted, parent_id) VALUES(3, 'Thank you.:)', '2015-04-03 19:00:58', 85, 1, 0, 2);
17001700
INSERT INTO comments(id, commentText, dateTime, post_id, user_id, deleted, parent_id) VALUES(4, 'OK!', '2015-04-03 19:10:58', 85, 1, 0, 1);
17011701

src/main/webapp/WEB-INF/templates/fragments/comments.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
</th:block>
2323
<span th:if="${comment.deleted}" class="deleted">[deleted]</span>
2424

25-
<span class="post-date" th:text="${#temporals.format(comment.dateTime, 'MMM dd, yyyy HH:mm')}"></span>
25+
<span class="post-date">
26+
<span th:text="${#temporals.format(comment.dateTime, 'MMM dd, yyyy HH:mm')}"></span>
27+
<i th:unless="${comment.modifiedDateTime == null}" th:text="' (modified ' + ${#temporals.format(comment.modifiedDateTime, 'MMM dd, yyyy HH:mm')} + ')'"></i>
28+
</span>
2629
</div>
2730

2831
<div>

src/test/java/alexp/blog/controller/CommentControllerIT.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package alexp.blog.controller;
22

33
import alexp.blog.AbstractIntegrationTest;
4+
import alexp.blog.service.CommentService;
45
import alexp.blog.utils.HsqldbSequenceResetter;
56
import com.github.springtestdbunit.annotation.*;
67
import com.github.springtestdbunit.assertion.DatabaseAssertionMode;
78
import org.junit.Test;
9+
import org.springframework.beans.factory.annotation.Autowired;
810
import org.springframework.http.MediaType;
911

12+
import java.time.LocalDate;
13+
1014
import static alexp.blog.utils.SecurityUtils.*;
15+
import static org.hamcrest.MatcherAssert.assertThat;
1116
import static org.hamcrest.Matchers.*;
1217
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
1318
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@@ -18,6 +23,9 @@
1823
@DbUnitConfiguration(databaseOperationLookup = HsqldbSequenceResetter.class)
1924
public class CommentControllerIT extends AbstractIntegrationTest {
2025

26+
@Autowired
27+
private CommentService commentService;
28+
2129
@Test
2230
@ExpectedDatabase("data.xml")
2331
public void shouldShowComments() throws Exception {
@@ -212,10 +220,12 @@ public void shouldEditComment() throws Exception {
212220
.param("commentText", text))
213221
.andExpect(status().isOk())
214222
.andExpect(content().string("ok"));
223+
224+
assertThat(commentService.getComment(4L).getModifiedDateTime().toLocalDate().equals(LocalDate.now()), equalTo(true));
215225
}
216226

217227
@Test
218-
@ExpectedDatabase("data-comment-edited.xml")
228+
@ExpectedDatabase(value = "data-comment-edited.xml", assertionMode = DatabaseAssertionMode.NON_STRICT)
219229
public void shouldAllowEditedAnyCommentIfAdmin() throws Exception {
220230
String text = "edited text";
221231

@@ -224,5 +234,7 @@ public void shouldAllowEditedAnyCommentIfAdmin() throws Exception {
224234
.param("commentText", text))
225235
.andExpect(status().isOk())
226236
.andExpect(content().string("ok"));
237+
238+
assertThat(commentService.getComment(3L).getModifiedDateTime().toLocalDate().equals(LocalDate.now()), equalTo(true));
227239
}
228240
}

src/test/resources/alexp/blog/controller/data-avatar-added.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<posts_tags post_id="2" tag_id="3"/>
2727

2828
<comments id="1" post_id="1" user_id="2" commentText="comment1 text" dateTime="2015-04-01 13:30:00" deleted="0"/>
29-
<comments id="2" post_id="1" user_id="1" commentText="comment2 text" dateTime="2015-04-01 13:40:00" deleted="0" parent_id="1"/>
29+
<comments id="2" post_id="1" user_id="1" commentText="comment2 text" dateTime="2015-04-01 13:40:00" deleted="0" parent_id="1" modifiedDateTime="2015-04-01 15:40:00"/>
3030
<comments id="3" post_id="1" user_id="2" commentText="comment3 text" dateTime="2015-04-01 13:50:00" deleted="0"/>
3131

3232
<persistent_logins/>

src/test/resources/alexp/blog/controller/data-comment-added-and-marked-deleted.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<posts_tags post_id="2" tag_id="3"/>
2626

2727
<comments id="1" post_id="1" user_id="2" commentText="comment1 text" deleted="0"/>
28-
<comments id="2" post_id="1" user_id="1" commentText="comment2 text" deleted="0" parent_id="1"/>
28+
<comments id="2" post_id="1" user_id="1" commentText="comment2 text" deleted="0" parent_id="1" modifiedDateTime="2015-04-01 15:40:00"/>
2929
<comments id="3" post_id="1" user_id="2" commentText="comment3 text" deleted="0"/>
3030
<comments id="4" post_id="1" user_id="3" commentText="new comment text" deleted="1"/>
3131

src/test/resources/alexp/blog/controller/data-comment-added.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<posts_tags post_id="2" tag_id="3"/>
2626

2727
<comments id="1" post_id="1" user_id="2" commentText="comment1 text" deleted="0"/>
28-
<comments id="2" post_id="1" user_id="1" commentText="comment2 text" deleted="0" parent_id="1"/>
28+
<comments id="2" post_id="1" user_id="1" commentText="comment2 text" deleted="0" parent_id="1" modifiedDateTime="2015-04-01 15:40:00"/>
2929
<comments id="3" post_id="1" user_id="2" commentText="comment3 text" deleted="0"/>
3030
<comments id="4" post_id="1" user_id="3" commentText="new comment text" deleted="0"/>
3131

src/test/resources/alexp/blog/controller/data-comment-marked-deleted.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<posts_tags post_id="2" tag_id="3"/>
2626

2727
<comments id="1" post_id="1" user_id="2" commentText="comment1 text" dateTime="2015-04-01 13:30:00" deleted="0"/>
28-
<comments id="2" post_id="1" user_id="1" commentText="comment2 text" dateTime="2015-04-01 13:40:00" deleted="0" parent_id="1"/>
28+
<comments id="2" post_id="1" user_id="1" commentText="comment2 text" dateTime="2015-04-01 13:40:00" deleted="0" parent_id="1" modifiedDateTime="2015-04-01 15:40:00"/>
2929
<comments id="3" post_id="1" user_id="2" commentText="comment3 text" dateTime="2015-04-01 13:50:00" deleted="1"/>
3030

3131
<persistent_logins/>

src/test/resources/alexp/blog/controller/data-comment-reply-added-limit-exceeded.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<posts_tags post_id="2" tag_id="3"/>
2626

2727
<comments id="1" post_id="1" user_id="2" commentText="comment1 text" deleted="0"/>
28-
<comments id="2" post_id="1" user_id="1" commentText="comment2 text" deleted="0" parent_id="1"/>
28+
<comments id="2" post_id="1" user_id="1" commentText="comment2 text" deleted="0" parent_id="1" modifiedDateTime="2015-04-01 15:40:00"/>
2929
<comments id="3" post_id="1" user_id="2" commentText="comment3 text" deleted="0"/>
3030
<comments id="4" post_id="1" user_id="3" commentText="new comment text" deleted="0" parent_id="3"/>
3131
<comments id="5" post_id="1" user_id="3" commentText="new comment text" deleted="0" parent_id="4"/>

0 commit comments

Comments
 (0)