Skip to content
This repository was archived by the owner on Jul 7, 2021. It is now read-only.

Commit 5fa3811

Browse files
committed
#20: Add shortcut for posts
1 parent af25886 commit 5fa3811

File tree

8 files changed

+41
-50
lines changed

8 files changed

+41
-50
lines changed

src/main/java/org/proshin/blog/dynamodb/DynamoPost.java

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
import com.amazonaws.services.dynamodbv2.document.Table;
77
import java.time.LocalDateTime;
88
import static java.time.LocalDateTime.now;
9+
import lombok.EqualsAndHashCode;
910
import lombok.NonNull;
10-
import org.apache.commons.lang3.builder.EqualsBuilder;
11-
import org.apache.commons.lang3.builder.HashCodeBuilder;
1211
import org.proshin.blog.DateToString;
1312
import org.proshin.blog.StringToDate;
1413
import org.proshin.blog.Url;
1514
import org.proshin.blog.model.PersistentPost;
1615

16+
@EqualsAndHashCode(exclude = "posts")
1717
public class DynamoPost implements PersistentPost {
1818
private final Table posts;
1919
private final Url url;
2020
private final String title;
21+
private final String shortcut;
2122
private final LocalDateTime creationDate;
2223
private final LocalDateTime publicationDate;
2324
private final boolean published;
@@ -27,18 +28,20 @@ public DynamoPost(@NonNull Table posts, @NonNull Item item) {
2728
this(posts,
2829
new Url(item.getString("url")),
2930
item.getString("title"),
31+
item.getString("shortcut"),
3032
new StringToDate(item.getString("creation_date")).toLocalDateTime(),
3133
new StringToDate(item.getString("publication_date")).toLocalDateTime(),
3234
item.getBOOL("published"),
3335
item.getString("content"));
3436
}
3537

36-
public DynamoPost(@NonNull Table posts, @NonNull Url url, @NonNull String title,
38+
public DynamoPost(@NonNull Table posts, @NonNull Url url, @NonNull String title, @NonNull String shortcut,
3739
@NonNull LocalDateTime creationDate, @NonNull LocalDateTime publicationDate, boolean published,
3840
@NonNull String content) {
3941
this.posts = posts;
4042
this.url = url;
4143
this.title = title;
44+
this.shortcut = shortcut;
4245
this.creationDate = creationDate;
4346
this.publicationDate = publicationDate;
4447
this.published = published;
@@ -55,6 +58,11 @@ public String title() {
5558
return title;
5659
}
5760

61+
@Override
62+
public String shortcut() {
63+
return shortcut;
64+
}
65+
5866
@Override
5967
public LocalDateTime creationDate() {
6068
return creationDate;
@@ -78,13 +86,13 @@ public String content() {
7886
@NonNull
7987
@Override
8088
public PersistentPost publish() {
81-
return new DynamoPost(posts, url, title, creationDate, now(), true, content);
89+
return new DynamoPost(posts, url, title, shortcut, creationDate, now(), true, content);
8290
}
8391

8492
@NonNull
8593
@Override
8694
public PersistentPost unpublish() {
87-
return new DynamoPost(posts, url, title, creationDate, publicationDate, false, content);
95+
return new DynamoPost(posts, url, title, shortcut, creationDate, publicationDate, false, content);
8896
}
8997

9098
@NonNull
@@ -94,6 +102,7 @@ public PersistentPost persist() {
94102
new Item()
95103
.withPrimaryKey(new PrimaryKey("url", url.decoded()))
96104
.with("title", title)
105+
.with("shortcut", shortcut)
97106
.with("creation_date", new DateToString(creationDate).toString())
98107
.with("publication_date", new DateToString(publicationDate).toString())
99108
.with("published", published)
@@ -107,6 +116,7 @@ public PersistentPost persist() {
107116
public PersistentPost update() {
108117
posts.updateItem(new PrimaryKey("url", url.decoded()),
109118
new AttributeUpdate("title").put(title),
119+
new AttributeUpdate("shortcut").put(shortcut),
110120
new AttributeUpdate("creation_date")
111121
.put(new DateToString(creationDate).toString()),
112122
new AttributeUpdate("publication_date")
@@ -120,36 +130,4 @@ public PersistentPost update() {
120130
public void delete() {
121131
posts.deleteItem(new PrimaryKey("url", url.decoded()));
122132
}
123-
124-
@Override
125-
public boolean equals(Object o) {
126-
if (this == o)
127-
return true;
128-
129-
if (o == null || getClass() != o.getClass())
130-
return false;
131-
132-
DynamoPost that = (DynamoPost) o;
133-
134-
return new EqualsBuilder()
135-
.append(published, that.published)
136-
.append(url, that.url)
137-
.append(title, that.title)
138-
.append(creationDate, that.creationDate)
139-
.append(publicationDate, that.publicationDate)
140-
.append(content, that.content)
141-
.isEquals();
142-
}
143-
144-
@Override
145-
public int hashCode() {
146-
return new HashCodeBuilder(17, 37)
147-
.append(url)
148-
.append(title)
149-
.append(creationDate)
150-
.append(publicationDate)
151-
.append(published)
152-
.append(content)
153-
.toHashCode();
154-
}
155133
}

src/main/java/org/proshin/blog/dynamodb/DynamoPosts.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.List;
1313
import java.util.Optional;
1414
import lombok.NonNull;
15+
import static org.apache.commons.lang3.StringUtils.left;
1516
import org.proshin.blog.Url;
1617
import org.proshin.blog.model.PersistentPost;
1718
import org.proshin.blog.model.PersistentPosts;
@@ -64,7 +65,7 @@ public List<PersistentPost> page(int offset, int count, boolean publishedOnly) {
6465
@NonNull
6566
@Override
6667
public PersistentPost newPost(@NonNull Url url, @NonNull String title, @NonNull String content) {
67-
return new DynamoPost(table, url, title, now(), now(), false, content)
68+
return new DynamoPost(table, url, title, left(content, 512), now(), now(), false, content)
6869
.persist();
6970
}
7071

src/main/java/org/proshin/blog/model/Post.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public interface Post {
88

99
String title();
1010

11+
String shortcut();
12+
1113
LocalDateTime creationDate();
1214

1315
LocalDateTime publicationDate();

src/main/java/org/proshin/blog/page/admin/AdminPostsPagesController.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public ModelAndView edit(@PathVariable("url") Url url) {
6262
new ChangedPost(
6363
post.url().decoded(),
6464
post.title(),
65+
post.shortcut(),
6566
post.creationDate(),
6667
post.publicationDate(),
6768
post.published(),
@@ -102,7 +103,7 @@ public ModelAndView save(@PathVariable("originalUrl") Url originalUrl,
102103
}
103104
Url newUrl = new Url(post.getUrl());
104105
DynamoPost dynamoPost =
105-
new DynamoPost(posts.getTable(), newUrl, post.getTitle(), post.getCreationDate(),
106+
new DynamoPost(posts.getTable(), newUrl, post.getTitle(), post.getShortcut(), post.getCreationDate(),
106107
post.getPublicationDate(), post.isPublished(), post.getContent());
107108
if (!newUrl.equals(originalUrl)) {
108109
dynamoPost.persist();
@@ -124,6 +125,8 @@ public static class ChangedPost {
124125
private String url;
125126
@NotBlank
126127
private String title;
128+
@NotBlank
129+
private String shortcut;
127130
private LocalDateTime creationDate;
128131
private LocalDateTime publicationDate;
129132
private boolean published;

src/test/java/org/proshin/blog/dynamodb/DynamoPostsTest.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ public class DynamoPostsTest extends AbstractIntegrationTest {
2424
@Test
2525
public void testSelectOne_findExistingPost() {
2626
PersistentPost post =
27-
new DynamoPost(posts.getTable(), new Url("url"), "Published post", now(), now(), true, "Some content")
28-
.persist();
27+
new DynamoPost(posts.getTable(), new Url("url"), "Published post", "Shortcut", now(), now(), true,
28+
"Some content")
29+
.persist();
2930
assertThat(posts.postByUrl(post.url()).orElseThrow(() -> new PostNotFoundException(new Url("url"))), is(post));
3031
}
3132

@@ -38,11 +39,13 @@ public void testSelectOne_findNonExistingPost() {
3839
@Test
3940
public void testThatPublishedAreFilteredOut() {
4041
Url publishedUrl =
41-
new DynamoPost(posts.getTable(), new Url("url-1"), "Published post", now(), now(), true, "Some content")
42-
.persist()
43-
.url();
42+
new DynamoPost(posts.getTable(), new Url("url-1"), "Published post", "Shortcut", now(), now(), true,
43+
"Some content")
44+
.persist()
45+
.url();
4446
Url nonPublishedUrl =
45-
new DynamoPost(posts.getTable(), new Url("url-2"), "NOT published post", now(), now(), false,
47+
new DynamoPost(posts.getTable(), new Url("url-2"), "NOT published post", "Shortcut", now(), now(),
48+
false,
4649
"Some content")
4750
.persist()
4851
.url();
@@ -77,8 +80,9 @@ public void testCreate() {
7780
@Test(expected = PostNotFoundException.class)
7881
public void testDelete() {
7982
PersistentPost post =
80-
new DynamoPost(posts.getTable(), new Url("url"), "Published post", now(), now(), true, "Some content")
81-
.persist();
83+
new DynamoPost(posts.getTable(), new Url("url"), "Published post", "Shortcut", now(), now(), true,
84+
"Some content")
85+
.persist();
8286

8387
post.delete();
8488

src/test/java/org/proshin/blog/page/GuestPagesControllerIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void testThatNonExistingPostReturnsNotFound() throws Exception {
4343
public void testThatNonPublishedPostIsNotAvailableForGuests() throws Exception {
4444
PersistentPost post =
4545
new DynamoPost(dynamoDB.getTable(DynamoPosts.TABLE_NAME),
46-
new Url("url"), "Some test post", now(), now(), false, "Just a piece of content")
46+
new Url("url"), "Some test post", "Shortcut", now(), now(), false, "Just a piece of content")
4747
.persist();
4848
// when it's not published
4949
post.unpublish().persist();

src/test/java/org/proshin/blog/page/GuestPagesControllerTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public class GuestPagesControllerTest {
2525
public void testThatIndexPageShowsFirstArticles() throws Exception {
2626
List<PersistentPost> postsForIndexPage =
2727
singletonList(
28-
new DynamoPost(postsTable, new Url("10"), "Test post #10", now(), now(), true, "content"));
28+
new DynamoPost(postsTable, new Url("10"), "Test post #10", "Shortcut", now(), now(), true,
29+
"content"));
2930

3031
when(posts.page(0, 10, true))
3132
.thenReturn(postsForIndexPage);
@@ -39,7 +40,7 @@ public void testThatIndexPageShowsFirstArticles() throws Exception {
3940
@Test
4041
public void testThatViewPostShowsTheRequestedPost() throws Exception {
4142
PersistentPost requestedPost =
42-
new DynamoPost(postsTable, new Url("10"), "Test post #10", now(), now(), true,
43+
new DynamoPost(postsTable, new Url("10"), "Test post #10", "Shortcut", now(), now(), true,
4344
"*markdown* content");
4445
when(posts.postByUrl(new Url("10")))
4546
.thenReturn(Optional.of(requestedPost));

src/test/java/org/proshin/blog/page/admin/AdminPostsPagesControllerTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public void testSave() throws Exception {
133133
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
134134
.param("url", post.url().decoded())
135135
.param("title", "New title")
136+
.param("shortcut", "New shortcut")
136137
.param("content", "New content")
137138
.param("creationDate", "2018-01-09T13:23")
138139
.param("publicationDate", "2018-01-07T13:23")
@@ -142,6 +143,7 @@ public void testSave() throws Exception {
142143

143144
posts.postByUrl(post.url()).ifPresent(changedPost -> {
144145
assertThat(changedPost.title(), is("New title"));
146+
assertThat(changedPost.shortcut(), is("New shortcut"));
145147
assertThat(changedPost.content(), is("New content"));
146148
assertThat(changedPost.creationDate(),
147149
is(new StringToDate("2018-01-09T13:23").toLocalDateTime()));

0 commit comments

Comments
 (0)