Include overall number of results and other data in entpoints for retrieving all entities

Jens Pelzetter 2020-05-27 21:35:49 +02:00
parent 81232cd19a
commit c4dee6362c
2 changed files with 42 additions and 19 deletions

View File

@ -23,9 +23,11 @@ import org.libreccm.core.api.IdentifierExtractor;
import org.libreccm.core.api.JsonArrayCollector;
import java.net.URI;
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
@ -67,9 +69,6 @@ public class GroupsApi {
@Inject
private RoleRepository roleRepository;
@Inject
private UserManager userManager;
@Inject
private UserRepository userRepository;
@ -79,16 +78,27 @@ public class GroupsApi {
@AuthorizationRequired
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
@Transactional(Transactional.TxType.REQUIRED)
public JsonArray getGroups(
public JsonObject getGroups(
@QueryParam("limit") @DefaultValue("20") final int limit,
@QueryParam("offset") @DefaultValue("0") final int offset
) {
return groupRepository
.findAll(limit, offset)
.stream()
.map(Group::buildJson)
.map(JsonObjectBuilder::build)
.collect(new JsonArrayCollector());
final long count = groupRepository.countAll();
final List<Group> groups = groupRepository.findAll();
return Json
.createObjectBuilder()
.add("count", count)
.add("limit", limit)
.add("offset", offset)
.add(
"groups",
groups
.stream()
.map(Group::buildJson)
.map(JsonObjectBuilder::build)
.collect(new JsonArrayCollector())
)
.build();
}
@GET

View File

@ -24,9 +24,11 @@ import org.libreccm.core.api.IdentifierExtractor;
import org.libreccm.core.api.JsonArrayCollector;
import java.net.URI;
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
@ -90,16 +92,26 @@ public class UsersApi {
@AuthorizationRequired
@RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN)
@Transactional(Transactional.TxType.REQUIRED)
public JsonArray getUsers(
public JsonObject getUsers(
@QueryParam("limit") @DefaultValue("20") final int limit,
@QueryParam("offset") @DefaultValue("0") final int offset
) {
return userRepository
.findAll(limit, offset)
.stream()
.map(User::buildJson)
.map(JsonObjectBuilder::build)
.collect(new JsonArrayCollector());
final long count = userRepository.countAll();
final List<User> users = userRepository.findAll(limit, offset);
return Json
.createObjectBuilder()
.add("count", count)
.add("limit", limit)
.add("offset", offset)
.add(
"users",
users
.stream()
.map(User::toJson)
.collect(new JsonArrayCollector())
)
.build();
}
/**
@ -275,9 +287,10 @@ public class UsersApi {
}
/**
* Deletes a user.
*
* Deletes a user.
*
* @param userIdentifier The identifier of the user to delete.
*
* @return A 200 (OK) response if the user was deleted successfully.
*/
@DELETE