Skip to content

Commit

Permalink
Merge pull request #489 from tukcomCD2024/refact/common_dto-B-#479
Browse files Browse the repository at this point in the history
feat : 그룹 캡슐 목록 조회 #476
  • Loading branch information
GaBaljaintheroom authored Jun 12, 2024
2 parents c5e4f9d + c8944f8 commit 752cbd3
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,20 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.time.ZonedDateTime;
import org.hibernate.validator.constraints.Range;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.response.GroupMemberCapsuleOpenStatusListResponse;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.reqeust.GroupCapsuleCreateRequest;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.reqeust.GroupCapsuleUpdateRequest;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.response.GroupCapsuleDetailResponse;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.response.GroupCapsuleOpenStateResponse;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.response.GroupCapsulePageResponse;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.response.GroupCapsuleSliceResponse;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.response.GroupCapsuleSummaryResponse;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.response.GroupMemberCapsuleOpenStatusListResponse;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.response.MyGroupCapsuleSliceResponse;
import site.timecapsulearchive.core.global.common.response.ApiSpec;
import site.timecapsulearchive.core.global.error.ErrorResponse;
Expand Down Expand Up @@ -115,21 +112,24 @@ ResponseEntity<ApiSpec<GroupCapsuleSummaryResponse>> getGroupCapsuleSummaryByCap
@ApiResponse(
responseCode = "200",
description = "처리 완료"
),
@ApiResponse(
responseCode = "403",
description = "그룹에 대한 권한이 없는 경우 발생한다",
content = @Content(schema = @Schema(implementation = ErrorResponse.class))
)
})
@GetMapping(
value = "/groups/{group_id}/capsules",
produces = {"application/json"}
)
ResponseEntity<GroupCapsulePageResponse> getGroupCapsules(
@Parameter(in = ParameterIn.PATH, description = "그룹 아이디", required = true, schema = @Schema())
@PathVariable("group_id") Long groupId,
ResponseEntity<ApiSpec<GroupCapsuleSliceResponse>> getGroupCapsules(
Long memberId,

@Parameter(in = ParameterIn.QUERY, description = "페이지 크기", required = true, schema = @Schema())
@NotNull @Valid @RequestParam(value = "size") Long size,
@Parameter(in = ParameterIn.QUERY, description = "그룹 아이디", required = true)
Long groupId,

@Parameter(in = ParameterIn.QUERY, description = "페이지 크기", required = true)
int size,

@Parameter(in = ParameterIn.QUERY, description = "마지막 캡슐 아이디", required = true, schema = @Schema())
@NotNull @Valid @RequestParam(value = "capsule_id") Long capsuleId
@Parameter(in = ParameterIn.QUERY, description = "마지막 캡슐 아이디", required = true)
Long lastCapsuleId
);

@Operation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
import site.timecapsulearchive.core.domain.capsule.data.dto.CapsuleBasicInfoDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleDetailDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleOpenStateDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleSliceRequestDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleSummaryDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupMemberCapsuleOpenStatusDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.reqeust.GroupCapsuleCreateRequest;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.reqeust.GroupCapsuleUpdateRequest;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.response.GroupCapsuleDetailResponse;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.response.GroupCapsuleOpenStateResponse;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.response.GroupCapsulePageResponse;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.response.GroupCapsuleSliceResponse;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.response.GroupCapsuleSummaryResponse;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.response.GroupMemberCapsuleOpenStatusListResponse;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.response.MyGroupCapsuleSliceResponse;
Expand Down Expand Up @@ -111,10 +112,33 @@ public ResponseEntity<ApiSpec<GroupCapsuleSummaryResponse>> getGroupCapsuleSumma
);
}

@GetMapping(
produces = {"application/json"}
)
@Override
public ResponseEntity<GroupCapsulePageResponse> getGroupCapsules(Long groupId, Long size,
Long capsuleId) {
return null;
public ResponseEntity<ApiSpec<GroupCapsuleSliceResponse>> getGroupCapsules(
@AuthenticationPrincipal final Long memberId,
@RequestParam("group_id") final Long groupId,
@RequestParam("size") final int size,
@RequestParam(value = "last_capsule_id", required = false) final Long lastCapsuleId
) {
final GroupCapsuleSliceRequestDto dto = GroupCapsuleSliceRequestDto.createOf(memberId,
groupId,
size, lastCapsuleId);

final Slice<CapsuleBasicInfoDto> groupCapsuleSlice = groupCapsuleService.findGroupCapsuleSlice(
dto);

return ResponseEntity.ok(
ApiSpec.success(
SuccessCode.SUCCESS,
GroupCapsuleSliceResponse.create(
groupCapsuleSlice.getContent(),
groupCapsuleSlice.hasNext(),
s3PreSignedUrlManager::getS3PreSignedUrlForGet
)
)
);
}

@GetMapping(value = "/my", produces = {"application/json"})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto;

public record GroupCapsuleSliceRequestDto(
Long memberId,
Long groupId,
int size,
Long lastCapsuleId
) {

public static GroupCapsuleSliceRequestDto createOf(
final Long memberId,
final Long groupId,
final int size,
final Long lastCapsuleId
) {
return new GroupCapsuleSliceRequestDto(memberId, groupId, size, lastCapsuleId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package site.timecapsulearchive.core.domain.capsule.group_capsule.data.response;

import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
import java.util.function.UnaryOperator;
import site.timecapsulearchive.core.domain.capsule.data.dto.CapsuleBasicInfoDto;
import site.timecapsulearchive.core.domain.capsule.data.response.CapsuleBasicInfoResponse;

@Schema(description = "그룹 캡슐 목록 응답")
public record GroupCapsuleSliceResponse(

@Schema(description = "캡슐 기본 정보 목록")
List<CapsuleBasicInfoResponse> capsuleBasicInfos,

@Schema(description = "다음 페이지 유무")
boolean hasNext
) {

public static GroupCapsuleSliceResponse create(
final List<CapsuleBasicInfoDto> dtos,
final boolean hasNext,
final UnaryOperator<String> preSignUrlFunction
) {
List<CapsuleBasicInfoResponse> capsuleBasicInfoResponses = dtos.stream()
.map(dto -> dto.toResponse(preSignUrlFunction))
.toList();

return new GroupCapsuleSliceResponse(capsuleBasicInfoResponses, hasNext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static site.timecapsulearchive.core.domain.member.entity.QMember.member;

import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.StringExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
Expand All @@ -24,6 +25,7 @@
import site.timecapsulearchive.core.domain.capsule.generic_capsule.data.dto.CapsuleDetailDto;
import site.timecapsulearchive.core.domain.capsule.generic_capsule.data.dto.CapsuleSummaryDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleDetailDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleSliceRequestDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleSummaryDto;
import site.timecapsulearchive.core.domain.group.data.dto.GroupMemberSummaryDto;
import site.timecapsulearchive.core.domain.member.entity.QMember;
Expand Down Expand Up @@ -177,4 +179,36 @@ public Long findGroupCapsuleCount(final Long groupId) {
.where(capsule.group.id.eq(groupId))
.fetchOne();
}

public Slice<CapsuleBasicInfoDto> findGroupCapsuleSlice(final GroupCapsuleSliceRequestDto dto) {
final List<CapsuleBasicInfoDto> groupCapsuleDtos = jpaQueryFactory
.select(
Projections.constructor(
CapsuleBasicInfoDto.class,
capsule.id,
capsuleSkin.imageUrl,
capsule.dueDate,
capsule.createdAt,
capsule.title,
capsule.isOpened,
capsule.type
)
)
.from(capsule)
.where(capsule.group.id.eq(dto.groupId())
.and(capsuleIdPagingCursorCondition(dto.lastCapsuleId())))
.orderBy(capsule.id.desc())
.limit(dto.size() + 1)
.fetch();

return SliceUtil.makeSlice(dto.size(), groupCapsuleDtos);
}

private BooleanExpression capsuleIdPagingCursorCondition(final Long capsuleId) {
if (capsuleId == null) {
return null;
}

return capsule.id.lt(capsuleId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
import site.timecapsulearchive.core.domain.capsule.generic_capsule.repository.capsule.CapsuleRepository;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleCreateRequestDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleDetailDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleOpenStateDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleSliceRequestDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleSummaryDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleOpenStateDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupMemberCapsuleOpenStatusDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.repository.GroupCapsuleOpenQueryRepository;
import site.timecapsulearchive.core.domain.capsule.group_capsule.repository.GroupCapsuleQueryRepository;
Expand Down Expand Up @@ -128,6 +129,16 @@ public GroupCapsuleOpenStateDto openGroupCapsule(final Long memberId, final Long
return GroupCapsuleOpenStateDto.opened();
}

public Slice<CapsuleBasicInfoDto> findGroupCapsuleSlice(final GroupCapsuleSliceRequestDto dto) {
boolean isGroupMember = memberGroupRepository.existMemberGroupByMemberIdAndGroupId(dto.memberId(),
dto.groupId());
if (!isGroupMember) {
throw new NoGroupAuthorityException();
}

return groupCapsuleQueryRepository.findGroupCapsuleSlice(dto);
}

public List<GroupMemberCapsuleOpenStatusDto> findGroupMemberCapsuleOpenStatus(
final Long memberId,
final Long capsuleId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.Field;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -214,4 +215,18 @@ public static Optional<Capsule> groupCapsuleExcludeSpecificMember(

return Optional.ofNullable(capsule);
}

public static List<Capsule> groupCapsules(
Member owner,
CapsuleSkin capsuleSkin,
Group group,
int maxCount
) {
List<Capsule> result = new ArrayList<>();
for (int count = 0; count < maxCount; count++) {
result.add(groupCapsule(owner, capsuleSkin, group));
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package site.timecapsulearchive.core.common.fixture.dto;

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import site.timecapsulearchive.core.domain.capsule.data.dto.CapsuleBasicInfoDto;
import site.timecapsulearchive.core.domain.capsule.entity.CapsuleType;

public class CapsuleBasicInfoDtoFixture {

public static List<CapsuleBasicInfoDto> capsuleBasicInfoDtos(Long capsuleId, int size) {
List<CapsuleBasicInfoDto> result = new ArrayList<>();
for (long count = capsuleId; count < size; count++) {
result.add(new CapsuleBasicInfoDto(count, count + "test-url", ZonedDateTime.now(),
ZonedDateTime.now(), count + "test-title", false, CapsuleType.GROUP));
}

return result;
}

}
Loading

0 comments on commit 752cbd3

Please sign in to comment.