Skip to content

#170 - 쪽지 답장 관련 예외들을 수정합니다. #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CreateMessageV2Controller(

@PostMapping
fun createMessageV2(
createdMessageV2Request: CreatedMessageV2Request
@RequestBody createdMessageV2Request: CreatedMessageV2Request
): ResponseEntity<Unit> {
createdMessageV2UseCase.createMessage(createdMessageV2Request)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import com.wespot.message.dto.response.MessageV2DetailsResponse
import com.wespot.message.dto.response.MessageV2OverviewResponse
import com.wespot.message.port.`in`.GetMessageV2UseCase
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/api/v2/messages")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.wespot.message.v2

import com.wespot.message.dto.response.TitleOfMessageV2Response
import com.wespot.message.port.`in`.GetTitleOfMessageV2UseCase
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/api/v2/messages")
class GetTitleOfMessageV2Controller(
private val getTitleOfMessageV2UseCase: GetTitleOfMessageV2UseCase,
) {

@GetMapping("/title")
fun getTitleOfMessage(): ResponseEntity<TitleOfMessageV2Response> {
val response = getTitleOfMessageV2UseCase.getTitle()

return ResponseEntity.ok(response)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class CreatedMessageV2Request(
val receiverId: Long,

val isAnonymous: Boolean,
val anonymousImageUrl: String,
val anonymousProfileName: String,
val anonymousImageUrl: String?,
val anonymousProfileName: String?,
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ data class MessageV2OverviewResponse( // TODO : 문서 변경
thumbnail = room.receiverProfileImage(),
isExistsUnreadMessage = room.isExistsUnReadMessage(),
latestChatTime = room.latestChatTime(),
isAnonymous = room.isAnonymous(),
isAnonymous = room.isReceiverUsingAnonymous(),
name = room.receiverName(),
schoolName = room.receiverSchoolName(),
grade = room.receiverGrade(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.wespot.message.dto.response

data class MessageV2OverviewResponses(
val messages: List<MessageV2OverviewResponse>,
val hasNext: Boolean,
val lastCursor: Long?,
) {

companion object {

// fun of(
// messages: List<MessageV2OverviewResponse>,
// hasNext: Boolean,
// ): MessageV2OverviewResponses {
// return MessageV2OverviewResponses(
// messages = messages,
// hasNext = hasNext,
// lastCursor = lastCursor
// )
// }
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.wespot.message.dto.response

import com.wespot.user.User

data class TitleOfMessageV2Response(
val title: String,
) {

companion object {

fun from(loginUser: User): TitleOfMessageV2Response {
return TitleOfMessageV2Response("${loginUser.name}님을 설레게 한 친구에게\n 쪽지로 마음을 표현해 보세요")
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.wespot.message.port.`in`

import com.wespot.message.dto.response.TitleOfMessageV2Response

interface GetTitleOfMessageV2UseCase {

fun getTitle(): TitleOfMessageV2Response

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class CreatedMessageV2Service(
if (createdMessageV2Request.isAnonymous) {
return profileUseCase.createAnonymousProfile(
CreatedAnonymousProfileRequest(
name = createdMessageV2Request.anonymousProfileName,
imageUrl = createdMessageV2Request.anonymousImageUrl,
name = createdMessageV2Request.anonymousProfileName!!,
imageUrl = createdMessageV2Request.anonymousImageUrl!!,
receiverId = createdMessageV2Request.receiverId
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ class GetAnonymousProfileService(

@Transactional(readOnly = true)
override fun getAnonymousProfileByReceiverId(receiverId: Long): List<AnonymousProfileResponse> {
val sender = SecurityUtils.getLoginUser(userPort = userPort)
val loginUser = SecurityUtils.getLoginUser(userPort = userPort)
val messageRooms =
messagePort.findAllMessageRoomBySenderIdAndReceiverId(senderId = sender.id, receiverId = receiverId)
messagePort.findAllMessageRoomBySenderIdAndReceiverId(senderId = loginUser.id, receiverId = receiverId)
val messageRoomIds = messageRooms.map { it.id }
val messageDetails = messagePort.findAllLastMessageOfRoomByRoomIdIn(messageRoomIds)

val rooms =
MessageRooms.createOverview(user = sender, rooms = messageRooms, messageDetails = messageDetails)
MessageRooms.createOverview(viewer = loginUser, rooms = messageRooms, messageDetails = messageDetails)

return UserProfiles.of(messageRooms = rooms)
return UserProfiles.of(viewer = loginUser, messageRooms = rooms)
.asList()
.map { AnonymousProfileResponse.from(it) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class GetMessageV2Service(
val messageRoomIds: List<Long> = rooms.map { it.id }
val messageDetails = messageV2Port.findAllLastMessageOfRoomByRoomIdIn(messageRoomIds)

val messageRooms = MessageRooms.createOverview(user = loginUser, rooms = rooms, messageDetails = messageDetails)
val messageRooms = MessageRooms.createOverview(viewer = loginUser, rooms = rooms, messageDetails = messageDetails)

return messageRooms.asList()
.map { MessageV2OverviewResponse.from(it) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.wespot.message.service.v2

import com.wespot.auth.service.SecurityUtils
import com.wespot.message.dto.response.TitleOfMessageV2Response
import com.wespot.message.port.`in`.GetTitleOfMessageV2UseCase
import com.wespot.user.port.out.UserPort
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class GetTitleOfMessageV2Service(
private val userPort: UserPort,
) : GetTitleOfMessageV2UseCase {

@Transactional(readOnly = true)
override fun getTitle(): TitleOfMessageV2Response {
val loginUser = SecurityUtils.getLoginUser(userPort = userPort)

return TitleOfMessageV2Response.from(loginUser)
}

}
10 changes: 7 additions & 3 deletions domain/src/main/kotlin/com/wespot/message/v2/MessageRoom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ data class MessageRoom(
return roomMessage.id
}

fun isAnonymous(): Boolean {
return roomMessage.isAnonymousReceiver(viewer = viewer)
fun isMeUsingAnonymous(): Boolean {
return roomMessage.isMeAnonymous(viewer = viewer)
}

fun isReceiverUsingAnonymous(): Boolean {
return roomMessage.isReceiverAnonymous(viewer = viewer)
}

fun isViewerOwnerOfMessageRoom(): Boolean {
Expand Down Expand Up @@ -173,7 +177,7 @@ data class MessageRoom(
return messages.asList(viewer = viewer)
}

fun readUnreadMessages():List<MessageV2>{
fun readUnreadMessages(): List<MessageV2> {
return messages.readUnreadMessage(viewer = viewer)
}

Expand Down
11 changes: 5 additions & 6 deletions domain/src/main/kotlin/com/wespot/message/v2/MessageRooms.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ data class MessageRooms(

companion object {

fun createOverview(user: User, rooms: List<MessageV2>, messageDetails: List<MessageV2>): MessageRooms {
fun createOverview(viewer: User, rooms: List<MessageV2>, messageDetails: List<MessageV2>): MessageRooms {
val roomIdToMessages: Map<Long, List<MessageV2>> = messageDetails
.filter { it.messageRoomId != null }
.groupBy { it.messageRoomId!! }

val resultOfRooms = rooms.map { roomMessage ->
val messages = roomIdToMessages[roomMessage.messageRoomId] ?: emptyList()
MessageRoom.of(viewer = user, roomMessage = roomMessage, messages = messages)
MessageRoom.of(viewer = viewer, roomMessage = roomMessage, messages = messages)
}

return MessageRooms(resultOfRooms)
Expand All @@ -33,19 +33,18 @@ data class MessageRooms(
return rooms
}

fun viewer(): User {
fun viewer(): User? {
val viewerSet = rooms.map { it.viewer }
.toSet()

if (viewerSet.size != 1) {
if (viewerSet.size > 1) {
throw CustomException(
message = "조회자는 무조건 1명을 초과할 수 없습니다.",
status = HttpStatus.BAD_REQUEST,
view = ExceptionView.TOAST,
)
}

return viewerSet.first()
return viewerSet.firstOrNull()
}

}
18 changes: 12 additions & 6 deletions domain/src/main/kotlin/com/wespot/message/v2/MessageV2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,17 @@ data class MessageV2(
return anonymousProfile != null
}

fun isAnonymousReceiver(viewer: User): Boolean {
fun isMeAnonymous(viewer: User): Boolean {
validateRoomMessage()

if (viewer.isMeReceiver(receiverId = receiver.id)) {
return false
}

return anonymousProfile != null
}

fun isReceiverAnonymous(viewer: User): Boolean {
validateRoomMessage()

return receiverUsingAnonymousProfile(viewer = viewer)
Expand Down Expand Up @@ -268,11 +278,7 @@ data class MessageV2(
}

fun isAbleToAnswer(viewer: User): Boolean {
if (viewer.isMeReceiver(receiverId = receiver.id)) {
return true
}

return false
return viewer.isMeReceiver(receiverId = receiver.id)
}

fun isSameUserProfileAndNotAnonymous(viewer: User): Boolean {
Expand Down
10 changes: 7 additions & 3 deletions domain/src/main/kotlin/com/wespot/message/v2/UserProfile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ data class UserProfile(
}

fun createByUserProfile(
user: User,
user: User?,
messageRoom: List<MessageRoom>
): UserProfile {
): UserProfile? {
if (user == null) {
return null
}

return UserProfile(
profileId = user.profile.id,
image = user.profile.iconUrl,
Expand All @@ -54,7 +58,7 @@ data class UserProfile(

fun isAbleToAnswer(): Boolean {
if (messageRoom == EMPTY_MESSAGE_ROOM) {
return false
return true
}

return messageRoom.isAbleToAnswer()
Expand Down
10 changes: 6 additions & 4 deletions domain/src/main/kotlin/com/wespot/message/v2/UserProfiles.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.wespot.message.v2

import com.wespot.exception.CustomException
import com.wespot.exception.ExceptionView
import com.wespot.user.User
import org.springframework.http.HttpStatus

data class UserProfiles(
Expand All @@ -10,7 +11,7 @@ data class UserProfiles(

companion object {

fun of(messageRooms: MessageRooms): UserProfiles {
fun of(viewer: User, messageRooms: MessageRooms): UserProfiles {
val isContainsOwnerIsNotViewer = messageRooms.asList()
.any { !it.isViewerOwnerOfMessageRoom() }
if (isContainsOwnerIsNotViewer) {
Expand All @@ -22,7 +23,7 @@ data class UserProfiles(
}

val anonymousProfiles = messageRooms.asList()
.filter { it.isAnonymous() }
.filter { it.isMeUsingAnonymous() }
.map { it.anonymousProfile()!! }

val resultOfUserProfiles = anonymousProfiles.map { anonymousProfile ->
Expand All @@ -31,12 +32,13 @@ data class UserProfiles(
messageRoom = messageRooms.asList()
)
} + UserProfile.createByUserProfile(
user = messageRooms.viewer(),
user = viewer,
messageRoom = messageRooms.asList()
)

return UserProfiles(
userProfiles = resultOfUserProfiles.sortedBy { it.recentlyTalk() }
userProfiles = resultOfUserProfiles.filterNotNull()
.sortedBy { it.recentlyTalk() }
.reversed()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ interface MessageV2JpaRepository : JpaRepository<MessageJpaEntityV2, Long> {
)
fun findAllLastMessageOfRoomByReceiverIdAndFromDate(
receiverId: Long,
from: LocalDate
from: LocalDateTime
): List<MessageJpaEntityV2>

}
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class MessageV2PersistenceAdapter(
val messages =
messageV2JpaRepository.findAllLastMessageOfRoomByReceiverIdAndFromDate(
receiverId = receiverId,
from = from
from = from.atStartOfDay()
)

return getCompleteMessageV2(messages = messages)
Expand Down
Loading