forked from kiworkshop/community
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
272 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
...pp/app-monolith/src/main/java/org/kiworkshop/community/article/api/ArticleController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.kiworkshop.community.article.api; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.kiworkshop.community.article.api.dto.ArticleRequestDto; | ||
import org.kiworkshop.community.article.api.dto.ArticleResponseDto; | ||
import org.kiworkshop.community.article.service.ArticleService; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("/article") | ||
@RequiredArgsConstructor | ||
public class ArticleController { | ||
private final ArticleService articleService; | ||
|
||
@GetMapping("/{id}") | ||
public ResponseEntity<ArticleResponseDto> read(@PathVariable Long id) { | ||
return ResponseEntity.ok(articleService.read(id)); | ||
} | ||
|
||
@PostMapping | ||
public ResponseEntity<Long> create(@RequestBody ArticleRequestDto articleRequestDto) { | ||
return ResponseEntity.ok(articleService.create(articleRequestDto)); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity<Void> delete(@PathVariable Long id) { | ||
articleService.delete(id); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@PutMapping("/{id}") | ||
public ResponseEntity<Void> update(@PathVariable Long id, @RequestBody ArticleRequestDto articleRequestDto) { | ||
articleService.update(id, articleRequestDto); | ||
return ResponseEntity.ok().build(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...pp-monolith/src/main/java/org/kiworkshop/community/article/api/dto/ArticleRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.kiworkshop.community.article.api.dto; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.kiworkshop.community.article.entity.Article; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class ArticleRequestDto { | ||
private String title; | ||
|
||
@Builder | ||
public ArticleRequestDto(String title, String content) { | ||
this.title = title; | ||
this.content = content; | ||
} | ||
|
||
private String content; | ||
|
||
public Article toEntity() { | ||
return Article.builder() | ||
.title(title) | ||
.content(content) | ||
.build(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...p-monolith/src/main/java/org/kiworkshop/community/article/api/dto/ArticleResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.kiworkshop.community.article.api.dto; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.kiworkshop.community.article.entity.Article; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class ArticleResponseDto { | ||
private String title; | ||
private String content; | ||
|
||
@Builder | ||
public ArticleResponseDto(String title, String content) { | ||
this.title = title; | ||
this.content = content; | ||
} | ||
|
||
public static ArticleResponseDto from(Article article) { | ||
return ArticleResponseDto.builder() | ||
.title(article.getTitle()) | ||
.content(article.getContent()) | ||
.build(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...ckend/app/app-monolith/src/main/java/org/kiworkshop/community/article/entity/Article.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.kiworkshop.community.article.entity; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.kiworkshop.community.common.domain.BaseEntity; | ||
|
||
import javax.persistence.Entity; | ||
|
||
@Entity | ||
@Getter | ||
public class Article extends BaseEntity { | ||
private String title; | ||
private String content; | ||
private Long userId; | ||
|
||
@Builder | ||
public Article(String title, String content, Long userId) { | ||
this.title = title; | ||
this.content = content; | ||
this.userId = userId; | ||
} | ||
|
||
public void update(Article article) { | ||
this.title = article.title; | ||
this.content = article.content; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...app-monolith/src/main/java/org/kiworkshop/community/article/entity/ArticleRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.kiworkshop.community.article.entity; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface ArticleRepository extends JpaRepository<Article, Long> { | ||
} |
7 changes: 7 additions & 0 deletions
7
...p-monolith/src/main/java/org/kiworkshop/community/article/exception/ArticleException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.kiworkshop.community.article.exception; | ||
|
||
public class ArticleException extends RuntimeException { | ||
public ArticleException(String s) { | ||
super(s); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...p/app-monolith/src/main/java/org/kiworkshop/community/article/service/ArticleService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.kiworkshop.community.article.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.kiworkshop.community.article.api.dto.ArticleRequestDto; | ||
import org.kiworkshop.community.article.api.dto.ArticleResponseDto; | ||
import org.kiworkshop.community.article.entity.Article; | ||
import org.kiworkshop.community.article.entity.ArticleRepository; | ||
import org.kiworkshop.community.article.exception.ArticleException; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ArticleService { | ||
private final ArticleRepository articleRepository; | ||
|
||
public Long create(ArticleRequestDto articleRequestDto) { | ||
Article article = articleRepository.save(articleRequestDto.toEntity()); | ||
return article.getId(); | ||
} | ||
|
||
public ArticleResponseDto read(Long id) { | ||
Article article = findById(id); | ||
return ArticleResponseDto.from(article); | ||
} | ||
|
||
public void update(Long id, ArticleRequestDto articleRequestDto) { | ||
Article article = findById(id); | ||
article.update(articleRequestDto.toEntity()); | ||
} | ||
|
||
public void delete(Long id) { | ||
articleRepository.deleteById(id); | ||
} | ||
|
||
private Article findById(Long id) { | ||
return articleRepository.findById(id).orElseThrow(() -> new ArticleException("게시물을 찾을 수 없습니다.")); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...d/app/app-monolith/src/test/java/org/kiworkshop/community/article/entity/ArticleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.kiworkshop.community.article.entity; | ||
|
||
import org.springframework.test.util.ReflectionTestUtils; | ||
|
||
import java.time.ZonedDateTime; | ||
|
||
public class ArticleTest { | ||
public static Article createArticleTestFixture() { | ||
Article article = Article.builder().title("title").content("content").build(); | ||
ReflectionTestUtils.setField(article, "id", 1L); | ||
ReflectionTestUtils.setField(article, "updatedAt", ZonedDateTime.now()); | ||
ReflectionTestUtils.setField(article, "createdAt", ZonedDateTime.now()); | ||
return article; | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...p-monolith/src/test/java/org/kiworkshop/community/article/service/ArticleServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package org.kiworkshop.community.article.service; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.kiworkshop.community.article.api.dto.ArticleRequestDto; | ||
import org.kiworkshop.community.article.api.dto.ArticleResponseDto; | ||
import org.kiworkshop.community.article.entity.Article; | ||
import org.kiworkshop.community.article.entity.ArticleRepository; | ||
import org.kiworkshop.community.article.exception.ArticleException; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.kiworkshop.community.article.entity.ArticleTest.createArticleTestFixture; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyLong; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.BDDMockito.then; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class ArticleServiceTest { | ||
private ArticleService articleService; | ||
@Mock | ||
private ArticleRepository articleRepository; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
this.articleService = new ArticleService(articleRepository); | ||
} | ||
|
||
@Test | ||
void create() { | ||
//given | ||
ArticleRequestDto articleRequestDto = ArticleRequestDto.builder().title("title").content("content").build(); | ||
Article article = createArticleTestFixture(); | ||
given(articleRepository.save(any(Article.class))).willReturn(article); | ||
//when | ||
Long id = articleService.create(articleRequestDto); | ||
//then | ||
assertThat(id).isNotNull(); | ||
then(articleRepository).should().save(any(Article.class)); | ||
} | ||
|
||
@Test | ||
void read() { | ||
//given | ||
Article article = createArticleTestFixture(); | ||
given(articleRepository.findById(anyLong())).willReturn(Optional.of(article)); | ||
//when | ||
ArticleResponseDto articleResponseDto = articleService.read(1L); | ||
//then | ||
assertThat(articleResponseDto.getTitle()).isEqualTo(article.getTitle()); | ||
assertThat(articleResponseDto.getContent()).isEqualTo(article.getContent()); | ||
then(articleRepository).should().findById(anyLong()); | ||
} | ||
|
||
@Test | ||
void update() { | ||
//given | ||
ArticleRequestDto articleRequestDto = ArticleRequestDto.builder().title("title1").content("content1").build(); | ||
Article article = createArticleTestFixture(); | ||
given(articleRepository.findById(anyLong())).willReturn(Optional.of(article)); | ||
//when | ||
articleService.update(1L, articleRequestDto); | ||
//then | ||
then(articleRepository).should().findById(anyLong()); | ||
} | ||
|
||
@Test | ||
void delete() { | ||
//when | ||
articleService.delete(1L); | ||
//then | ||
then(articleRepository).should().deleteById(anyLong()); | ||
} | ||
|
||
@Test | ||
void findByIdThrowsException() { | ||
//given | ||
ArticleRequestDto articleRequestDto = ArticleRequestDto.builder().title("title1").content("content1").build(); | ||
//when & then | ||
assertThrows(ArticleException.class, () -> articleService.read(1L)); | ||
assertThrows(ArticleException.class, () -> articleService.update(1L, articleRequestDto)); | ||
} | ||
} |