Interface MessageRepository
- All Superinterfaces:
org.springframework.data.repository.CrudRepository<Message,,Integer> org.springframework.data.jpa.repository.JpaRepository<Message,,Integer> org.springframework.data.repository.ListCrudRepository<Message,,Integer> org.springframework.data.repository.ListPagingAndSortingRepository<Message,,Integer> org.springframework.data.repository.PagingAndSortingRepository<Message,,Integer> org.springframework.data.repository.query.QueryByExampleExecutor<Message>,org.springframework.data.repository.Repository<Message,Integer>
@Repository
public interface MessageRepository
extends org.springframework.data.jpa.repository.JpaRepository<Message,Integer>
Spring Data JPA repository for
Message entities.
Provides standard CRUD operations as well as custom query methods for retrieving conversations, counting unread messages, and marking messages as seen.
- Author:
- SportTrack Team
-
Method Summary
Modifier and TypeMethodDescriptionlongcountByRecipientIdAndSeenFalse(Integer recipientId) Counts the number of unseen (unread) messages for the specified recipient.findByInitiatorIdOrRecipientIdOrderBySentAtDesc(Integer initiatorId, Integer recipientId) Finds all messages in which the specified athlete is either the sender or the recipient, ordered by send time descending.findConversation(Integer firstId, Integer secondId) Retrieves the full conversation between two athletes, ordered by send time ascending.intmarkConversationAsSeen(Integer recipientId, Integer friendId) Marks all unseen messages sent by the specified friend to the specified recipient as seen.Methods inherited from interface org.springframework.data.repository.CrudRepository
count, delete, deleteAll, deleteAll, deleteAllById, deleteById, existsById, findById, saveMethods inherited from interface org.springframework.data.jpa.repository.JpaRepository
deleteAllByIdInBatch, deleteAllInBatch, deleteAllInBatch, deleteInBatch, findAll, findAll, flush, getById, getOne, getReferenceById, saveAllAndFlush, saveAndFlushMethods inherited from interface org.springframework.data.repository.ListCrudRepository
findAll, findAllById, saveAllMethods inherited from interface org.springframework.data.repository.ListPagingAndSortingRepository
findAllMethods inherited from interface org.springframework.data.repository.PagingAndSortingRepository
findAllMethods inherited from interface org.springframework.data.repository.query.QueryByExampleExecutor
count, exists, findAll, findBy, findOne
-
Method Details
-
findConversation
@Query("SELECT m FROM Message m WHERE (m.initiator.id = :firstId AND m.recipient.id = :secondId) OR (m.initiator.id = :secondId AND m.recipient.id = :firstId) ORDER BY m.sentAt ASC") List<Message> findConversation(@Param("firstId") Integer firstId, @Param("secondId") Integer secondId) Retrieves the full conversation between two athletes, ordered by send time ascending.- Parameters:
firstId- the identifier of the first athletesecondId- the identifier of the second athlete- Returns:
- a list of messages exchanged between the two athletes, oldest first
-
findByInitiatorIdOrRecipientIdOrderBySentAtDesc
List<Message> findByInitiatorIdOrRecipientIdOrderBySentAtDesc(Integer initiatorId, Integer recipientId) Finds all messages in which the specified athlete is either the sender or the recipient, ordered by send time descending.- Parameters:
initiatorId- the identifier to match against the sender fieldrecipientId- the identifier to match against the recipient field- Returns:
- a list of messages involving the athlete, newest first
-
markConversationAsSeen
@Modifying @Query("UPDATE Message m SET m.seen = true WHERE m.recipient.id = :recipientId AND m.initiator.id = :friendId AND m.seen = false") int markConversationAsSeen(@Param("recipientId") Integer recipientId, @Param("friendId") Integer friendId) Marks all unseen messages sent by the specified friend to the specified recipient as seen.- Parameters:
recipientId- the identifier of the message recipient (the current user)friendId- the identifier of the message sender (the friend)- Returns:
- the number of messages that were updated
-
countByRecipientIdAndSeenFalse
Counts the number of unseen (unread) messages for the specified recipient.- Parameters:
recipientId- the identifier of the message recipient- Returns:
- the count of unseen messages
-