19
19
import bio .overture .ego .model .entity .Group ;
20
20
import bio .overture .ego .model .entity .GroupPermission ;
21
21
import bio .overture .ego .model .enums .AccessLevel ;
22
+ import bio .overture .ego .model .exceptions .NotFoundException ;
22
23
import bio .overture .ego .model .exceptions .PostWithIdentifierException ;
23
24
import bio .overture .ego .model .params .PolicyIdStringWithAccessLevel ;
24
25
import bio .overture .ego .model .search .SearchFilter ;
26
+ import bio .overture .ego .repository .ApplicationRepository ;
25
27
import bio .overture .ego .repository .GroupRepository ;
28
+ import bio .overture .ego .repository .UserRepository ;
26
29
import bio .overture .ego .repository .queryspecification .GroupSpecification ;
27
30
import com .google .common .collect .ImmutableList ;
28
31
import lombok .NonNull ;
44
47
public class GroupService extends AbstractNamedService <Group , UUID > {
45
48
46
49
private final GroupRepository groupRepository ;
50
+ private final UserRepository userRepository ;
51
+ private final ApplicationRepository applicationRepository ;
47
52
private final ApplicationService applicationService ;
48
53
private final PolicyService policyService ;
49
54
private final GroupPermissionService permissionService ;
50
55
51
56
@ Autowired
52
57
public GroupService (
53
58
@ NonNull GroupRepository groupRepository ,
54
- @ NonNull ApplicationService applicationService ,
59
+ @ NonNull UserRepository userRepository ,
60
+ @ NonNull ApplicationRepository applicationRepository ,
55
61
@ NonNull PolicyService policyService ,
62
+ @ NonNull ApplicationService applicationService ,
56
63
@ NonNull GroupPermissionService permissionService ) {
57
64
super (Group .class , groupRepository );
65
+ this .groupRepository = groupRepository ;
66
+ this .userRepository = userRepository ;
67
+ this .applicationRepository = applicationRepository ;
58
68
this .applicationService = applicationService ;
59
69
this .policyService = policyService ;
60
70
this .permissionService = permissionService ;
61
- this .groupRepository = groupRepository ;
62
71
}
63
72
64
73
public Group create (@ NonNull Group groupInfo ) {
@@ -79,6 +88,17 @@ public Group addAppsToGroup(@NonNull String grpId, @NonNull List<String> appIDs)
79
88
return getRepository ().save (group );
80
89
}
81
90
91
+ public Group addUsersToGroup (@ NonNull String grpId , @ NonNull List <String > userIds ) {
92
+ val group = getById (fromString (grpId ));
93
+ userIds .forEach (
94
+ userId -> {
95
+ val user = userRepository .findById (fromString (userId )).orElseThrow (() -> new NotFoundException (String .format ("Could not find User with ID: %s" , userId )));
96
+ group .getUsers ().add (user );
97
+ user .getGroups ().add (group );
98
+ });
99
+ return groupRepository .save (group );
100
+ }
101
+
82
102
public Group addGroupPermissions (
83
103
@ NonNull String groupId , @ NonNull List <PolicyIdStringWithAccessLevel > permissions ) {
84
104
val group = getById (fromString (groupId ));
@@ -98,7 +118,19 @@ public Group get(@NonNull String groupId) {
98
118
}
99
119
100
120
public Group update (@ NonNull Group other ) {
101
- return groupRepository .save (other );
121
+ val existingGroup = getById (other .getId ());
122
+
123
+ val updatedGroup =
124
+ Group .builder ()
125
+ .id (existingGroup .getId ())
126
+ .name (other .getName ())
127
+ .description (other .getDescription ())
128
+ .status (other .getStatus ())
129
+ .applications (existingGroup .getApplications ())
130
+ .users (existingGroup .getUsers ())
131
+ .build ();;
132
+
133
+ return groupRepository .save (updatedGroup );
102
134
}
103
135
104
136
// TODO - this was the original update - will use an improved version of this for the PATCH
@@ -191,8 +223,9 @@ public void deleteAppsFromGroup(@NonNull String grpId, @NonNull List<String> app
191
223
// TODO - Properly handle invalid IDs here
192
224
appIDs .forEach (
193
225
appId -> {
194
- // TODO if app id not valid (does not exist) we need to throw EntityNotFoundException
195
- group .getApplications ().remove (applicationService .get (appId ));
226
+ val app = applicationRepository .findById (fromString (appId )).orElseThrow (() -> new NotFoundException (String .format ("Could not find Application with ID: %s" , appId )));
227
+ group .getApplications ().remove (app );
228
+ app .getGroups ().remove (group );
196
229
});
197
230
groupRepository .save (group );
198
231
}
0 commit comments