Skip to content

Commit

Permalink
Merge pull request #6 from molohala/Feature/community
Browse files Browse the repository at this point in the history
chore: add islike in community res
  • Loading branch information
jombidev authored Apr 16, 2024
2 parents 96ce56f + 3134a96 commit 9bcc4af
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 32 deletions.
3 changes: 3 additions & 0 deletions infinity-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ dependencies {
implementation(project(":infinity-common"))
implementation(project(":infinity-core"))
implementation(project(":infinity-infra"))

testImplementation ("org.springframework.boot:spring-boot-starter-test")

}

tasks.bootJar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.molohala.infinityapi.response.ResponseData
import com.molohala.infinitycore.common.PageRequest
import com.molohala.infinitycore.community.application.dto.req.CommunityModifyReq
import com.molohala.infinitycore.community.application.dto.req.CommunitySaveReq
import com.molohala.infinitycore.community.application.dto.res.CommunityRes
import com.molohala.infinitycore.community.application.service.CommunityService
import org.springframework.web.bind.annotation.*

Expand All @@ -18,7 +19,7 @@ class CommunityController(
@RequestBody communitySaveReq: CommunitySaveReq
): Response {
communityService.save(communitySaveReq)
return Response.ok("커뮤니티 생성 성공")
return Response.created("커뮤니티 생성 성공")
}

@GetMapping
Expand All @@ -29,7 +30,9 @@ class CommunityController(
@GetMapping("/{id}")
fun getById(
@PathVariable("id") id: Long
) = ResponseData.ok("커뮤니티 조회 성공", communityService.getById(id))
): ResponseData<CommunityRes?> {
return ResponseData.ok("커뮤니티 조회 성공", communityService.getById(id))
}

@PatchMapping
fun modify(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.molohala.infinityapi

//import org.junit.jupiter.api.Test
//import org.springframework.boot.test.context.SpringBootTest
//
//@SpringBootTest
//class InfinityApiApplicationTests {
//
// @Test
// fun contextLoads() {
// }
//
//}
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest
class InfinityApiApplicationTests {

@Test
fun contextLoads() {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class QueryDslCommentRepository(
return queryFactory.select(commentProjection())
.from(comment)
.where(comment.communityId.eq(communityId))
.orderBy(comment.createdAt.desc())
.innerJoin(member)
.on(comment.memberId.eq(member.id))
.fetch()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package com.molohala.infinitycore.community.application.dto.res
import com.querydsl.core.annotations.QueryProjection
import java.time.LocalDateTime

data class CommunityListRes @QueryProjection constructor(
data class CommunityRes @QueryProjection constructor(
val communityId: Long,
val content: String,
val createdAt: LocalDateTime,
val like: Long,
val isLiked: Boolean,
val writerName: String,
val writerId: Long
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import com.molohala.infinitycommon.exception.custom.CustomException
import com.molohala.infinitycore.common.PageRequest
import com.molohala.infinitycore.community.application.dto.req.CommunityModifyReq
import com.molohala.infinitycore.community.application.dto.req.CommunitySaveReq
import com.molohala.infinitycore.community.application.dto.res.CommunityListRes
import com.molohala.infinitycore.community.application.dto.res.CommunityRes
import com.molohala.infinitycore.community.domain.consts.CommunityState
import com.molohala.infinitycore.community.domain.entity.Community
import com.molohala.infinitycore.community.domain.exception.CommunityNotFoundException
import com.molohala.infinitycore.community.repository.CommunityJpaRepository
import com.molohala.infinitycore.community.repository.QueryCommunityRepository
import com.molohala.infinitycore.like.repository.QueryLikeRepository
import com.molohala.infinitycore.member.application.MemberSessionHolder
import com.molohala.infinitycore.member.domain.entity.Member
import com.molohala.infinitycore.member.domain.exception.AccessDeniedException
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
Expand All @@ -38,24 +39,28 @@ class CommunityService(
)
}

fun getList(page: PageRequest): List<CommunityListRes> {
fun getList(page: PageRequest): List<CommunityRes> {
if (page.page < 1) throw CustomException(GlobalExceptionCode.INVALID_PARAMETER)
return queryCommunityRepository.findWithPagination(page)
.map {
CommunityListRes(
CommunityRes(
it.communityId,
it.content,
it.createdAt,
queryLikeRepository.getCntByCommunityId(it.communityId),
queryLikeRepository.existsByCommunityIdAndMemberId(it.communityId,it.writerId),
it.writerName,
it.writerId
)
}
}

fun getById(id: Long): CommunityListRes? {
fun getById(id: Long): CommunityRes? {
val member: Member = memberSessionHolder.current()
val likeCnt: Long = queryLikeRepository.getCntByCommunityId(id)
return queryCommunityRepository.findById(id, likeCnt)
val isLike: Boolean = queryLikeRepository
.existsByCommunityIdAndMemberId(id,member.id!!)
return queryCommunityRepository.findById(id, likeCnt, isLike)
}

@Transactional(rollbackFor = [Exception::class])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.molohala.infinitycore.community.repository

import com.molohala.infinitycore.common.PageRequest
import com.molohala.infinitycore.community.application.dto.res.CommunityListRes
import com.molohala.infinitycore.community.application.dto.res.CommunityRes

interface QueryCommunityRepository {
fun findWithPagination(pageRequest: PageRequest):List<CommunityListRes>
fun findById(id: Long, likeCnt:Long): CommunityListRes?
fun findWithPagination(pageRequest: PageRequest):List<CommunityRes>
fun findById(id: Long, likeCnt:Long, isLike: Boolean): CommunityRes?
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.molohala.infinitycore.community.repository

import com.molohala.infinitycore.common.PageRequest
import com.molohala.infinitycore.community.application.dto.res.CommunityListRes
import com.molohala.infinitycore.community.application.dto.res.CommunityRes
import com.molohala.infinitycore.community.domain.entity.Community
import com.molohala.infinitycore.community.domain.entity.QCommunity.community
import com.molohala.infinitycore.member.domain.entity.QMember.member
Expand All @@ -19,34 +19,36 @@ class QueryDslCommunityRepository(
val query: JPAQuery<Community> = queryFactory.select(community)
.from(community)

override fun findWithPagination(pageRequest: PageRequest): List<CommunityListRes> {
override fun findWithPagination(pageRequest: PageRequest): List<CommunityRes> {
return queryFactory
.select(
communityProjection(0)
communityProjection(0, false)
)
.from(community)
.orderBy(community.createdAt.desc())
.innerJoin(member).on(community.memberId.eq(member.id))
.offset((pageRequest.page - 1) * pageRequest.size)
.limit(pageRequest.size)
.fetch()
}

override fun findById(id: Long, likeCnt:Long): CommunityListRes? {
override fun findById(id: Long, likeCnt:Long, isLike:Boolean): CommunityRes? {
return queryFactory
.select(communityProjection(likeCnt))
.select(communityProjection(likeCnt, isLike))
.from(community)
.innerJoin(member).on(community.memberId.eq(member.id))
.where(community.id.eq(id))
.fetchFirst()
}

private fun communityProjection(likeCount: Long): ConstructorExpression<CommunityListRes> {
private fun communityProjection(likeCount: Long, isLike: Boolean): ConstructorExpression<CommunityRes> {
return Projections.constructor(
CommunityListRes::class.java,
CommunityRes::class.java,
community.id,
community.content,
community.createdAt,
Expressions.constant(likeCount), // 집계 함수로 얻은 좋아요 개수를 상수 표현식으로 추가
Expressions.constant(likeCount),
Expressions.constant(isLike),
member.name,
member.id
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,4 @@ class QueryDslLikeRepository(
.where(like.id.eq(communityId))
.fetchCount()
}


}

0 comments on commit 9bcc4af

Please sign in to comment.