Class MessageService
java.lang.Object
fr.utc.miage.sporttrack.service.user.communication.MessageService
Service layer component responsible for managing
Message entities
within the SportTrack application.
Provides business logic for sending messages, retrieving conversations, and tracking unread message counts between athlete friends.
- Author:
- SportTrack Team
-
Constructor Summary
ConstructorsConstructorDescriptionMessageService(MessageRepository messageRepository, AthleteRepository athleteRepository, FriendshipRepository friendshipRepository) Constructs aMessageServicewithout notification support.MessageService(MessageRepository messageRepository, AthleteRepository athleteRepository, FriendshipRepository friendshipRepository, NotificationService notificationService) Constructs aMessageServicewith full notification support. -
Method Summary
Modifier and TypeMethodDescriptionlongcountUnreadMessages(Integer athleteId) Counts the total number of unread messages for the specified athlete.getConversation(Integer currentUserId, Integer friendId) Retrieves the full conversation between two friends and marks all messages as seen.getLatestMessageByFriend(Integer athleteId) Returns a map of friend identifiers to their most recent message with the given athlete.getUnreadCountByFriend(Integer athleteId) Returns a map of friend identifiers to the count of unread messages from that friend.sendMessage(Integer senderId, Integer recipientId, String content) Sends a message from one athlete to another.
-
Constructor Details
-
MessageService
public MessageService(MessageRepository messageRepository, AthleteRepository athleteRepository, FriendshipRepository friendshipRepository) Constructs aMessageServicewithout notification support.- Parameters:
messageRepository- the message repositoryathleteRepository- the athlete repositoryfriendshipRepository- the friendship repository
-
MessageService
@Autowired public MessageService(MessageRepository messageRepository, AthleteRepository athleteRepository, FriendshipRepository friendshipRepository, NotificationService notificationService) Constructs aMessageServicewith full notification support.- Parameters:
messageRepository- the message repositoryathleteRepository- the athlete repositoryfriendshipRepository- the friendship repositorynotificationService- the notification service for message events
-
-
Method Details
-
sendMessage
Sends a message from one athlete to another. Both athletes must be friends.- Parameters:
senderId- the identifier of the sending athleterecipientId- the identifier of the receiving athletecontent- the textual content of the message- Returns:
- the persisted
Message - Throws:
IllegalArgumentException- if sender equals recipient, content is empty, or a user is not foundIllegalStateException- if the two athletes are not friends
-
getConversation
Retrieves the full conversation between two friends and marks all messages as seen.- Parameters:
currentUserId- the identifier of the current userfriendId- the identifier of the friend- Returns:
- a list of messages in the conversation, ordered by send time ascending
- Throws:
IllegalStateException- if the two athletes are not friends
-
getLatestMessageByFriend
Returns a map of friend identifiers to their most recent message with the given athlete.- Parameters:
athleteId- the identifier of the athlete- Returns:
- a map where each key is a friend's identifier and the value is the latest message
-
getUnreadCountByFriend
Returns a map of friend identifiers to the count of unread messages from that friend.- Parameters:
athleteId- the identifier of the athlete- Returns:
- a map where each key is a friend's identifier and the value is the unread count
-
countUnreadMessages
Counts the total number of unread messages for the specified athlete.- Parameters:
athleteId- the identifier of the athlete- Returns:
- the total count of unread messages
-