Skip to content

Commit

Permalink
Merge pull request #24 from Preta3418/main
Browse files Browse the repository at this point in the history
[Refactor]: ScoutRequest 리팩토링 완료
  • Loading branch information
Preta3418 authored Nov 4, 2024
2 parents 16061b9 + 9c054dd commit 8b31e85
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.tenten.bittakotlin.apply.entity.Apply
import org.tenten.bittakotlin.like.entity.Like
import org.tenten.bittakotlin.member.entity.Member
import org.tenten.bittakotlin.profile.constant.Job
import org.tenten.bittakotlin.scout.entity.ScoutRequest

//data class 로 변경
@Entity
Expand Down Expand Up @@ -37,5 +38,11 @@ class Profile(
val apply: List<Apply> = mutableListOf(),

@OneToMany(mappedBy = "profile", fetch = FetchType.EAGER, cascade = [CascadeType.REMOVE], orphanRemoval = true)
val like: List<Like> = mutableListOf()
val like: List<Like> = mutableListOf(),

@OneToMany(mappedBy = "sender", cascade = [CascadeType.ALL], orphanRemoval = true)
val sentScoutRequests: List<ScoutRequest> = mutableListOf(),

@OneToMany(mappedBy = "receiver", cascade = [CascadeType.ALL], orphanRemoval = true)
val receivedScoutRequests: List<ScoutRequest> = mutableListOf()
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.tenten.bittakotlin.scout.controller
/*


import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
Expand Down Expand Up @@ -44,4 +44,3 @@ class ScoutRequestController(
}
}

*/
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package org.tenten.bittakotlin.scout.entity
import jakarta.persistence.*
import org.springframework.data.annotation.CreatedDate
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import org.tenten.bittakotlin.member.entity.Member
import org.tenten.bittakotlin.feed.entity.Feed
import org.tenten.bittakotlin.profile.entity.Profile
import java.time.LocalDateTime

@Entity
Expand All @@ -13,18 +14,17 @@ data class ScoutRequest(
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long? = null,

//feed 마이그레이션 종료가 안되어 일단 주석처리 하겠습니다
//@ManyToOne
//@JoinColumn(name = "feed_id", nullable = false)
//val feed: Feed,
@ManyToOne
@JoinColumn(name = "feed_id", nullable = false)
val feed: Feed,

@ManyToOne
@JoinColumn(name = "sender_id", nullable = false)
val sender: Member,
val sender: Profile,

@ManyToOne
@JoinColumn(name = "receiver_id", nullable = false)
val receiver: Member,
val receiver: Profile,

@Lob
val description: String? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import org.springframework.data.jpa.repository.JpaRepository
import org.tenten.bittakotlin.scout.entity.ScoutRequest

interface ScoutRequestRepository : JpaRepository<ScoutRequest, Long> {
fun findBySenderIdOrderById(senderId: Long, pageable: Pageable): Page<ScoutRequest>
fun findByReceiverIdOrderById(receiverId: Long, pageable: Pageable): Page<ScoutRequest>
fun findBySender_IdOrderById(senderId: Long, pageable: Pageable): Page<ScoutRequest>
fun findByReceiver_IdOrderById(receiverId: Long, pageable: Pageable): Page<ScoutRequest>
}
Original file line number Diff line number Diff line change
@@ -1,69 +1,90 @@
package org.tenten.bittakotlin.scout.service
/*
//import com.prgrms2.java.bitta.feed.entity.Feed
//import com.prgrms2.java.bitta.member.entity.Member
//import com.prgrms2.java.bitta.member.service.MemberProvider
//import com.prgrms2.java.bitta.feed.service.FeedProvider


import jakarta.persistence.EntityNotFoundException
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.tenten.bittakotlin.member.repository.MemberRepository
import org.tenten.bittakotlin.feed.repository.FeedRepository
import org.tenten.bittakotlin.profile.repository.ProfileRepository
import org.tenten.bittakotlin.scout.dto.ScoutDTO
import org.tenten.bittakotlin.scout.entity.ScoutRequest
import org.tenten.bittakotlin.scout.repository.ScoutRequestRepository
import java.lang.reflect.Member


@Service
class ScoutRequestService(
private val scoutRequestRepository: ScoutRequestRepository,
// private val feedProvider: FeedProvider,
private val memberRepository: MemberRepository
private val feedRepository: FeedRepository,
private val profileRepository: ProfileRepository
) {

private val logger: Logger = LoggerFactory.getLogger(ScoutRequestService::class.java)

@Transactional
fun sendScoutRequest(scoutDTO: ScoutDTO): ScoutDTO {
logger.info("Attempting to send scout request from senderId=${scoutDTO.senderId} to receiverId=${scoutDTO.receiverId}")

val request = dtoToEntity(scoutDTO)
val savedRequest = scoutRequestRepository.save(request)

logger.info("Scout request successfully saved with id=${savedRequest.id}")
return entityToDto(savedRequest)
}

@Transactional(readOnly = true)
fun getSentScoutRequests(senderId: Long, pageable: Pageable): Page<ScoutDTO> {
return scoutRequestRepository.findBySenderIdOrderById(senderId, pageable)
logger.info("Fetching sent scout requests for senderId=$senderId")

val sentRequests = scoutRequestRepository.findBySender_IdOrderById(senderId, pageable)
.map { request -> entityToDto(request) }

logger.info("Retrieved ${sentRequests.totalElements} sent scout requests for senderId=$senderId")
return sentRequests
}

@Transactional(readOnly = true)
fun getReceivedScoutRequests(receiverId: Long, pageable: Pageable): Page<ScoutDTO> {
return scoutRequestRepository.findByReceiverIdOrderById(receiverId, pageable)
logger.info("Fetching received scout requests for receiverId=$receiverId")

val receivedRequests = scoutRequestRepository.findByReceiver_IdOrderById(receiverId, pageable)
.map { request -> entityToDto(request) }

logger.info("Retrieved ${receivedRequests.totalElements} received scout requests for receiverId=$receiverId")
return receivedRequests
}

private fun entityToDto(request: ScoutRequest): ScoutDTO {
// return ScoutDTO(
// id = request.id,
// senderId = request.sender?.id ?: throw IllegalStateException("Sender ID is missing"),
//receiverId = request.receiver?.id ?: throw IllegalStateException("Receiver ID is missing"),
// description = request.description,
//sentAt = request.sentAt
return ScoutDTO(
id = request.id,
feedId = request.feed.id ?: throw IllegalStateException("Feed ID is missing"),
senderId = request.sender.id ?: throw IllegalStateException("Sender Profile ID is missing"),
receiverId = request.receiver.id ?: throw IllegalStateException("Receiver Profile ID is missing"),
description = request.description,
sentAt = request.sentAt
)
}

private fun dtoToEntity(scoutDTO: ScoutDTO): ScoutRequest {
// val feed = feedRepository.findById(scoutDTO.feedId)
// .orElseThrow { EntityNotFoundException("Feed not found with id=${scoutDTO.feedId}") }
logger.info("Converting ScoutDTO to ScoutRequest entity")

val feed = feedRepository.findById(scoutDTO.feedId)
.orElseThrow { EntityNotFoundException("Feed not found with id=${scoutDTO.feedId}") }

val sender = memberRepository.findById(scoutDTO.senderId)
val sender = profileRepository.findById(scoutDTO.senderId)
.orElseThrow { EntityNotFoundException("Sender not found with id=${scoutDTO.senderId}") }

val receiver = memberRepository.findById(scoutDTO.receiverId)
val receiver = profileRepository.findById(scoutDTO.receiverId)
.orElseThrow { EntityNotFoundException("Receiver not found with id=${scoutDTO.receiverId}") }

logger.info("ScoutRequest entity created with feedId=${feed.id}, senderId=${sender.id}, receiverId=${receiver.id}")

return ScoutRequest(
id = scoutDTO.id,
// feed = feed,
feed = feed,
sender = sender,
receiver = receiver,
description = scoutDTO.description,
Expand All @@ -72,4 +93,3 @@ class ScoutRequestService(
}
}

*/

0 comments on commit 8b31e85

Please sign in to comment.