Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

재활 관련 정보 제공 완료 #21

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.stempo.api.domain.presentation.dto.request.VideoRequestDto;
import com.stempo.api.domain.presentation.dto.request.VideoUpdateRequestDto;
import com.stempo.api.domain.presentation.dto.response.VideoDetailsResponseDto;
import com.stempo.api.domain.presentation.dto.response.VideoResponseDto;

import java.util.List;
Expand All @@ -12,6 +13,8 @@ public interface VideoService {

List<VideoResponseDto> getVideos();

VideoDetailsResponseDto getVideo(Long videoId);

Long updateVideo(Long videoId, VideoUpdateRequestDto requestDto);

Long deleteVideo(Long id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.stempo.api.domain.domain.repository.VideoRepository;
import com.stempo.api.domain.presentation.dto.request.VideoRequestDto;
import com.stempo.api.domain.presentation.dto.request.VideoUpdateRequestDto;
import com.stempo.api.domain.presentation.dto.response.VideoDetailsResponseDto;
import com.stempo.api.domain.presentation.dto.response.VideoResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -14,33 +15,39 @@
@RequiredArgsConstructor
public class VideoServiceImpl implements VideoService {

private final VideoRepository videoRepository;
private final VideoRepository repository;

@Override
public Long registerVideo(VideoRequestDto requestDto) {
Video video = VideoRequestDto.toDomain(requestDto);
return videoRepository.save(video).getId();
return repository.save(video).getId();
}

@Override
public List<VideoResponseDto> getVideos() {
List<Video> videos = videoRepository.findAll();
List<Video> videos = repository.findAll();
return videos.stream()
.map(VideoResponseDto::toDto)
.toList();
}

@Override
public VideoDetailsResponseDto getVideo(Long videoId) {
Video video = repository.findByIdOrThrow(videoId);
return VideoDetailsResponseDto.toDto(video);
}

@Override
public Long updateVideo(Long videoId, VideoUpdateRequestDto requestDto) {
Video video = videoRepository.findByIdOrThrow(videoId);
Video video = repository.findByIdOrThrow(videoId);
video.update(requestDto);
return videoRepository.save(video).getId();
return repository.save(video).getId();
}

@Override
public Long deleteVideo(Long id) {
Video video = videoRepository.findByIdOrThrow(id);
Video video = repository.findByIdOrThrow(id);
video.delete();
return videoRepository.save(video).getId();
return repository.save(video).getId();
}
}
39 changes: 39 additions & 0 deletions src/main/java/com/stempo/api/domain/domain/model/Article.java
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);
}
}
16 changes: 6 additions & 10 deletions src/main/java/com/stempo/api/domain/domain/model/Video.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.stempo.api.domain.domain.model;

import com.stempo.api.domain.presentation.dto.request.VideoUpdateRequestDto;
import lombok.*;
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;
Expand All @@ -21,15 +26,6 @@ public class Video {
private LocalDateTime createdAt;
private boolean deleted;

public static Video create(String title, String content, String thumbnailUrl, String videoUrl) {
return Video.builder()
.title(title)
.content(content)
.thumbnailUrl(thumbnailUrl)
.videoUrl(videoUrl)
.build();
}

public void update(VideoUpdateRequestDto requestDto) {
Optional.ofNullable(requestDto.getTitle()).ifPresent(this::setTitle);
Optional.ofNullable(requestDto.getContent()).ifPresent(this::setContent);
Expand Down
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);
}
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();
}
}
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);
}
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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface VideoJpaRepository extends JpaRepository<VideoEntity, Long> {

Expand All @@ -13,4 +14,9 @@ public interface VideoJpaRepository extends JpaRepository<VideoEntity, Long> {
"WHERE v.deleted = false " +
"ORDER BY v.createdAt ASC")
List<VideoEntity> findAllVideos();

@Query("SELECT v " +
"FROM VideoEntity v " +
"WHERE v.id = :videoId AND v.deleted = false")
Optional<VideoEntity> findByIdAndNotDeleted(Long videoId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public List<Video> findAll() {

@Override
public Video findByIdOrThrow(Long videoId) {
return repository.findById(videoId)
return repository.findByIdAndNotDeleted(videoId)
.map(mapper::toDomain)
.orElseThrow(() -> new NotFoundException("[Video] id: " + videoId + " not found"));
}
Expand Down
Loading