Class FriendshipService

java.lang.Object
fr.utc.miage.sporttrack.service.user.communication.FriendshipService

@Service public class FriendshipService extends Object
Service layer component responsible for managing Friendship relationships between athletes within the SportTrack application.

Provides business logic for sending, accepting, and rejecting friend requests, blocking and unblocking users, querying friends and blocked users, and determining the relationship status between two athletes.

Author:
SportTrack Team
  • Constructor Details

    • FriendshipService

      public FriendshipService(FriendshipRepository friendshipRepository, AthleteRepository athleteRepository)
      Constructs a FriendshipService without notification support.
      Parameters:
      friendshipRepository - the friendship repository
      athleteRepository - the athlete repository
    • FriendshipService

      @Autowired public FriendshipService(FriendshipRepository friendshipRepository, AthleteRepository athleteRepository, NotificationService notificationService)
      Constructs a FriendshipService with full notification support.
      Parameters:
      friendshipRepository - the friendship repository
      athleteRepository - the athlete repository
      notificationService - the notification service for friendship events
  • Method Details

    • sendFriendRequest

      @Transactional public void sendFriendRequest(Integer initiatorId, Integer recipientId)
      Sends a friend request from one user to another. Checks for block relationships before allowing the request.
    • acceptFriendRequest

      @Transactional public void acceptFriendRequest(Integer friendshipId, Integer currentUserId)
      Accept a pending friend request.
    • rejectFriendRequest

      @Transactional public void rejectFriendRequest(Integer friendshipId, Integer currentUserId)
      Rejects a pending friend request.
    • removeFriend

      @Transactional public void removeFriend(Integer currentUserId, Integer otherUserId)
      Removes an established friendship between two users.
    • blockUser

      @Transactional public void blockUser(Integer blockerId, Integer blockedId)
      Blocks a user. Handles all existing relationship states: - If already friends: removes friendship and blocks. - If pending request: cancels it and blocks. - If rejected: blocks. - If already blocked by blocker: throws exception. - If no relationship: creates a new BLOCKED record.
      Parameters:
      blockerId - the user performing the block
      blockedId - the user being blocked
    • unblockUser

      @Transactional public void unblockUser(Integer blockerId, Integer blockedId)
      Unblocks a previously blocked user. Only the user who initiated the block can unblock.
      Parameters:
      blockerId - the user who performed the block
      blockedId - the user to unblock
    • getFriendsOfAthlete

      public List<Athlete> getFriendsOfAthlete(Integer athleteId)
      Returns a list of all accepted friends for a specific athlete.
    • getPendingRequestsForAthlete

      public List<Friendship> getPendingRequestsForAthlete(Integer athleteId)
      Returns all pending friend requests waiting for the athlete to accept.
    • getSentPendingRequests

      public List<Friendship> getSentPendingRequests(Integer athleteId)
      Returns all friend requests sent by the athlete that are still pending.
    • getBlockedUsers

      public List<Friendship> getBlockedUsers(Integer athleteId)
      Returns all users blocked by the given athlete.
    • getRelationshipStatus

      public RelationshipStatusDTO getRelationshipStatus(Integer viewerId, Integer targetId)
      Computes the relationship status from the perspective of viewerId looking at targetId. Returns a RelationshipStatusDTO that the frontend can use to determine button display.
      Parameters:
      viewerId - the current user
      targetId - the user being viewed
      Returns:
      the computed relationship status
    • searchVisibleAthletes

      public List<Athlete> searchVisibleAthletes(Integer currentUserId, String keyword)
      Searches for athletes visible to the current user. Filters out: - The current user themselves - Users the current user has blocked - Users who have blocked the current user
      Parameters:
      currentUserId - the ID of the user performing the search
      keyword - the search keyword (username)
      Returns:
      list of visible athletes matching the keyword