Class GroupService

java.lang.Object
vaultWeb.services.GroupService

@Service public class GroupService extends Object
Service class for managing groups, including creating, updating, joining, leaving, and managing group members.
  • Constructor Details

    • GroupService

      public GroupService()
  • Method Details

    • getPublicGroups

      public List<Group> getPublicGroups()
      Retrieves all public groups.
      Returns:
      a list of all groups marked as public.
    • getGroupById

      public Optional<Group> getGroupById(Long id)
      Retrieves a group by its ID.
      Parameters:
      id - the ID of the group.
      Returns:
      an Optional containing the group if found.
    • createGroup

      public Group createGroup(GroupDto dto, User creator)
      Creates a new group with the given DTO and sets the creator as admin.
      Parameters:
      dto - the group data transfer object containing name, description, and visibility.
      creator - the user who creates the group.
      Returns:
      the newly created group.
    • updateGroup

      public Group updateGroup(Long id, GroupDto updatedGroup)
      Updates an existing group's details.
      Parameters:
      id - the ID of the group to update.
      updatedGroup - the DTO containing updated group information.
      Returns:
      the updated group.
      Throws:
      GroupNotFoundException - if no group exists with the given ID.
    • deleteGroup

      public void deleteGroup(Long id)
      Deletes a group by its ID.
      Parameters:
      id - the ID of the group to delete.
    • joinGroup

      public Group joinGroup(Long groupId, User currentUser)
      Adds a user to a group as a regular member.
      Parameters:
      groupId - the ID of the group to join.
      currentUser - the user who wants to join the group.
      Returns:
      the group the user joined.
      Throws:
      GroupNotFoundException - if no group exists with the given ID.
      AlreadyMemberException - if the user is already a member of the group.
    • leaveGroup

      public Group leaveGroup(Long groupId, User currentUser)
      Removes a user from a group.
      Parameters:
      groupId - the ID of the group to leave.
      currentUser - the user who wants to leave the group.
      Returns:
      the group the user left.
      Throws:
      GroupNotFoundException - if no group exists with the given ID.
      NotMemberException - if the user is not a member of the group.
    • getMembers

      public List<User> getMembers(Long groupId)
      Retrieves all members of a group.
      Parameters:
      groupId - the ID of the group.
      Returns:
      a list of users who are members of the group.
      Throws:
      GroupNotFoundException - if no group exists with the given ID.
    • removeMember

      public Group removeMember(Long groupId, Long userId)
      Removes a specific user from a group.

      If the user to remove is an admin, ensures that the group still has at least one admin remaining.

      Parameters:
      groupId - the ID of the group.
      userId - the ID of the user to remove.
      Returns:
      the group after removing the member.
      Throws:
      GroupNotFoundException - if no group exists with the given ID.
      UserNotFoundException - if no user exists with the given ID.
      NotMemberException - if the user is not a member of the group.
      LastAdminException - if the user is the last admin in the group.