diff --git a/growthookServer/src/main/java/com/example/growthookserver/api/cave/domain/Cave.java b/growthookServer/src/main/java/com/example/growthookserver/api/cave/domain/Cave.java index 86e1252..288e3f7 100644 --- a/growthookServer/src/main/java/com/example/growthookserver/api/cave/domain/Cave.java +++ b/growthookServer/src/main/java/com/example/growthookserver/api/cave/domain/Cave.java @@ -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; @@ -37,11 +40,12 @@ public class Cave extends BaseTimeEntity { private List 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) { diff --git a/growthookServer/src/main/java/com/example/growthookserver/api/cave/dto/response/CaveAllResponseDto.java b/growthookServer/src/main/java/com/example/growthookserver/api/cave/dto/response/CaveAllResponseDto.java index 4cedc37..6c5b5d6 100644 --- a/growthookServer/src/main/java/com/example/growthookserver/api/cave/dto/response/CaveAllResponseDto.java +++ b/growthookServer/src/main/java/com/example/growthookserver/api/cave/dto/response/CaveAllResponseDto.java @@ -11,4 +11,5 @@ public class CaveAllResponseDto { private Long caveId; private String caveName; + private Integer caveImageIndex; } diff --git a/growthookServer/src/main/java/com/example/growthookserver/api/cave/dto/response/CaveCreateResponseDto.java b/growthookServer/src/main/java/com/example/growthookserver/api/cave/dto/response/CaveCreateResponseDto.java index 96d7fa5..9b0125b 100644 --- a/growthookServer/src/main/java/com/example/growthookserver/api/cave/dto/response/CaveCreateResponseDto.java +++ b/growthookServer/src/main/java/com/example/growthookserver/api/cave/dto/response/CaveCreateResponseDto.java @@ -10,4 +10,5 @@ @AllArgsConstructor(staticName = "of") public class CaveCreateResponseDto { private Long caveId; + private Integer caveImageIndex; } diff --git a/growthookServer/src/main/java/com/example/growthookserver/api/cave/repository/CaveRepository.java b/growthookServer/src/main/java/com/example/growthookserver/api/cave/repository/CaveRepository.java index 25789b5..340add2 100644 --- a/growthookServer/src/main/java/com/example/growthookserver/api/cave/repository/CaveRepository.java +++ b/growthookServer/src/main/java/com/example/growthookserver/api/cave/repository/CaveRepository.java @@ -13,6 +13,8 @@ public interface CaveRepository extends JpaRepository { Optional findCaveById(Long caveId); + Optional findTopByMemberIdOrderByIdDesc(Long memberId); + default Cave findCaveByIdOrThrow(Long caveId) { return findCaveById(caveId) .orElseThrow(() -> new NotFoundException(ErrorStatus.NOT_FOUND_CAVE.getMessage())); diff --git a/growthookServer/src/main/java/com/example/growthookserver/api/cave/service/Impl/CaveServiceImpl.java b/growthookServer/src/main/java/com/example/growthookserver/api/cave/service/Impl/CaveServiceImpl.java index 1cdb5d4..c6b7568 100644 --- a/growthookServer/src/main/java/com/example/growthookserver/api/cave/service/Impl/CaveServiceImpl.java +++ b/growthookServer/src/main/java/com/example/growthookserver/api/cave/service/Impl/CaveServiceImpl.java @@ -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; @@ -25,19 +26,24 @@ 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 @@ -45,7 +51,7 @@ public List getCaveAll(Long memberId){ List 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()); } @@ -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); + } + }