Skip to content

Commit

Permalink
Feature/#13 스택 조회 API 페이징, 본인 포함 옵션 추가, 모든 프로젝트 조회 API 스택커 resposne 수정 (
Browse files Browse the repository at this point in the history
#50)

* [#4] feat: 프로젝트 게시 API

* [#13] feat: 스택 조회 API

* [#13] style: Swagger 파라미터 dataType 변경

* Merge branch 'dev' of github.com:leeeeeyeon/artistack-server into feature/#13

* Merge branch 'dev' of github.com:leeeeeyeon/artistack-server into feature/#13

* [#13] feat: 해당 유저가 쌓은 프로젝트 정보 추가

* [#13] refactor: 사용하지 않는 메서드 제거

* [#13] chore: gitignore 파일 트랙킹하지 않도록 수정

* [#13] feat: 스택 조회 API 페이징, query string 추가

* fix: 모든 프로젝트 조회 API response 수정 및 스택 조회 API 본인 포함 여부 옵션 추가

* fix: 모든 프로젝트 조회 API 스택커 수 추가 및 스택커 정보 response 수정
  • Loading branch information
leeeeeyeon authored Aug 23, 2022
1 parent 00fb253 commit 7dce8c0
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import io.swagger.annotations.ApiOperation;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
Expand Down Expand Up @@ -143,25 +146,32 @@ public DataResponseDto<Object> getProjectLikeUsers(

/**
* 스택 조회 API - 제이
* [Get] /projects/{projectId}/prev
* [Get] /projects/{projectId}/next
* [Get] /projects/{projectId}/prev?current=true
* [Get] /projects/{projectId}/next?current=true
*/
@ApiOperation(value = "스택 조회")
@ApiImplicitParams( value = {
@ApiImplicitParam(name = "projectId", value = "현재 프로젝트 id", required = true, dataType = "long", paramType = "path"),
@ApiImplicitParam(name = "sequence", value = "순서(prev or next)", required = true, dataType = "string", paramType = "path")})
@GetMapping("/{projectId}/{sequence}")
public DataResponseDto<Object> getStack(@PathVariable Long projectId, @PathVariable String sequence) {
public DataResponseDto<Object> getStack(
@PageableDefault(size = 6) Pageable pageable,
@PathVariable Long projectId, @PathVariable String sequence,
@RequestParam(defaultValue = "true") Boolean current) {
// validation
// 1. query parameter가 next, prev를 제외한 다른 값이 들어올 경우
if (!(sequence.equals("next") || sequence.equals("prev"))) {
throw new GeneralException(Code.INVALID_SEQUENCE, "sequence는 prev나 next만 사용할 수 있습니다.");
}

try {
List<UserDto> stackers = projectService.getStackers(projectId, sequence);
List<UserDto> stackers = projectService.getStackers(projectId, sequence, current);

return DataResponseDto.of(stackers);
final int start = (int) pageable.getOffset();
final int end = Math.min((start + pageable.getPageSize()), stackers.size());
final Page<UserDto> page = new PageImpl<>(stackers.subList(start, end), pageable, stackers.size());

return DataResponseDto.of(page);

} catch (Exception e) {
e.printStackTrace();
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/artistack/project/dto/ProjectDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class ProjectDto {

private List<UserDto> prevStackers;

private Integer prevStackCount;

private Integer likeCount;

private Integer stackCount;
Expand Down Expand Up @@ -116,6 +118,7 @@ public static ProjectDto projectResponse(Project project, ProjectInstrumentRepos
e -> e.findByProjectId(project.getId()).stream().map(ProjectInstrument::getInstrument)
.map(InstrumentDto::response).collect(Collectors.toList())).orElse(null))
.prevStackers(prevStackers)
.prevStackCount(prevStackers.size())
.stackCount(project.getStackCount())
.likeCount(project.getLikeCount())
.isLiked(isLiked)
Expand Down
92 changes: 67 additions & 25 deletions src/main/java/com/artistack/project/service/ProjectService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.artistack.util.SecurityUtil;
import java.io.IOException;
import java.util.ArrayList;

import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -55,7 +56,7 @@ public Page<ProjectDto> getByConditionWithPaging(Pageable pageable, Optional<Str
return projectRepository.getByConditionWithPaging(pageable, artistackId.orElse(null),
lastId.orElse(null), Scope.PUBLIC)
.map(project -> ProjectDto.projectResponse(project, projectInstrumentRepository, projectLikeRepository,
userRepository, getPrevStackers(project.getId()))
userRepository, getPrevStackers(project.getId(), false, true))
);
}

Expand Down Expand Up @@ -204,34 +205,53 @@ public String insertProject(Long prevProjectId, MultipartFile video, ProjectDto
}

// 스택 조회 - 다음 스택인지, 이전 스택인지 선택
public List<UserDto> getStackers(Long projectId, String sequence) {
public List<UserDto> getStackers(Long projectId, String sequence, Boolean current) {
if (sequence.equals("prev")) {
return getPrevStackers(projectId);
return getPrevStackers(projectId, current, false);
} else {
return getNextStackers(projectId);
return getNextStackers(projectId, current);
}
}

// 이전에 스택을 쌓은 유저 목록(이전 스택) 조회
public List<UserDto> getPrevStackers(Long projectId) {
// while문을 이용하여 prevProjectId = 0(최초 프로젝트)이 될 때까지 스택 조회
public List<UserDto> getPrevStackers(Long projectId, Boolean current, Boolean search) {
ArrayList<UserDto> stackers = new ArrayList<>();

if (search) {
Long prevProjectId = projectRepository.findById(projectId)
.orElseThrow(() -> new GeneralException(Code.PROJECT_NOT_FOUND, "프로젝트를 찾을 수 없습니다."))
.getPrevProjectId();

// while문을 이용하여 prevProjectId = 0(최초 프로젝트)이 될 때까지 스택 조회
while (prevProjectId != 0) {
Project project = projectRepository.findById(prevProjectId)
.orElseThrow(() -> new GeneralException(Code.PROJECT_NOT_FOUND, "프로젝트를 찾을 수 없습니다."));

UserDto userDto = getSearchStackResponse(project.getId());
stackers.add(userDto);

prevProjectId = project.getPrevProjectId();
}

return stackers;
}

// 현재 프로젝트 유저 정보 추가
if (current) {
UserDto userDto = getStackResponse(projectId);
stackers.add(userDto);
}

Long prevProjectId = projectRepository.findById(projectId)
.orElseThrow(() -> new GeneralException(Code.PROJECT_NOT_FOUND, "프로젝트를 찾을 수 없습니다."))
.getPrevProjectId();
.getPrevProjectId();

// while문을 이용하여 prevProjectId = 0(최초 프로젝트)이 될 때까지 스택 조회
while (prevProjectId != 0) {
Project project = projectRepository.findById(prevProjectId)
.orElseThrow(() -> new GeneralException(Code.PROJECT_NOT_FOUND, "프로젝트를 찾을 수 없습니다."));

User user = project.getUser();

List<InstrumentDto> instruments = getInstrumentDtoFromProject(project);

ProjectDto projectDto = ProjectDto.stackResponse(project);

UserDto userDto = UserDto.stackResponse(user, instruments, projectDto);
UserDto userDto = getStackResponse(project.getId());
stackers.add(userDto);

prevProjectId = project.getPrevProjectId();
Expand All @@ -241,24 +261,20 @@ public List<UserDto> getPrevStackers(Long projectId) {
}

// 현재 스택 위에 스택을 쌓은 유저 목록(다음 스택) 조회
public List<UserDto> getNextStackers(Long projectId) {
public List<UserDto> getNextStackers(Long projectId, Boolean current) {
ArrayList<UserDto> stackers = new ArrayList<>();

if (current) {
UserDto userDto = getStackResponse(projectId);
stackers.add(userDto);
}

// prevProjectId가 projectId인 자식 노드들 반환
// 1. prevProjectId가 projectId인 프로젝트들을 찾아
List<Project> projects = projectRepository.findAllByPrevProjectId(projectId);

for (Project project : projects) {
Long userId = project.getUser().getId();

List<InstrumentDto> instruments = getInstrumentDtoFromProject(project);

User user = userRepository.findById(userId)
.orElseThrow(() -> new GeneralException(Code.USER_NOT_FOUND, "유저를 찾을 수 없습니다."));

ProjectDto projectDto = ProjectDto.stackResponse(project);

UserDto userDto = UserDto.stackResponse(user, instruments, projectDto);
UserDto userDto = getStackResponse(project.getId());
stackers.add(userDto);
}

Expand All @@ -278,4 +294,30 @@ private List<InstrumentDto> getInstrumentDtoFromProject(Project project) {

return instruments;
}

// 스택 조회 메서드에서 중복되는 부분 모듈화
public UserDto getStackResponse(Long projectId) {
Project project = projectRepository.findById(projectId)
.orElseThrow(() -> new GeneralException(Code.PROJECT_NOT_FOUND, "프로젝트를 찾을 수 없습니다."));

User user = project.getUser();

List<InstrumentDto> instruments = getInstrumentDtoFromProject(project);

ProjectDto projectDto = ProjectDto.stackResponse(project);

return UserDto.stackResponse(user, instruments, projectDto);
}

public UserDto getSearchStackResponse(Long projectId) {
Project project = projectRepository.findById(projectId)
.orElseThrow(() -> new GeneralException(Code.PROJECT_NOT_FOUND, "프로젝트를 찾을 수 없습니다."));

User user = project.getUser();

List<InstrumentDto> instruments = getInstrumentDtoFromProject(project);

return UserDto.SearchStackResponse(user, instruments);
}

}
10 changes: 10 additions & 0 deletions src/main/java/com/artistack/user/dto/UserDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@

import com.artistack.base.GeneralException;
import com.artistack.base.constant.Code;
import com.artistack.instrument.domain.Instrument;
import com.artistack.instrument.domain.UserInstrument;
import com.artistack.instrument.dto.InstrumentDto;
import com.artistack.instrument.repository.UserInstrumentRepository;
import com.artistack.oauth.constant.ProviderType;

import com.artistack.project.domain.ProjectLike;

import com.artistack.project.dto.ProjectDto;
import com.artistack.user.constant.Role;
import com.artistack.user.domain.User;
Expand Down Expand Up @@ -96,6 +98,14 @@ public static UserDto stackResponse(User user, List<InstrumentDto> instruments,
.build();
}

public static UserDto SearchStackResponse(User user, List<InstrumentDto> instruments) {
return UserDto.builder()
.nickname(user.getNickname())
.profileImgUrl(user.getProfileImgUrl())
.instruments(instruments)
.build();
}

public static UserDto projectLikeUsersResponse(ProjectLike projectLike) {
return UserDto.builder()
.artistackId(projectLike.getUser().getArtistackId())
Expand Down

0 comments on commit 7dce8c0

Please sign in to comment.