Package vaultWeb.services
Class GroupService
java.lang.Object
vaultWeb.services.GroupService
Service class for managing groups, including creating, updating, joining, leaving, and managing
group members.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncreateGroup(GroupDto dto, User creator) Creates a new group with the given DTO and sets the creator as admin.voiddeleteGroup(Long id) Deletes a group by its ID.getGroupById(Long id) Retrieves a group by its ID.getMembers(Long groupId) Retrieves all members of a group.Retrieves all public groups.Adds a user to a group as a regular member.leaveGroup(Long groupId, User currentUser) Removes a user from a group.removeMember(Long groupId, Long userId) Removes a specific user from a group.updateGroup(Long id, GroupDto updatedGroup) Updates an existing group's details.
-
Constructor Details
-
GroupService
public GroupService()
-
-
Method Details
-
getPublicGroups
Retrieves all public groups.- Returns:
- a list of all groups marked as public.
-
getGroupById
Retrieves a group by its ID.- Parameters:
id- the ID of the group.- Returns:
- an Optional containing the group if found.
-
createGroup
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
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
Deletes a group by its ID.- Parameters:
id- the ID of the group to delete.
-
joinGroup
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
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
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
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.
-