@@ -16,17 +16,17 @@ class MatchService(database: Database) {
1616
1717 class InvitationEntity (id : EntityID <Int >) : IntEntity(id) {
1818 companion object : IntEntityClass <InvitationEntity >(InvitationTable ) {
19- fun findMatchesForUser (userId : Int ) = find {
19+ fun findMatchesForUser (userId : Int ): List < EntityID < Int >> = find {
2020 (InvitationTable .userId eq userId) and (InvitationTable .requestStatus eq MatchStatus .ACK ) and (InvitationTable .responseStatus eq MatchStatus .ACK )
21- }.toList(). map { it.matchedUserId }
21+ }.map { it.matchedUserId }
2222
23- fun findRequestReceivedForUser (userId : Int ) = find {
23+ fun findRequestReceivedForUser (userId : Int ): List < EntityID < Int >> = find {
2424 (InvitationTable .matchedUserId eq userId) and (InvitationTable .responseStatus eq MatchStatus .NONE ) and (InvitationTable .requestStatus eq MatchStatus .ACK )
25- }.toList(). map { it.userId }
25+ }.map { it.userId }
2626
27- fun findResponseSentForUser (userId : Int ) = find {
28- (InvitationTable .userId eq userId) and (InvitationTable .requestStatus neq MatchStatus .NACK )
29- }.toList()
27+ fun findPendingSentRequestsForUser (userId : Int ): List < EntityID < Int >> = find {
28+ (InvitationTable .userId eq userId) and (InvitationTable .requestStatus eq MatchStatus .ACK ) and ( InvitationTable .responseStatus eq MatchStatus . NONE )
29+ }.map { it.matchedUserId }
3030 }
3131
3232 var userId by InvitationTable .userId
@@ -51,49 +51,46 @@ class MatchService(database: Database) {
5151 }
5252
5353 fun getResultsForUser (userId : Int ): MatchResultResponse = transaction {
54- val user = UserService .UserEntity .findById(userId)!! .toShared()
54+ val user = UserService .UserEntity .findById(userId)? .toShared() ? : throw IllegalArgumentException ( " User not found " )
5555
5656 val matches = InvitationEntity .findMatchesForUser(userId)
57- val sentRequests =
58- InvitationEntity .findResponseSentForUser(userId)
59- .filter { it.requestStatus == MatchStatus .ACK && it.responseStatus == MatchStatus .NONE }
60- .map { it.matchedUserId }
57+ val sentRequests = InvitationEntity .findPendingSentRequestsForUser(userId)
6158 val receivedRequests = InvitationEntity .findRequestReceivedForUser(userId)
62- val allUsers = UserService . UserEntity .findByIds(
63- matches + sentRequests + receivedRequests
64- )
59+
60+ val allUserIds = ( matches + sentRequests + receivedRequests).distinct()
61+ val allUsers = UserService . UserEntity .findByIds(allUserIds).associateBy { it.id }
6562
6663 MatchResultResponse (
67- allUsers.filter { it.id in matches }.map { Match (it.toShared(), countScore(it.toShared(), user)) },
68- allUsers.filter { it.id in sentRequests }.map { Match (it.toShared(), countScore(it.toShared(), user)) },
69- allUsers.filter { it.id in receivedRequests }.map { Match (it.toShared(), countScore(it.toShared(), user)) })
64+ matches = matches.mapNotNull { allUsers[it] }
65+ .map { Match (it.toShared(), countScore(it.toShared(), user)) },
66+ sentRequests = sentRequests.mapNotNull { allUsers[it] }
67+ .map { Match (it.toShared(), countScore(it.toShared(), user)) },
68+ receivedRequests = receivedRequests.mapNotNull { allUsers[it] }
69+ .map { Match (it.toShared(), countScore(it.toShared(), user)) })
7070 }
7171
7272 fun getAvailableMatchesForUser (userId : Int ): List <User > = transaction {
73- val requestsSent =
74- InvitationEntity .find {
75- (InvitationTable .userId eq userId) and (InvitationTable .requestStatus neq MatchStatus .NONE )
76- }.toList().map { it.matchedUserId }
77-
78- val result =
73+ val requestsSent = InvitationEntity .find {
74+ (InvitationTable .userId eq userId) and (InvitationTable .requestStatus neq MatchStatus .NONE )
75+ }.map { it.matchedUserId }
76+
77+ // Find users who:
78+ // 1. Are not the current user
79+ // 2. Haven't been swiped on by the current user
80+ // 3. Have completed their profile (have info and preferences)
7981 UserService .UserEntity .find {
8082 (UsersTable .id neq userId) and (UsersTable .id notInList requestsSent) and (UsersTable .info neq null ) and (UsersTable .preferences neq null )
8183 }.map { it.toShared() }
82- result
8384 }
8485
8586 fun getRequestReceivedForUser (userId : Int ): List <User > = transaction {
86- val requests = InvitationEntity .findRequestReceivedForUser(userId)
87- UserService .UserEntity .findByIds(requests ).map { it.toShared() }
87+ val requestUserIds = InvitationEntity .findRequestReceivedForUser(userId)
88+ UserService .UserEntity .findByIds(requestUserIds ).map { it.toShared() }
8889 }
8990
9091 fun registerSwipe (thisUserId : Int , swipedUserId : Int , status : MatchStatus ): MatchStatus = transaction {
91- val thisUser = UserService .UserEntity .findById(thisUserId)
92- val swipedUser = UserService .UserEntity .findById(swipedUserId)
93-
94- if (thisUser == null || swipedUser == null ) {
95- throw IllegalArgumentException (" User not found" )
96- }
92+ val thisUser = UserService .UserEntity .findById(thisUserId) ? : throw IllegalArgumentException (" User not found" )
93+ val swipedUser = UserService .UserEntity .findById(swipedUserId) ? : throw IllegalArgumentException (" User not found" )
9794
9895 val invitation = InvitationEntity .find {
9996 (InvitationTable .userId eq thisUser.id) and (InvitationTable .matchedUserId eq swipedUser.id)
0 commit comments