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

View File

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