-
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.
- Loading branch information
Showing
22 changed files
with
495 additions
and
26 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
src/main/java/com/stempo/api/domain/application/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,21 @@ | ||
package com.stempo.api.domain.application.service; | ||
|
||
import com.stempo.api.domain.presentation.dto.request.ArticleRequestDto; | ||
import com.stempo.api.domain.presentation.dto.request.ArticleUpdateRequestDto; | ||
import com.stempo.api.domain.presentation.dto.response.ArticleDetailsResponseDto; | ||
import com.stempo.api.domain.presentation.dto.response.ArticleResponseDto; | ||
|
||
import java.util.List; | ||
|
||
public interface ArticleService { | ||
|
||
Long registerArticle(ArticleRequestDto requestDto); | ||
|
||
List<ArticleResponseDto> getArticles(); | ||
|
||
ArticleDetailsResponseDto getArticle(Long articleId); | ||
|
||
Long updateArticle(Long articleId, ArticleUpdateRequestDto requestDto); | ||
|
||
Long deleteArticle(Long articleId); | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/stempo/api/domain/application/service/ArticleServiceImpl.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,53 @@ | ||
package com.stempo.api.domain.application.service; | ||
|
||
import com.stempo.api.domain.domain.model.Article; | ||
import com.stempo.api.domain.domain.repository.ArticleRepository; | ||
import com.stempo.api.domain.presentation.dto.request.ArticleRequestDto; | ||
import com.stempo.api.domain.presentation.dto.request.ArticleUpdateRequestDto; | ||
import com.stempo.api.domain.presentation.dto.response.ArticleDetailsResponseDto; | ||
import com.stempo.api.domain.presentation.dto.response.ArticleResponseDto; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ArticleServiceImpl implements ArticleService { | ||
|
||
private final ArticleRepository repository; | ||
|
||
@Override | ||
public Long registerArticle(ArticleRequestDto requestDto) { | ||
Article article = ArticleRequestDto.toDomain(requestDto); | ||
return repository.save(article).getId(); | ||
} | ||
|
||
@Override | ||
public List<ArticleResponseDto> getArticles() { | ||
List<Article> articles = repository.findAll(); | ||
return articles.stream() | ||
.map(ArticleResponseDto::toDto) | ||
.toList(); | ||
} | ||
|
||
@Override | ||
public ArticleDetailsResponseDto getArticle(Long articleId) { | ||
Article article = repository.findByIdOrThrow(articleId); | ||
return ArticleDetailsResponseDto.toDto(article); | ||
} | ||
|
||
@Override | ||
public Long updateArticle(Long articleId, ArticleUpdateRequestDto requestDto) { | ||
Article article = repository.findByIdOrThrow(articleId); | ||
article.update(requestDto); | ||
return repository.save(article).getId(); | ||
} | ||
|
||
@Override | ||
public Long deleteArticle(Long articleId) { | ||
Article article = repository.findByIdOrThrow(articleId); | ||
article.delete(); | ||
return repository.save(article).getId(); | ||
} | ||
} |
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
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
39 changes: 39 additions & 0 deletions
39
src/main/java/com/stempo/api/domain/domain/model/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,39 @@ | ||
package com.stempo.api.domain.domain.model; | ||
|
||
import com.stempo.api.domain.presentation.dto.request.ArticleUpdateRequestDto; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Optional; | ||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class Article { | ||
|
||
private Long id; | ||
private String title; | ||
private String content; | ||
private String thumbnailUrl; | ||
private String articleUrl; | ||
private LocalDateTime createdAt; | ||
private boolean deleted = false; | ||
|
||
public void update(ArticleUpdateRequestDto requestDto) { | ||
Optional.ofNullable(requestDto.getTitle()).ifPresent(this::setTitle); | ||
Optional.ofNullable(requestDto.getContent()).ifPresent(this::setContent); | ||
Optional.ofNullable(requestDto.getThumbnailUrl()).ifPresent(this::setThumbnailUrl); | ||
Optional.ofNullable(requestDto.getArticleUrl()).ifPresent(this::setArticleUrl); | ||
} | ||
|
||
public void delete() { | ||
setDeleted(true); | ||
} | ||
} |
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
14 changes: 14 additions & 0 deletions
14
src/main/java/com/stempo/api/domain/domain/repository/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,14 @@ | ||
package com.stempo.api.domain.domain.repository; | ||
|
||
import com.stempo.api.domain.domain.model.Article; | ||
|
||
import java.util.List; | ||
|
||
public interface ArticleRepository { | ||
|
||
Article save(Article article); | ||
|
||
List<Article> findAll(); | ||
|
||
Article findByIdOrThrow(Long articleId); | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/stempo/api/domain/persistence/mappper/ArticleMapper.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,32 @@ | ||
package com.stempo.api.domain.persistence.mappper; | ||
|
||
import com.stempo.api.domain.domain.model.Article; | ||
import com.stempo.api.domain.persistence.entity.ArticleEntity; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ArticleMapper { | ||
|
||
public ArticleEntity toEntity(Article article) { | ||
return ArticleEntity.builder() | ||
.id(article.getId()) | ||
.title(article.getTitle()) | ||
.content(article.getContent()) | ||
.thumbnailUrl(article.getThumbnailUrl()) | ||
.articleUrl(article.getArticleUrl()) | ||
.deleted(article.isDeleted()) | ||
.build(); | ||
} | ||
|
||
public Article toDomain(ArticleEntity entity) { | ||
return Article.builder() | ||
.id(entity.getId()) | ||
.title(entity.getTitle()) | ||
.content(entity.getContent()) | ||
.thumbnailUrl(entity.getThumbnailUrl()) | ||
.articleUrl(entity.getArticleUrl()) | ||
.createdAt(entity.getCreatedAt()) | ||
.deleted(entity.isDeleted()) | ||
.build(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/stempo/api/domain/persistence/repository/ArticleJpaRepository.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,22 @@ | ||
package com.stempo.api.domain.persistence.repository; | ||
|
||
import com.stempo.api.domain.persistence.entity.ArticleEntity; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface ArticleJpaRepository extends JpaRepository<ArticleEntity, Long> { | ||
|
||
@Query("SELECT a " + | ||
"FROM ArticleEntity a " + | ||
"WHERE a.deleted = false " + | ||
"ORDER BY a.createdAt ASC") | ||
List<ArticleEntity> findAllArticles(); | ||
|
||
@Query("SELECT a " + | ||
"FROM ArticleEntity a " + | ||
"WHERE a.id = :articleId AND a.deleted = false") | ||
Optional<ArticleEntity> findByIdAndNotDeleted(Long articleId); | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/stempo/api/domain/persistence/repository/ArticleRepositoryImpl.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,42 @@ | ||
package com.stempo.api.domain.persistence.repository; | ||
|
||
import com.stempo.api.domain.domain.model.Article; | ||
import com.stempo.api.domain.domain.repository.ArticleRepository; | ||
import com.stempo.api.domain.persistence.entity.ArticleEntity; | ||
import com.stempo.api.domain.persistence.mappper.ArticleMapper; | ||
import com.stempo.api.global.exception.NotFoundException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class ArticleRepositoryImpl implements ArticleRepository { | ||
|
||
private final ArticleJpaRepository repository; | ||
private final ArticleMapper mapper; | ||
|
||
|
||
@Override | ||
public Article save(Article article) { | ||
ArticleEntity jpaEntity = mapper.toEntity(article); | ||
ArticleEntity savedEntity = repository.save(jpaEntity); | ||
return mapper.toDomain(savedEntity); | ||
} | ||
|
||
@Override | ||
public List<Article> findAll() { | ||
List<ArticleEntity> articles = repository.findAllArticles(); | ||
return articles.stream() | ||
.map(mapper::toDomain) | ||
.toList(); | ||
} | ||
|
||
@Override | ||
public Article findByIdOrThrow(Long articleId) { | ||
return repository.findByIdAndNotDeleted(articleId) | ||
.map(mapper::toDomain) | ||
.orElseThrow(() -> new NotFoundException("[Article] id: " + articleId + " not found")); | ||
} | ||
} |
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
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
Oops, something went wrong.