Add methods to count members of an entity and members by type

This commit is contained in:
Konstantinos Triantafyllou 2021-09-09 19:25:44 +03:00
parent be31b7c90b
commit 9b3d6f2fbb
1 changed files with 27 additions and 0 deletions

View File

@ -20,6 +20,7 @@ import org.springframework.web.client.HttpClientErrorException;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
@RestController
@RequestMapping("/member")
@ -68,6 +69,32 @@ public class MemberController {
throw new ResourceNotFoundException("Role has not been found");
}
/**
* Get the number of the members of a type(Community, etc.) with id(ee, egi, etc.)
*/
@RequestMapping(value = "/{type}/{id}/count", method = RequestMethod.GET)
public ResponseEntity<Integer> getCount(@PathVariable("type") String type, @PathVariable("id") String id) {
Integer couId = registryService.getCouId(AuthoritiesUtils.memberRole(type, id));
if (couId != null) {
JsonArray users = registryService.getUserIdByCouId(couId, false);
return ResponseEntity.ok((users.size()));
}
throw new ResourceNotFoundException("Role has not been found");
}
/**
* Get the number of the members of a type(Community, etc.)
*/
@RequestMapping(value = "/{type}/count", method = RequestMethod.GET)
public ResponseEntity<Integer> getCountByType(@PathVariable("type") String type) {
JsonArray cous = registryService.getCous(type + ".");
AtomicInteger counter = new AtomicInteger(0);
cous.forEach(cou -> {
counter.addAndGet(registryService.getUserIdByCouId(cou.getAsJsonObject().get("Id").getAsInt(), false).size());
});
return ResponseEntity.ok(counter.get());
}
/**
* Assign member role to logged in user or user with @email
* If role doesn't exists, use force=true to create and assign the role