Skip to content

Commit

Permalink
[kiworkshop#31] Refactor domain
Browse files Browse the repository at this point in the history
- Refactor Article, Tag domain
- Add article.http
  • Loading branch information
jiwooo-kim committed Nov 6, 2019
1 parent bbfaa68 commit 2d8ed4f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# For a quick start check out our HTTP Requests collection (Tools|HTTP Client|Open HTTP Requests Collection) or
# paste cURL into the file and request will be converted to HTTP Request format.
#
# Following HTTP Request Live Templates are available:
# * 'gtrp' and 'gtr' create a GET request with or without query parameters;
# * 'ptr' and 'ptrp' create a POST request with a simple or parameter-like body;
# * 'mptr' and 'fptr' create a POST request to submit a form with a text or file field (multipart/form-data);import community.content.simplelife.article.domain.SimpleArticle;
POST localhost:8080/simplelife/articles
Content-Type: application/json

{
"title": "title1",
"description": "description1",
"content": "content1",
"simpleTags": ["tag1","tag2"]
}

###
DELETE localhost:8080/simplelife/articles/1

###
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class SimpleArticle {
private ZonedDateTime updatedAt;
@ManyToMany(fetch = FetchType.LAZY,
cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinTable(name = "post_tags",
@JoinTable(name = "post_tag",
joinColumns = { @JoinColumn(name = "post_id") },
inverseJoinColumns = { @JoinColumn(name = "tag_id") })
private Set<SimpleTag> simpleTags = new HashSet<>();
Expand All @@ -62,10 +62,16 @@ public void addTags(Set<SimpleTag> simpleTags) {
this.simpleTags.addAll(simpleTags);
}

public void update(String title, String description, String content, Set<SimpleTag> simpleTags) {
this.title = title;
this.description = description;
this.content = content;
this.simpleTags.addAll(simpleTags);
public void updateTag(Set<SimpleTag> simpleTags) {
this.simpleTags = simpleTags;
}

public void update(SimpleArticle simpleArticle, Set<SimpleTag> simpleTags) {
this.simpleTags.forEach(simpleTag -> simpleTag.deletePost(this));

this.title = simpleArticle.title;
this.description = simpleArticle.description;
this.content = simpleArticle.content;
this.simpleTags = simpleTags;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public class SimpleTag {
private Set<SimpleArticle> simpleArticles = new HashSet<>();

@Builder
private SimpleTag(String name
) {
private SimpleTag(String name) {
Assert.hasLength(name, "tagName should not be empty.");

this.name = name;
Expand All @@ -37,4 +36,8 @@ private SimpleTag(String name
public void addArticle(SimpleArticle simpleArticle) {
this.simpleArticles.add(simpleArticle);
}

public void deletePost(SimpleArticle simpleArticle) {
this.simpleArticles.remove(simpleArticle);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package community.content.simplelife.article.domain;

import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface SimpleTagRepository extends JpaRepository<SimpleTag, Long> {
Optional<SimpleTag> findByName(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,21 @@ void update_ValidInput_ValidOutput() {
.content("content")
.simpleTags(simpleTags).build();

SimpleArticle articleToUpdate = SimpleArticle.builder()
.title("updated title")
.description("updated description")
.content("updated content").build();

// when
simpleArticle.update("updated title", "updated description", "updated content", newSimpleTags);
simpleArticle.update(articleToUpdate, newSimpleTags);

// then
simpleTags.addAll(newSimpleTags);
then(simpleArticle).hasNoNullFieldsOrPropertiesExcept("id","createdAt", "updatedAt");
then(simpleArticle.getTitle()).isEqualTo("updated title");
then(simpleArticle.getDescription()).isEqualTo("updated description");
then(simpleArticle.getContent()).isEqualTo("updated content");
then(simpleArticle.getSimpleTags()).isEqualTo(simpleTags);
then(simpleArticle.getSimpleTags()).isEqualTo(newSimpleTags);
}

@Test
Expand Down

0 comments on commit 2d8ed4f

Please sign in to comment.