-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSpRepositoryCustom.java
More file actions
59 lines (50 loc) · 1.93 KB
/
SpRepositoryCustom.java
File metadata and controls
59 lines (50 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.pitchain.sp.infrastucture;
import com.pitchain.common.constant.MainCategory;
import com.pitchain.sp.infrastucture.dto.QSpWithLikeDto;
import com.pitchain.sp.infrastucture.dto.SpWithLikeDto;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import static com.pitchain.bm.domain.QBm.bm;
import static com.pitchain.sp.domain.QSp.sp;
import static com.pitchain.splike.domain.QSpLike.spLike;
@RequiredArgsConstructor
@Repository
public class SpRepositoryCustom {
private final JPAQueryFactory queryFactory;
public List<SpWithLikeDto> getSpWithLikeDtoFilteredCategory(Long memberId, MainCategory category, Long lastSpId, int size) {
return queryFactory
.select(new QSpWithLikeDto(
sp,
spLike.isNotNull()
))
.from(sp)
.leftJoin(sp.bm, bm)
.leftJoin(spLike).on(spLike.sp.id.eq(sp.id).and(spLike.member.id.eq(memberId)))
.where(
eqMainCategory(category),
ltSpId(lastSpId)
)
.orderBy(sp.id.desc())
.limit(size + 1)
.distinct()
.fetch();
}
private static BooleanExpression eqMainCategory(MainCategory category) {
return bm.mainCategory.eq(category);
}
private BooleanExpression ltSpId(Long lastSpId) {
return lastSpId == null ? null : sp.id.lt(lastSpId);
}
@Transactional
public long updateSpView(Long spId, Long views) {
return queryFactory
.update(sp)
.set(sp.views, sp.views.add(views))
.where(sp.id.eq(spId))
.execute();
}
}