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

페이지네이션 완료 #26

Merged
merged 3 commits into from
Jul 25, 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
Expand Up @@ -3,14 +3,14 @@
import com.stempo.api.domain.presentation.dto.request.AchievementRequestDto;
import com.stempo.api.domain.presentation.dto.request.AchievementUpdateRequestDto;
import com.stempo.api.domain.presentation.dto.response.AchievementResponseDto;

import java.util.List;
import com.stempo.api.global.common.dto.PagedResponseDto;
import org.springframework.data.domain.Pageable;

public interface AchievementService {

Long registerAchievement(AchievementRequestDto requestDto);

List<AchievementResponseDto> getAchievements();
PagedResponseDto<AchievementResponseDto> getAchievements(Pageable pageable);

Long updateAchievement(Long achievementId, AchievementUpdateRequestDto requestDto);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import com.stempo.api.domain.presentation.dto.request.AchievementRequestDto;
import com.stempo.api.domain.presentation.dto.request.AchievementUpdateRequestDto;
import com.stempo.api.domain.presentation.dto.response.AchievementResponseDto;
import com.stempo.api.global.common.dto.PagedResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class AchievementServiceImpl implements AchievementService {
Expand All @@ -27,11 +28,9 @@ public Long registerAchievement(AchievementRequestDto requestDto) {
}

@Override
public List<AchievementResponseDto> getAchievements() {
List<Achievement> achievements = repository.findAll();
return achievements.stream()
.map(AchievementResponseDto::toDto)
.toList();
public PagedResponseDto<AchievementResponseDto> getAchievements(Pageable pageable) {
Page<Achievement> achievements = repository.findAll(pageable);
return new PagedResponseDto<>(achievements.map(AchievementResponseDto::toDto));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
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;
import com.stempo.api.global.common.dto.PagedResponseDto;
import org.springframework.data.domain.Pageable;

public interface ArticleService {

Long registerArticle(ArticleRequestDto requestDto);

List<ArticleResponseDto> getArticles();
PagedResponseDto<ArticleResponseDto> getArticles(Pageable pageable);

ArticleDetailsResponseDto getArticle(Long articleId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
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 com.stempo.api.global.common.dto.PagedResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ArticleServiceImpl implements ArticleService {
Expand All @@ -24,11 +25,9 @@ public Long registerArticle(ArticleRequestDto requestDto) {
}

@Override
public List<ArticleResponseDto> getArticles() {
List<Article> articles = repository.findAll();
return articles.stream()
.map(ArticleResponseDto::toDto)
.toList();
public PagedResponseDto<ArticleResponseDto> getArticles(Pageable pageable) {
Page<Article> articles = repository.findAll(pageable);
return new PagedResponseDto<>(articles.map(ArticleResponseDto::toDto));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import com.stempo.api.domain.presentation.dto.request.BoardRequestDto;
import com.stempo.api.domain.presentation.dto.request.BoardUpdateRequestDto;
import com.stempo.api.domain.presentation.dto.response.BoardResponseDto;

import java.util.List;
import com.stempo.api.global.common.dto.PagedResponseDto;
import org.springframework.data.domain.Pageable;

public interface BoardService {

Long registerBoard(BoardRequestDto requestDto);

List<BoardResponseDto> getBoardsByCategory(BoardCategory category);
PagedResponseDto<BoardResponseDto> getBoardsByCategory(BoardCategory category, Pageable pageable);

Long updateBoard(Long boardId, BoardUpdateRequestDto requestDto);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import com.stempo.api.domain.presentation.dto.request.BoardRequestDto;
import com.stempo.api.domain.presentation.dto.request.BoardUpdateRequestDto;
import com.stempo.api.domain.presentation.dto.response.BoardResponseDto;
import com.stempo.api.global.common.dto.PagedResponseDto;
import com.stempo.api.global.exception.PermissionDeniedException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class BoardServiceImpl implements BoardService {
Expand All @@ -29,12 +30,10 @@ public Long registerBoard(BoardRequestDto requestDto) {
}

@Override
public List<BoardResponseDto> getBoardsByCategory(BoardCategory category) {
public PagedResponseDto<BoardResponseDto> getBoardsByCategory(BoardCategory category, Pageable pageable) {
validateAccessPermissionForSuggestion(category);
List<Board> boards = repository.findByCategory(category);
return boards.stream()
.map(BoardResponseDto::toDto)
.toList();
Page<Board> boards = repository.findByCategory(category, pageable);
return new PagedResponseDto<>(boards.map(BoardResponseDto::toDto));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.stempo.api.domain.application.service;

import com.stempo.api.domain.presentation.dto.response.UserAchievementResponseDto;

import java.util.List;
import com.stempo.api.global.common.dto.PagedResponseDto;
import org.springframework.data.domain.Pageable;

public interface UserAchievementService {

Long registerUserAchievement(Long achievementId);

List<UserAchievementResponseDto> getUserAchievements();
PagedResponseDto<UserAchievementResponseDto> getUserAchievements(Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
import com.stempo.api.domain.domain.model.UserAchievement;
import com.stempo.api.domain.domain.repository.AchievementRepository;
import com.stempo.api.domain.domain.repository.UserAchievementRepository;
import com.stempo.api.domain.persistence.entity.UserAchievementEntity;
import com.stempo.api.domain.presentation.dto.response.UserAchievementResponseDto;
import com.stempo.api.global.common.dto.PagedResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

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

@Service
Expand All @@ -23,7 +27,8 @@ public class UserAchievementServiceImpl implements UserAchievementService {
@Override
public Long registerUserAchievement(Long achievementId) {
String deviceTag = userService.getCurrentDeviceTag();
Optional<UserAchievementEntity> existingUserAchievement = userAchievementRepository.findByDeviceTagAndAchievementId(deviceTag, achievementId);
Optional<UserAchievement> existingUserAchievement =
userAchievementRepository.findByDeviceTagAndAchievementId(deviceTag, achievementId);

if (existingUserAchievement.isPresent()) {
return existingUserAchievement.get().getId();
Expand All @@ -34,16 +39,20 @@ public Long registerUserAchievement(Long achievementId) {
}

@Override
public List<UserAchievementResponseDto> getUserAchievements() {
public PagedResponseDto<UserAchievementResponseDto> getUserAchievements(Pageable pageable) {
String deviceTag = userService.getCurrentDeviceTag();
List<UserAchievementEntity> userAchievements = userAchievementRepository.findByDeviceTag(deviceTag);
return userAchievements.stream()
.map(this::getUserAchievementResponseDto)
.toList();
Page<Achievement> achievements = achievementRepository.findAll(pageable);
List<UserAchievementResponseDto> userAchievementDtos = getUserAchievementResponseDtos(achievements, deviceTag);
return new PagedResponseDto<>(new PageImpl<>(userAchievementDtos, pageable, userAchievementDtos.size()));
}

private UserAchievementResponseDto getUserAchievementResponseDto(UserAchievementEntity ua) {
Achievement achievement = achievementRepository.findByIdOrThrow(ua.getAchievementId());
return UserAchievementResponseDto.toDto(achievement);
private List<UserAchievementResponseDto> getUserAchievementResponseDtos(Page<Achievement> achievements, String deviceTag) {
return achievements.stream()
.map(achievement -> userAchievementRepository.findByDeviceTagAndAchievementId(deviceTag, achievement.getId())
.map(userAchievement -> UserAchievementResponseDto.toDto(achievement, userAchievement.getCreatedAt()))
.orElse(null)
)
.filter(Objects::nonNull)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
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;
import com.stempo.api.global.common.dto.PagedResponseDto;
import org.springframework.data.domain.Pageable;

public interface VideoService {

Long registerVideo(VideoRequestDto requestDto);

List<VideoResponseDto> getVideos();
PagedResponseDto<VideoResponseDto> getVideos(Pageable pageable);

VideoDetailsResponseDto getVideo(Long videoId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
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 com.stempo.api.global.common.dto.PagedResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class VideoServiceImpl implements VideoService {
Expand All @@ -24,11 +25,9 @@ public Long registerVideo(VideoRequestDto requestDto) {
}

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

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.stempo.api.domain.domain.repository;

import com.stempo.api.domain.domain.model.Achievement;

import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface AchievementRepository {

Achievement findByIdOrThrow(Long achievementId);

List<Achievement> findAll();
Page<Achievement> findAll(Pageable pageable);

Achievement save(Achievement achievement);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.stempo.api.domain.domain.repository;

import com.stempo.api.domain.domain.model.Article;

import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface ArticleRepository {

Article save(Article article);

List<Article> findAll();
Page<Article> findAll(Pageable pageable);

Article findByIdOrThrow(Long articleId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import com.stempo.api.domain.domain.model.Board;
import com.stempo.api.domain.domain.model.BoardCategory;

import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface BoardRepository {

Board save(Board board);

List<Board> findByCategory(BoardCategory category);
Page<Board> findByCategory(BoardCategory category, Pageable pageable);

Board findByIdOrThrow(Long boardId);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.stempo.api.domain.domain.repository;

import com.stempo.api.domain.domain.model.UserAchievement;
import com.stempo.api.domain.persistence.entity.UserAchievementEntity;

import java.util.List;
import java.util.Optional;
Expand All @@ -12,9 +11,7 @@ public interface UserAchievementRepository {

void saveAll(List<UserAchievement> achievements);

List<UserAchievementEntity> findByDeviceTag(String deviceTag);

Optional<UserAchievementEntity> findByDeviceTagAndAchievementId(String deviceTag, Long achievementId);
Optional<UserAchievement> findByDeviceTagAndAchievementId(String deviceTag, Long achievementId);

List<UserAchievement> findByAchievementId(Long achievementId);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.stempo.api.domain.domain.repository;

import com.stempo.api.domain.domain.model.Video;

import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface VideoRepository {

Video save(Video video);

List<Video> findAll();
Page<Video> findAll(Pageable pageable);

Video findByIdOrThrow(Long videoId);
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package com.stempo.api.domain.persistence.repository;

import com.stempo.api.domain.persistence.entity.AchievementEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

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

public interface AchievementJpaRepository extends JpaRepository<AchievementEntity, Long> {

@Query("SELECT a " +
"FROM AchievementEntity a " +
"WHERE a.deleted = false " +
"ORDER BY a.createdAt ASC")
List<AchievementEntity> findAllActiveAchievements();
"WHERE a.deleted = false")
Page<AchievementEntity> findAllActiveAchievements(Pageable pageable);

@Query("SELECT a " +
"FROM AchievementEntity a " +
Expand Down
Loading