Skip to content

Commit

Permalink
feat: 링크 목록 조회 표현, 서비스 계층에 정렬 기준 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
hseong3243 committed Jun 8, 2024
1 parent 749f93f commit a21d7a0
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

Expand All @@ -23,4 +24,10 @@ public ResponseEntity<ErrorResponse> exHandle(Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new ErrorResponse("서버 에러가 발생했습니다.", ErrorCode.SERVER_ERROR.getErrorCode()));
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ErrorResponse> validExHandle(MethodArgumentNotValidException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new ErrorResponse(e.getMessage(), ErrorCode.ILLEGAL_ARGUMENT.getErrorCode()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public ResponseEntity<FindLinksResponse> findLinks(
memberId,
request.linkBundleId(),
request.page(),
request.size()));
request.size(),
request.linkOrderBy()));
return ResponseEntity.ok(response);
}

Expand Down Expand Up @@ -85,7 +86,8 @@ public ResponseEntity<FindLinksResponse> findHubLinks(
hubId,
nullableMemberId,
request.page(),
request.size()));
request.size(),
request.linkOrderBy()));
return ResponseEntity.ok(response);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.seong.shoutlink.domain.link.link.controller.request;

import com.seong.shoutlink.domain.link.link.service.LinkOrderBy;
import jakarta.validation.constraints.NotNull;
import java.util.Objects;

public record FindLinksRequest(
@NotNull(message = "링크 묶음 ID는 필수입니다.")
Long linkBundleId,
Integer page,
Integer size) {
Integer size,
LinkOrderBy linkOrderBy) {

public FindLinksRequest(Long linkBundleId, Integer page, Integer size) {
public FindLinksRequest(Long linkBundleId, Integer page, Integer size, LinkOrderBy linkOrderBy) {
this.linkBundleId = linkBundleId;
this.page = Objects.isNull(page) ? 0 : page;
this.size = Objects.isNull(size) ? 20 : size;
this.linkOrderBy = linkOrderBy == null ? LinkOrderBy.CREATED_AT : linkOrderBy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public FindLinksResponse findLinks(FindLinksCommand command) {
linkBundle,
command.page(),
command.size(),
LinkOrderBy.CREATED_AT);
command.linkOrderBy());
return FindLinksResponse.of(result.links(), result.totalElements(), result.hasNext());
}

Expand Down Expand Up @@ -104,7 +104,7 @@ public FindLinksResponse findHubLinks(FindHubLinksCommand command) {

LinkBundle hubLinkBundle = getHubLinkBundle(command.linkBundleId(), hub);
LinkPaginationResult result = linkRepository.findLinks(hubLinkBundle, command.page(),
command.size(), LinkOrderBy.CREATED_AT);
command.size(), command.linkOrderBy());
return FindLinksResponse.of(result.links(), result.totalElements(), result.hasNext());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.seong.shoutlink.domain.link.link.service.request;

import com.seong.shoutlink.domain.link.link.service.LinkOrderBy;
import jakarta.annotation.Nullable;

public record FindHubLinksCommand(
Expand All @@ -8,6 +9,7 @@ public record FindHubLinksCommand(
@Nullable
Long nullableMemberId,
int page,
int size) {
int size,
LinkOrderBy linkOrderBy) {

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.seong.shoutlink.domain.link.link.service.request;

public record FindLinksCommand(Long memberId, Long linkBundleId, int page, int size) {
import com.seong.shoutlink.domain.link.link.service.LinkOrderBy;

public record FindLinksCommand(Long memberId, Long linkBundleId, int page, int size,
LinkOrderBy linkOrderBy) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ void findLinks() throws Exception {
params.add("linkBundleId", "1");
params.add("page", "0");
params.add("size", "10");
params.add("linkOrderBy", "CREATED_AT");
FindLinkResponse findLinkResponse = new FindLinkResponse(1L, "url", "간단한 설명",
LinkFixture.DEFAULT_EXPIRED_AT);
FindLinksResponse response = new FindLinksResponse(List.of(findLinkResponse), 1,
Expand All @@ -99,7 +100,8 @@ void findLinks() throws Exception {
queryParameters(
parameterWithName("linkBundleId").description("링크 묶음 ID"),
parameterWithName("page").description("페이지"),
parameterWithName("size").description("사이즈")
parameterWithName("size").description("사이즈"),
parameterWithName("linkOrderBy").description("정렬 기준")
),
responseFields(
fieldWithPath("links").type(JsonFieldType.ARRAY).description("링크 목록"),
Expand Down Expand Up @@ -185,7 +187,8 @@ void findHubLinks() throws Exception {
queryParameters(
parameterWithName("linkBundleId").description("링크 묶음 ID"),
parameterWithName("page").description("페이지").optional(),
parameterWithName("size").description("사이즈").optional()
parameterWithName("size").description("사이즈").optional(),
parameterWithName("linkOrderBy").description("정렬 기준").optional()
),
responseFields(
fieldWithPath("links").type(JsonFieldType.ARRAY).description("허브 링크 목록"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.seong.shoutlink.domain.hub.Hub;
import com.seong.shoutlink.domain.hubmember.repository.StubHubMemberRepository;
import com.seong.shoutlink.domain.link.link.Link;
import com.seong.shoutlink.domain.link.link.service.LinkOrderBy;
import com.seong.shoutlink.domain.link.link.service.LinkService;
import com.seong.shoutlink.domain.link.repository.StubLinkRepository;
import com.seong.shoutlink.domain.link.link.service.request.CreateHubLinkCommand;
Expand Down Expand Up @@ -125,7 +126,8 @@ void findLinks() {
member.getMemberId(),
linkBundle.getLinkBundleId(),
0,
20);
20,
LinkOrderBy.CREATED_AT);

//when
FindLinksResponse response = linkService.findLinks(command);
Expand Down Expand Up @@ -295,7 +297,7 @@ void findHubLinks() {
linkRepository.stub(link);

FindHubLinksCommand command = new FindHubLinksCommand(linkBundle.getLinkBundleId(),
hub.getHubId(), null, 0, 10);
hub.getHubId(), null, 0, 10, LinkOrderBy.CREATED_AT);

//when
FindLinksResponse response = linkService.findHubLinks(command);
Expand All @@ -315,7 +317,7 @@ void findHubLinks() {
@DisplayName("예외(NotFound): 존재하지 않는 허브")
void notFound_WhenHubNotFound() {
//given
FindHubLinksCommand command = new FindHubLinksCommand(1L, 1L, null, 0, 10);
FindHubLinksCommand command = new FindHubLinksCommand(1L, 1L, null, 0, 10, LinkOrderBy.CREATED_AT);

//when
Exception exception = catchException(() -> linkService.findHubLinks(command));
Expand All @@ -333,7 +335,7 @@ void notFound_WhenLinkBundleNotFound() {
memberRepository.stub(member);
hubRepository.stub(hub);

FindHubLinksCommand command = new FindHubLinksCommand(1L, hub.getHubId(), null, 0, 10);
FindHubLinksCommand command = new FindHubLinksCommand(1L, hub.getHubId(), null, 0, 10, LinkOrderBy.CREATED_AT);

//when
Exception exception = catchException(() -> linkService.findHubLinks(command));
Expand All @@ -360,7 +362,7 @@ void findHubLinks_WhenMemberIsHubMember() {
linkRepository.stub(link);

FindHubLinksCommand command = new FindHubLinksCommand(linkBundle.getLinkBundleId(),
privateHub.getHubId(), member.getMemberId(), 0, 10);
privateHub.getHubId(), member.getMemberId(), 0, 10, LinkOrderBy.CREATED_AT);

//when
FindLinksResponse response = linkService.findHubLinks(command);
Expand All @@ -387,7 +389,7 @@ void unauthenticated_WhenMemberIsUnauthenticated() {
linkRepository.stub(link);

FindHubLinksCommand command = new FindHubLinksCommand(linkBundle.getLinkBundleId(),
hub.getHubId(), null, 0, 10);
hub.getHubId(), null, 0, 10, LinkOrderBy.CREATED_AT);

//when
Exception exception = catchException(() -> linkService.findHubLinks(command));
Expand All @@ -413,7 +415,7 @@ void unauthorized_WhenMemberIsNotHubMember() {
"asdf123!", "nickname", MemberRole.ROLE_USER);
memberRepository.stub(anotherMember);
FindHubLinksCommand command = new FindHubLinksCommand(linkBundle.getLinkBundleId(),
hub.getHubId(), anotherMember.getMemberId(), 0, 10);
hub.getHubId(), anotherMember.getMemberId(), 0, 10, LinkOrderBy.CREATED_AT);

//when
Exception exception = catchException(() -> linkService.findHubLinks(command));
Expand Down

0 comments on commit a21d7a0

Please sign in to comment.