9
9
import jakarta .ws .rs .QueryParam ;
10
10
import jakarta .ws .rs .core .MediaType ;
11
11
import org .cryptomator .hub .entities .Authority ;
12
+ import org .cryptomator .hub .entities .Group ;
13
+ import org .cryptomator .hub .entities .User ;
12
14
import org .eclipse .microprofile .openapi .annotations .Operation ;
13
15
import org .eclipse .microprofile .openapi .annotations .responses .APIResponse ;
14
16
import org .jboss .resteasy .reactive .NoCache ;
@@ -21,6 +23,9 @@ public class AuthorityResource {
21
23
22
24
@ Inject
23
25
Authority .Repository authorityRepo ;
26
+
27
+ @ Inject
28
+ User .Repository userRepo ; // UserRepo wird hier für die Gruppen-Mitgliederanzahl benötigt
24
29
25
30
@ GET
26
31
@ Path ("/search" )
@@ -31,7 +36,7 @@ public class AuthorityResource {
31
36
public List <AuthorityDto > search (@ QueryParam ("query" ) @ NotBlank String query ) {
32
37
List <Authority > authorities = authorityRepo .byName (query ).toList ();
33
38
return authorities .stream ()
34
- .map (AuthorityDto :: fromEntity )
39
+ .map (this :: convertToDto ) // Neue Methode für die Konvertierung mit Member-Anzahl
35
40
.toList ();
36
41
}
37
42
@@ -43,7 +48,24 @@ public List<AuthorityDto> search(@QueryParam("query") @NotBlank String query) {
43
48
@ Operation (summary = "lists all authorities matching the given ids" , description = "lists for each id in the list its corresponding authority. Ignores all id's where an authority cannot be found" )
44
49
@ APIResponse (responseCode = "200" )
45
50
public List <AuthorityDto > getSome (@ QueryParam ("ids" ) List <String > authorityIds ) {
46
- return authorityRepo .findAllInList (authorityIds ).map (AuthorityDto ::fromEntity ).toList ();
51
+ return authorityRepo .findAllInList (authorityIds )
52
+ .map (this ::convertToDto ) // Neue Methode wird hier genutzt
53
+ .toList ();
54
+ }
55
+
56
+ /**
57
+ * Konvertiert eine Authority-Entity in das passende DTO,
58
+ * inklusive der Gruppen-Mitgliederanzahl für Gruppen.
59
+ */
60
+ private AuthorityDto convertToDto (Authority a ) {
61
+ if (a instanceof User u ) {
62
+ return UserDto .justPublicInfo (u );
63
+ } else if (a instanceof Group g ) {
64
+ int memberCount = (int ) userRepo .getEffectiveGroupUsers (g .getId ()).count ();
65
+ return new GroupDto (g .getId (), g .getName (), memberCount );
66
+ } else {
67
+ throw new IllegalStateException ("authority is not of type user or group" );
68
+ }
47
69
}
48
70
49
- }
71
+ }
0 commit comments