Skip to content

Commit

Permalink
Merge pull request #30 from depromeet/feat/LS-23
Browse files Browse the repository at this point in the history
feat/Ls-23: 스페이스 회원 조회 API
  • Loading branch information
raymondanythings authored Jul 14, 2024
2 parents 736e0f8 + d20ca10 commit 912dca4
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 74 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ project(":layer-external") {
bootJar.enabled = false
jar.enabled = true

dependencies {
implementation project(path: ':layer-common')
implementation project(path: ':layer-domain')
dependencies {
implementation project(path: ':layer-common')
implementation project(path: ':layer-domain')

testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public interface SpaceApi {
""")
@ApiResponses({
@ApiResponse(responseCode = "200", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = SpaceResponse.SpaceWithUserCountInfo.class))
@Content(mediaType = "application/json", schema = @Schema(implementation = SpaceResponse.SpaceWithMemberCountInfo.class))
})
})
ResponseEntity<SpaceResponse.SpaceWithUserCountInfo> getSpaceById(@MemberId Long memberId, @PathVariable Long spaceId);
ResponseEntity<SpaceResponse.SpaceWithMemberCountInfo> getSpaceById(@MemberId Long memberId, @PathVariable Long spaceId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void updateSpace(@MemberId Long memberId, @RequestBody @Validated SpaceRe
}

@GetMapping("/{spaceId}")
public ResponseEntity<SpaceResponse.SpaceWithUserCountInfo> getSpaceById(@MemberId Long memberId, @PathVariable Long spaceId) {
public ResponseEntity<SpaceResponse.SpaceWithMemberCountInfo> getSpaceById(@MemberId Long memberId, @PathVariable Long spaceId) {
var foundSpace = spaceService.getSpaceById(memberId, spaceId);
return ResponseEntity.ok((foundSpace));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
import static org.layer.domain.auth.exception.TokenExceptionType.INVALID_REFRESH_TOKEN;

public class SpaceResponse {


@Builder
@Schema(description = "스페이스 정보 응답")
public record SpaceWithUserCountInfo(
public record SpaceWithMemberCountInfo(
@Schema(description = "스페이스 ID")
@NotNull
Long id,
Expand All @@ -40,13 +40,13 @@ public record SpaceWithUserCountInfo(
Long formId,

@Schema(description = "소속된 회원 수")
Long userCount
Long memberCount
) {
public static SpaceWithUserCountInfo toResponse(SpaceWithMemberCount space) {
public static SpaceWithMemberCountInfo toResponse(SpaceWithMemberCount space) {
return Optional.ofNullable(space)
.map(it -> SpaceWithUserCountInfo.builder().id(it.getId()).category(it.getCategory())
.map(it -> SpaceWithMemberCountInfo.builder().id(it.getId()).category(it.getCategory())
.field(it.getField()).name(it.getName()).introduction(it.getIntroduction())
.formId(it.getFormId()).userCount(it.getUserCount()).build())
.formId(it.getFormId()).memberCount(it.getMemberCount()).build())
.orElseThrow(() -> new BaseCustomException(INVALID_REFRESH_TOKEN));
}
}
Expand All @@ -55,13 +55,13 @@ public static SpaceWithUserCountInfo toResponse(SpaceWithMemberCount space) {
@Schema()
public record SpacePage(
@Schema()
List<SpaceWithUserCountInfo> data,
List<SpaceWithMemberCountInfo> data,

@Schema()
Meta meta
) {

public static SpacePage toResponse(List<SpaceWithUserCountInfo> spaceInfo, Meta meta) {
public static SpacePage toResponse(List<SpaceWithMemberCountInfo> spaceInfo, Meta meta) {
return SpacePage.builder().data(spaceInfo).meta(meta).build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.layer.common.dto.Meta;
import org.layer.common.exception.BaseCustomException;
import org.layer.domain.space.dto.SpaceRequest;
import org.layer.domain.space.dto.SpaceResponse;
import org.layer.domain.space.entity.MemberSpaceRelation;
import org.layer.domain.space.exception.SpaceException;
import org.layer.domain.space.repository.MemberSpaceRelationRepository;
import org.layer.domain.space.repository.SpaceRepository;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -37,7 +36,7 @@ public SpaceResponse.SpacePage getSpaceListFromMemberId(Long memberId, SpaceRequ
Long newCursor = !hasNextPage ? null : spacePages.isEmpty() ? null : spacePages.get(spacePages.size() - 1).getId();


var spaceList = spacePages.stream().map(SpaceResponse.SpaceWithUserCountInfo::toResponse).collect(Collectors.toList());
var spaceList = spacePages.stream().map(SpaceResponse.SpaceWithMemberCountInfo::toResponse).collect(Collectors.toList());

var meta = Meta.builder().cursor(newCursor).hasNextPage(hasNextPage).build();
return SpaceResponse.SpacePage.toResponse(spaceList, meta);
Expand All @@ -53,14 +52,14 @@ public void createSpace(Long memberId, SpaceRequest.CreateSpaceRequest mutateSpa

@Transactional
public void updateSpace(Long memberId, SpaceRequest.UpdateSpaceRequest updateSpaceRequest) {
spaceRepository.findByIdAndJoinedMemberId(updateSpaceRequest.id(), memberId).orElseThrow(() -> new BaseCustomException(SPACE_NOT_FOUND));
spaceRepository.findByIdAndJoinedMemberId(updateSpaceRequest.id(), memberId).orElseThrow(() -> new SpaceException(SPACE_NOT_FOUND));
spaceRepository.updateSpace(updateSpaceRequest.id(), updateSpaceRequest.category(), updateSpaceRequest.field(), updateSpaceRequest.name(), updateSpaceRequest.introduction());
}

public SpaceResponse.SpaceWithUserCountInfo getSpaceById(Long memberId, Long spaceId) {
var foundSpace = spaceRepository.findByIdAndJoinedMemberId(spaceId, memberId).orElseThrow(() -> new BaseCustomException(SPACE_NOT_FOUND));
public SpaceResponse.SpaceWithMemberCountInfo getSpaceById(Long memberId, Long spaceId) {
var foundSpace = spaceRepository.findByIdAndJoinedMemberId(spaceId, memberId).orElseThrow(() -> new SpaceException(SPACE_NOT_FOUND));

return SpaceResponse.SpaceWithUserCountInfo.toResponse(foundSpace);
return SpaceResponse.SpaceWithMemberCountInfo.toResponse(foundSpace);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public class SpaceWithMemberCount {
@NotNull
private Long leaderId;
private Long formId;
private Long userCount;
private Long memberCount;

@QueryProjection
public SpaceWithMemberCount(Long id, LocalDateTime createdAt, LocalDateTime updatedAt, SpaceCategory category, SpaceField field, String name, String introduction, Long leaderId, Long formId, Long userCount) {
public SpaceWithMemberCount(Long id, LocalDateTime createdAt, LocalDateTime updatedAt, SpaceCategory category, SpaceField field, String name, String introduction, Long leaderId, Long formId, Long memberCount) {
this.id = id;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
Expand All @@ -40,6 +40,6 @@ public SpaceWithMemberCount(Long id, LocalDateTime createdAt, LocalDateTime upda
this.introduction = introduction;
this.leaderId = leaderId;
this.formId = formId;
this.userCount = userCount;
this.memberCount = memberCount;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.layer.domain.space.repository;

import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import lombok.extern.slf4j.Slf4j;
import org.layer.domain.space.dto.QSpaceWithMemberCount;
import org.layer.domain.space.dto.SpaceWithMemberCount;
import org.layer.domain.space.entity.QMemberSpaceRelation;
import org.layer.domain.space.entity.SpaceCategory;
import org.layer.domain.space.entity.SpaceField;
import org.springframework.stereotype.Repository;
Expand All @@ -34,26 +34,7 @@ public List<SpaceWithMemberCount> findAllSpacesByMemberIdAndCategoryAndCursor(Lo
.and(cursorId == null ? null : space.id.gt(cursorId))
.and(hasCategory(category));

return queryFactory.select(
new QSpaceWithMemberCount(
space.id,
space.createdAt,
space.updatedAt,
space.category,
space.field,
space.name,
space.introduction,
space.leaderId,
space.formId,
ExpressionUtils.as(JPAExpressions.select(
memberSpaceRelation.id.count()
)
.from(memberSpaceRelation)
.where(memberSpaceRelation.space.id.eq(space.id))
, "userCount")
))
.from(space)
.join(memberSpaceRelation).on(space.id.eq(memberSpaceRelation.space.id))
return getSpaceWithMemberCountQuery()
.where(predicate)
.groupBy(space.id)
.orderBy(space.id.asc())
Expand All @@ -63,37 +44,17 @@ public List<SpaceWithMemberCount> findAllSpacesByMemberIdAndCategoryAndCursor(Lo

@Override
public Optional<SpaceWithMemberCount> findByIdAndJoinedMemberId(Long spaceId, Long memberId) {
var foundSpace = queryFactory.select(
new QSpaceWithMemberCount(space.id,
space.createdAt,
space.updatedAt,
space.category,
space.field,
space.name,
space.introduction,
space.leaderId,
space.formId,
ExpressionUtils.as(JPAExpressions.select(
memberSpaceRelation.id.count()
)
.from(memberSpaceRelation)
.where(memberSpaceRelation.space.id.eq(space.id))
, "userCount")
)
)
.from(memberSpaceRelation)
.join(space)
.on(space.id.eq(memberSpaceRelation.space.id))

var foundSpace = getSpaceWithMemberCountQuery()
.where(space.id.eq(spaceId)
.and(memberSpaceRelation.memberId.eq(memberId)))
.limit(1)
.fetchOne();

if (isSpaceWithMemberCountEmpty(foundSpace)) {
return Optional.empty();
}
// TODO: 커스텀 에러로 변경
return Optional.ofNullable(foundSpace);
return Optional.of(foundSpace);
}

@Override
Expand All @@ -109,15 +70,32 @@ public Long updateSpace(Long spaceId, SpaceCategory category, SpaceField field,
return query.where(space.id.eq(spaceId)).execute();
}

private JPAQuery<SpaceWithMemberCount> getSpaceWithMemberCountQuery() {
QMemberSpaceRelation memberCountRelationTable = new QMemberSpaceRelation("msr");
return queryFactory.select(
new QSpaceWithMemberCount(
space.id,
space.createdAt,
space.updatedAt,
space.category,
space.field,
space.name,
space.introduction,
space.leaderId,
space.formId,
memberCountRelationTable.space.id.count().as("memberCount")
))
.from(space)
.leftJoin(memberSpaceRelation).on(space.id.eq(memberSpaceRelation.space.id))
.leftJoin(memberCountRelationTable).on(space.id.eq(memberCountRelationTable.space.id));
}


private BooleanExpression hasCategory(Optional<SpaceCategory> category) {
if (category.isPresent()) {
return space.category.eq(category.get());
}
return null;
return category.map(space.category::eq).orElse(null);
}

private boolean isSpaceWithMemberCountEmpty(SpaceWithMemberCount space) {
return space.getId() == null && space.getName() == null && space.getUserCount() == 0;
return space.getId() == null && space.getName() == null && space.getMemberCount() == 0;
}
}

0 comments on commit 912dca4

Please sign in to comment.