-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from depromeet/feat/LS-19
feat: 스페이스 단건조회 / LS-13 리뷰 사항 적용
- Loading branch information
Showing
20 changed files
with
446 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
layer-api/src/main/java/org/layer/domain/space/exception/SpaceExceptionType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.layer.domain.space.exception; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.layer.common.exception.ExceptionType; | ||
import org.springframework.http.HttpStatus; | ||
|
||
import static org.springframework.http.HttpStatus.BAD_REQUEST; | ||
|
||
@RequiredArgsConstructor | ||
public enum SpaceExceptionType implements ExceptionType { | ||
|
||
/** | ||
* | ||
*/ | ||
|
||
SPACE_NOT_FOUND(BAD_REQUEST, "스페이스를 찾을 수 없어요."); | ||
|
||
private final HttpStatus status; | ||
private final String message; | ||
|
||
@Override | ||
public HttpStatus httpStatus() { | ||
return status; | ||
} | ||
|
||
@Override | ||
public String message() { | ||
return message; | ||
} | ||
} |
50 changes: 33 additions & 17 deletions
50
layer-api/src/main/java/org/layer/domain/space/service/SpaceService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,67 @@ | ||
package org.layer.domain.space.service; | ||
|
||
import jakarta.transaction.Transactional; | ||
|
||
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.dto.SpaceWithMemberCount; | ||
import org.layer.domain.space.entity.MemberSpaceRelation; | ||
import org.layer.domain.space.entity.Space; | ||
import org.layer.domain.space.repository.MemberSpaceRelationRepository; | ||
import org.layer.domain.space.repository.SpaceRepository; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
import static org.layer.domain.space.exception.SpaceExceptionType.SPACE_NOT_FOUND; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
@Transactional(readOnly = true) | ||
public class SpaceService { | ||
private final SpaceRepository spaceRepository; | ||
private final MemberSpaceRelationRepository memberSpaceRelationRepository; | ||
|
||
public SpaceResponse.SpacePage getSpaceListFromMemberId(Long memberId, SpaceRequest.GetSpaceRequest getSpaceRequest) { | ||
PageRequest pageRequest = PageRequest.of(0, getSpaceRequest.pageSize()); | ||
Page<SpaceWithMemberCount> spacePages; | ||
if (getSpaceRequest.category().isPresent()) { | ||
spacePages = spaceRepository.findAllSpacesByMemberIdAndCategoryAndCursor(memberId, getSpaceRequest.cursorId(), getSpaceRequest.category().get(), pageRequest); | ||
} else { | ||
spacePages = spaceRepository.findAllSpacesByMemberIdAndCursor(memberId, getSpaceRequest.cursorId(), pageRequest); | ||
|
||
var spacePages = spaceRepository.findAllSpacesByMemberIdAndCategoryAndCursor(memberId, getSpaceRequest.cursorId(), getSpaceRequest.category(), getSpaceRequest.pageSize()); | ||
|
||
boolean hasNextPage = spacePages.size() > getSpaceRequest.pageSize(); | ||
if (hasNextPage) { | ||
spacePages.remove(spacePages.size() - 1); | ||
} | ||
Long newCursor = !hasNextPage ? null : spacePages.isEmpty() ? null : spacePages.get(spacePages.size() - 1).getId(); | ||
|
||
|
||
var spaceList = spacePages.stream().map(SpaceResponse.SpaceWithUserCountInfo::toResponse).collect(Collectors.toList()); | ||
boolean hasNextPage = spacePages.hasNext(); | ||
Long newCursor = hasNextPage ? spacePages.getContent().get(spacePages.getNumberOfElements() - 1).getId() : null; | ||
|
||
var meta = Meta.builder().cursor(newCursor).hasNextPage(hasNextPage).build(); | ||
return SpaceResponse.SpacePage.toResponse(spaceList, meta); | ||
} | ||
|
||
@Transactional | ||
public Boolean createSpace(Long memberId, SpaceRequest.CreateSpaceRequest createSpaceRequest) { | ||
Space newSpace = createSpaceRequest.toEntity(memberId); | ||
public void createSpace(Long memberId, SpaceRequest.CreateSpaceRequest mutateSpaceRequest) { | ||
var newSpace = spaceRepository.save(mutateSpaceRequest.toEntity(memberId)); | ||
var memberSpaceRelation = MemberSpaceRelation.builder().memberId(memberId).spaceId(newSpace.getId()).build(); | ||
spaceRepository.save(newSpace); | ||
|
||
memberSpaceRelationRepository.save(memberSpaceRelation); | ||
return true; | ||
} | ||
|
||
@Transactional | ||
public void updateSpace(Long memberId, SpaceRequest.UpdateSpaceRequest updateSpaceRequest) { | ||
spaceRepository.findByIdAndJoinedMemberId(updateSpaceRequest.id(), memberId).orElseThrow(() -> new BaseCustomException(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)); | ||
|
||
return SpaceResponse.SpaceWithUserCountInfo.toResponse(foundSpace); | ||
} | ||
|
||
|
||
} | ||
|
24 changes: 24 additions & 0 deletions
24
layer-common/src/main/java/org/layer/common/annotation/AtLeastNotNull.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.layer.common.annotation; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
import org.layer.common.validator.AtLeastNotNullValidator; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Constraint(validatedBy = AtLeastNotNullValidator.class) | ||
public @interface AtLeastNotNull { | ||
String message() default "{min} 개 이상의 값이 필요해요."; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
|
||
int min() default 1; | ||
|
||
} |
Oops, something went wrong.