Skip to content

Commit

Permalink
Merge pull request #78 from Team-Growthook/fix/#71-get-cave-api
Browse files Browse the repository at this point in the history
[FIX] 동굴 이미지 인덱스도 내려주도록 수정
  • Loading branch information
yeseul106 authored Jan 16, 2024
2 parents 2d51ad9 + ddd97a6 commit 60c8dc6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public class Cave extends BaseTimeEntity {

private String introduction;

@Column(name = "image_index")
private Integer imageIndex;

@Column(name = "is_shared")
private Boolean isShared;

Expand All @@ -37,11 +40,12 @@ public class Cave extends BaseTimeEntity {
private List<Seed> seeds = new ArrayList<>();

@Builder
public Cave(String name, String introduction, Boolean isShared, Member member) {
public Cave(String name, String introduction, Boolean isShared, Member member, Integer imageIndex) {
this.name = name;
this.introduction = introduction;
this.isShared = isShared;
this.member = member;
this.imageIndex = imageIndex;
}

public void updateCave(String newName, String newIntroduction, Boolean newIsShared) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
public class CaveAllResponseDto {
private Long caveId;
private String caveName;
private Integer caveImageIndex;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
@AllArgsConstructor(staticName = "of")
public class CaveCreateResponseDto {
private Long caveId;
private Integer caveImageIndex;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public interface CaveRepository extends JpaRepository<Cave, Long> {

Optional<Cave> findCaveById(Long caveId);

Optional<Cave> findTopByMemberIdOrderByIdDesc(Long memberId);

default Cave findCaveByIdOrThrow(Long caveId) {
return findCaveById(caveId)
.orElseThrow(() -> new NotFoundException(ErrorStatus.NOT_FOUND_CAVE.getMessage()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.example.growthookserver.api.member.repository.MemberRepository;
import com.example.growthookserver.common.exception.NotFoundException;
import com.example.growthookserver.common.response.ErrorStatus;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -25,27 +26,32 @@
public class CaveServiceImpl implements CaveService {
private final MemberRepository memberRepository;
private final CaveRepository caveRepository;
private static final int CAVE_IMAGE_TOTAL_COUNT = 4;

@Override
@Transactional
public CaveCreateResponseDto createCave(Long memberId, CaveCreateRequestDto caveCreateRequestDto){
Member member = memberRepository.findMemberByIdOrThrow(memberId);
int imageIndex = calculateImageIndex(memberId);

Cave cave = Cave.builder()
.name(caveCreateRequestDto.getName())
.introduction(caveCreateRequestDto.getIntroduction())
.isShared(caveCreateRequestDto.getIsShared())
.member(member)
.build();
.name(caveCreateRequestDto.getName())
.introduction(caveCreateRequestDto.getIntroduction())
.isShared(caveCreateRequestDto.getIsShared())
.member(member)
.imageIndex(imageIndex)
.build();

Cave savedCave = caveRepository.save(cave);
return CaveCreateResponseDto.of(savedCave.getId());
return CaveCreateResponseDto.of(savedCave.getId(), savedCave.getImageIndex());
}

@Override
public List<CaveAllResponseDto> getCaveAll(Long memberId){
List<Cave> caves = caveRepository.findAllByMemberId(memberId);

return caves.stream()
.map(cave -> CaveAllResponseDto.of(cave.getId(), cave.getName()))
.map(cave -> CaveAllResponseDto.of(cave.getId(), cave.getName(), cave.getImageIndex()))
.collect(Collectors.toList());
}

Expand All @@ -71,4 +77,10 @@ public void deleteCave(Long caveId) {
caveRepository.delete(cave);
}

private int calculateImageIndex(Long memberId) {
return caveRepository.findTopByMemberIdOrderByIdDesc(memberId)
.map(recentlyCreatedCave -> (recentlyCreatedCave.getImageIndex() + 1) % CAVE_IMAGE_TOTAL_COUNT)
.orElse(0);
}

}

0 comments on commit 60c8dc6

Please sign in to comment.