Skip to content

Commit

Permalink
[fix] @where을 통해 select 쿼리 조건 추가 및 고아 객체 설정 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
tlarbals824 committed May 24, 2023
1 parent 40d12d1 commit e6873d1
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import com.kusitms.samsion.common.querydsl.OrderByNull;
import com.kusitms.samsion.domain.album.domain.entity.*;
import com.kusitms.samsion.domain.album.domain.repository.AlbumRepositoryCustom;
import com.kusitms.samsion.domain.comment.domain.entity.QComment;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.JPAExpressions;
Expand Down Expand Up @@ -85,17 +82,10 @@ private OrderSpecifier<?> getSortedColumn(SortType sortType) {
case EMPATHY:
return QAlbum.album.empathies.size().desc();
case COMMENT:
return new OrderSpecifier<>(Order.DESC, getCommentSubQuery());
return QAlbum.album.comments.size().desc();
}
return OrderByNull.getDefault();
}

private Expression getCommentSubQuery() {
return JPAExpressions.select(QComment.comment.id.count())
.from(QComment.comment)
.where(QComment.comment.deleted.isFalse(),
QComment.comment.album.id.eq(QAlbum.album.id));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.ArrayList;
Expand All @@ -15,7 +16,8 @@
@Getter
@Entity
@NoArgsConstructor(access = lombok.AccessLevel.PROTECTED)
@SQLDelete(sql = "UPDATE comment SET deleted = true WHERE id = ?")
@SQLDelete(sql = "UPDATE comment SET deleted = true WHERE comment_id = ?")
@Where(clause = "deleted = false")
public class Comment extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -40,7 +42,7 @@ public class Comment extends BaseEntity {
private Comment parent;

//자식 정의 (대댓글)
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Comment> childList = new ArrayList<>();

public void addChild(Comment child){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public CommentInfoResponse update(@PathVariable Long commentId, @RequestBody Com
return commentUpdateUseCase.updateComment(commentId, commentUpdateRequest);
}

@CacheEvict(value = CachingStoreConst.COMMENT_COUNT_CACHE_NAME, key = "#albumId")
@CacheEvict(value = CachingStoreConst.COMMENT_COUNT_CACHE_NAME, key = "#albumId",condition="#albumId!=null")
@DeleteMapping("/{albumId}/comment/{commentId}")
public void delete(@PathVariable Long commentId){
commentDeleteUseCase.deleteComment(commentId);
Expand Down

0 comments on commit e6873d1

Please sign in to comment.