Skip to content

Commit

Permalink
feat: 불필요한 wrapping 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
versatile0010 committed Mar 27, 2024
1 parent 9e30366 commit 90bd052
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public ResponseEntity<GetArticleDetailResponse> getArticleDetails(@PathVariable(
tag 가 null 이면, tag 상관 없이 전체 조회를 수행합니다.
tag 가 null 이 아니면, 해당 tag 에 해당하는 게시글만 조회합니다.
lastArticleId 는 직전에 조회한 게시글 중 가장 먀지막(작은) articleId 를 의미합니다.
lastArticleId 는 직전에 조회한 게시글 중 가장 마지막(작은) articleId 를 의미합니다.
- 첫 페이지를 요청할 경우에는 lastArticleId 를 null 로 보내야합니다.
- 첫 페이지 이후에 대한 요청은, 직전 페이지 요청에서 얻어온 lastArticleId 를 넣어서 보내면 그 다음 페이지를 호출합니다.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.numberone.backend.domain.notification.entity.NotificationTag;
import com.numberone.backend.domain.notification.repository.NotificationRepository;
import com.numberone.backend.domain.notificationregion.entity.NotificationRegion;
import com.numberone.backend.domain.notificationregion.repository.NotificationRegionRepository;
import com.numberone.backend.exception.conflict.UnauthorizedLocationException;
import com.numberone.backend.exception.notfound.NotFoundArticleException;
import com.numberone.backend.exception.notfound.NotFoundMemberException;
Expand Down Expand Up @@ -51,20 +50,24 @@
@Service
public class ArticleService {

// todo: 리팩토링 (db 정리하고, 불필요한 쿼리 + 비효율적인 코드 모두 제거 )


private final ArticleRepository articleRepository;
private final MemberRepository memberRepository;
private final ArticleParticipantRepository articleParticipantRepository;
private final ArticleImageRepository articleImageRepository;
private final CommentRepository commentRepository;
private final ArticleLikeRepository articleLikeRepository;
private final NotificationRegionRepository notificationRegionRepository;

private final NotificationRepository notificationRepository;
private final S3Provider s3Provider;
private final LocationProvider locationProvider;
private final FcmMessageProvider fcmMessageProvider;

@Transactional
public UploadArticleResponse uploadArticle(UploadArticleRequest request) {
// todo: 리팩토링
long id = SecurityContextProvider.getAuthenticatedUserId();
Member owner = memberRepository.findById(id)
.orElseThrow(NotFoundMemberException::new);
Expand All @@ -82,7 +85,7 @@ public UploadArticleResponse uploadArticle(UploadArticleRequest request) {
new ArticleParticipant(article, owner)
);

// 2. 이미지 업로드
// 2. 이미지 업로드 todo: 비동기 업로드
List<ArticleImage> articleImages = new ArrayList<>();
List<String> imageUrls = new ArrayList<>();
String thumbNailImageUrl = "";
Expand Down Expand Up @@ -185,16 +188,19 @@ public Slice<GetArticleListResponse> getArticleListPaging(ArticleSearchParameter
List<Long> memberLikedArticleIdList = articleLikeRepository.findByMember(member)
.stream().map(ArticleLike::getArticleId)
.toList();
return new SliceImpl<>(
articleRepository.getArticlesNoOffSetPaging(param, pageable)
.stream()
.peek(article -> {
updateArticleInfo(article, memberLikedArticleIdList);
})
.toList());

Slice<GetArticleListResponse> slices = articleRepository.getArticlesNoOffSetPaging(param, pageable);
List<GetArticleListResponse> content = slices.getContent().stream()
.peek(article -> {
updateArticleInfo(article, memberLikedArticleIdList);
})
.toList();

// todo: 리팩토링 ( slices 크기가 n 일 때, n * (3) 개의 쿼리가 발생하고 있음.)
return new SliceImpl<>(content, pageable, slices.hasNext());
}

public void updateArticleInfo(GetArticleListResponse articleInfo, List<Long> memberLikedArticleIdList) {
private void updateArticleInfo(GetArticleListResponse articleInfo, List<Long> memberLikedArticleIdList) {
Long ownerId = articleInfo.getOwnerId();
Long thumbNailImageUrlId = articleInfo.getThumbNailImageId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,25 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class GetArticleListResponse {
public class GetArticleListResponse { // todo: record

private ArticleTag tag;
private Long id;
private String title;
private String content;
private String address;
@Setter
private String ownerNickName;
private Long ownerId;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd hh:mm", timezone = "Asia/Seoul")
private LocalDateTime createdAt;
private ArticleStatus articleStatus;
@Setter
private String thumbNailImageUrl;
private Long thumbNailImageId;

private Integer articleLikeCount;
@Setter
private Long commentCount;
private Boolean isLiked;

Expand Down Expand Up @@ -63,18 +66,8 @@ public GetArticleListResponse(Article article, Long ownerId, Long thumbNailImage
}
}

public void setOwnerNickName(String nickName){
this.ownerNickName = nickName;
}

public void setThumbNailImageUrl(String thumbNailImageUrl){
this.thumbNailImageUrl = thumbNailImageUrl;
}

public void setCommentCount(Long commentCount){
this.commentCount = commentCount;
}

// todo: 파라미터로 optional 을 받지말자.
public void updateInfo(Optional<Member> owner,
Optional<ArticleImage> articleImage,
List<Long> memberLikedArticleIdList,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public class ArticleRepositoryCustomImpl implements ArticleRepositoryCustom {

@Override
public Slice<GetArticleListResponse> getArticlesNoOffSetPaging(ArticleSearchParameter param, Pageable pageable) {
List<GetArticleListResponse> result = queryFactory.select(new QGetArticleListResponse(
List<GetArticleListResponse> result = queryFactory.select(
new QGetArticleListResponse(
article,
article.articleOwnerId,
article.thumbNailImageUrlId
Expand All @@ -36,10 +37,11 @@ public Slice<GetArticleListResponse> getArticlesNoOffSetPaging(ArticleSearchPara
article.articleStatus.eq(ArticleStatus.ACTIVATED),
article.lv2.eq(param.getRegionLv2())
)
.orderBy(article.id.desc())
.limit(pageable.getPageSize() + 1)
.orderBy(article.id.desc())
.fetch();
return checkLastPage(pageable, result);

return new SliceImpl<>(result, pageable, checkLastPage(pageable, result));
}

private BooleanExpression checkTagCondition(ArticleTag tag) {
Expand All @@ -56,15 +58,15 @@ private BooleanExpression ltArticleId(Long articleId) {
return article.id.lt(articleId);
}

private Slice<GetArticleListResponse> checkLastPage(Pageable pageable, List<GetArticleListResponse> result) {
private boolean checkLastPage(Pageable pageable, List<GetArticleListResponse> result) {
boolean hasNext = false;

if (result.size() > pageable.getPageSize()) {
hasNext = true;
result.remove(pageable.getPageSize());
}

return new SliceImpl<>(result, pageable, hasNext);
return hasNext;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Slice<NotificationTabResponse> getNotificationTabPagesNoOffSetByMember(No
.orderBy(notificationEntity.id.desc())
.limit(pageable.getPageSize() + 1)
.fetch();
return checkLastPage(pageable, result);
return new SliceImpl<>(result, pageable, checkLastPage(pageable, result));
}

private BooleanExpression checkDisasterFlag(boolean isDisaster) {
Expand All @@ -53,13 +53,14 @@ private BooleanExpression ltNotificationEntityId(Long notificationId) {
return notificationEntity.id.lt(notificationId);
}

private Slice<NotificationTabResponse> checkLastPage(Pageable pageable, List<NotificationTabResponse> result) {
private boolean checkLastPage(Pageable pageable, List<NotificationTabResponse> result) {
boolean hasNext = false;

if (result.size() > pageable.getPageSize()) {
hasNext = true;
result.remove(pageable.getPageSize());
}
return new SliceImpl<>(result, pageable, hasNext);

return hasNext;
}
}

0 comments on commit 90bd052

Please sign in to comment.