Class PollService

java.lang.Object
vaultWeb.services.PollService

@Service public class PollService extends Object
Service class responsible for managing polls within groups.

Provides functionalities to create, update, delete, retrieve, and vote on polls. It also converts Poll entities to PollResponseDto objects for API responses.

  • Constructor Details

    • PollService

      public PollService()
  • Method Details

    • createPoll

      public Poll createPoll(Group group, User author, PollRequestDto pollDto)
      Creates a new poll in the specified group by the given author.
      Parameters:
      group - the group in which the poll will be created
      author - the user who creates the poll
      pollDto - the data transfer object containing poll details
      Returns:
      the created Poll entity
      Throws:
      NotMemberException - if the author is not a member of the group
    • getPollsByGroup

      public List<Poll> getPollsByGroup(Long groupId, User currentUser)
      Retrieves all polls for a given group.
      Parameters:
      groupId - the ID of the group
      currentUser - the current user requesting the polls
      Returns:
      a list of polls in the group
      Throws:
      GroupNotFoundException - if the group does not exist
      NotMemberException - if the user is not a member of the group
    • toResponseDto

      public PollResponseDto toResponseDto(Poll poll)
      Converts a Poll entity to a PollResponseDto suitable for API responses.
      Parameters:
      poll - the Poll entity to convert
      Returns:
      a PollResponseDto representing the poll and its options
    • vote

      public void vote(Long groupId, Long pollId, Long optionId, User user)
      Allows a user to vote for a specific option in a poll within a group.
      Parameters:
      groupId - the ID of the group containing the poll
      pollId - the ID of the poll
      optionId - the ID of the option to vote for
      user - the user casting the vote
      Throws:
      GroupNotFoundException - if the group does not exist
      NotMemberException - if the user is not a member of the group
      PollNotFoundException - if the poll does not exist
      PollDoesNotBelongToGroupException - if the poll doesn't belong to the group
      PollOptionNotFoundException - if the poll option invalid
      AlreadyVotedException - if the user has already voted
    • updatePoll

      public Poll updatePoll(Long groupId, Long pollId, User user, PollRequestDto pollDto)
      Updates an existing poll authored by a user.
      Parameters:
      groupId - the ID of the group containing the poll
      pollId - the ID of the poll to update
      user - the author of the poll
      pollDto - the new poll data
      Returns:
      the updated Poll entity
      Throws:
      PollNotFoundException - if the poll does not exist
      UnauthorizedException - if the user is not the author
      PollDoesNotBelongToGroupException - if the poll doesn't belong to the group
    • deletePoll

      public void deletePoll(Long groupId, Long pollId, User user)
      Deletes a poll authored by a user.
      Parameters:
      groupId - the ID of the group containing the poll
      pollId - the ID of the poll to delete
      user - the author of the poll
      Throws:
      PollNotFoundException - if the poll does not exist
      UnauthorizedException - if the user is not the author
      PollDoesNotBelongToGroupException - if the poll doesn't belong to the group