mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 16:35:45 +01:00
Get group collections
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import de.otto.edison.hal.Embedded;
|
||||
import de.otto.edison.hal.HalRepresentation;
|
||||
import de.otto.edison.hal.Links;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class GroupCollectionDto extends HalRepresentation {
|
||||
|
||||
private int page;
|
||||
private int pageTotal;
|
||||
|
||||
public GroupCollectionDto(Links links, Embedded embedded) {
|
||||
super(links, embedded);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import com.webcohesion.enunciate.metadata.rs.ResponseCode;
|
||||
import com.webcohesion.enunciate.metadata.rs.ResponseHeader;
|
||||
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
|
||||
import com.webcohesion.enunciate.metadata.rs.TypeHint;
|
||||
import sonia.scm.PageResult;
|
||||
import sonia.scm.api.rest.resources.AbstractManagerResource;
|
||||
import sonia.scm.group.Group;
|
||||
import sonia.scm.group.GroupException;
|
||||
@@ -12,11 +13,15 @@ import sonia.scm.group.GroupManager;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.DefaultValue;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.GenericEntity;
|
||||
import javax.ws.rs.core.Request;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
import java.io.IOException;
|
||||
@@ -38,7 +43,6 @@ public class GroupCollectionResource extends AbstractManagerResource<Group, Grou
|
||||
this.groupToDtoMapper = groupToDtoMapper;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new group.
|
||||
* @param groupDto The group to be created.
|
||||
@@ -61,6 +65,36 @@ public class GroupCollectionResource extends AbstractManagerResource<Group, Grou
|
||||
return Response.created(URI.create(group(uriInfo).self(group.getName()))).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all groups for a given page number with a given page size (default page size is {@value DEFAULT_PAGE_SIZE}).
|
||||
* <strong>Note:</strong> This method requires admin privileges.
|
||||
*
|
||||
* @param request the current request
|
||||
* @param page the number of the requested page
|
||||
* @param pageSize the page size (default page size is {@value DEFAULT_PAGE_SIZE})
|
||||
* @param sortby sort parameter
|
||||
* @param desc sort direction desc or aesc
|
||||
* @return
|
||||
*/
|
||||
@GET
|
||||
@Path("")
|
||||
@TypeHint(GroupDto[].class)
|
||||
@StatusCodes({
|
||||
@ResponseCode(code = 200, condition = "success"),
|
||||
@ResponseCode(code = 403, condition = "forbidden, the current user has no admin privileges"),
|
||||
@ResponseCode(code = 500, condition = "internal server error")
|
||||
})
|
||||
public Response getAll(@Context Request request, @Context UriInfo uriInfo,
|
||||
@DefaultValue("0") @QueryParam("page") int page,
|
||||
@DefaultValue("" + DEFAULT_PAGE_SIZE) @QueryParam("pageSize") int pageSize,
|
||||
@QueryParam("sortby") String sortby,
|
||||
@DefaultValue("false")
|
||||
@QueryParam("desc") boolean desc) {
|
||||
PageResult<Group> pageResult = fetchPage(sortby, desc, page, pageSize);
|
||||
|
||||
return Response.ok(new GroupCollectionToDtoMapper(groupToDtoMapper).map(uriInfo, page, pageSize, pageResult)).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GenericEntity<Collection<Group>> createGenericEntity(Collection<Group> items) {
|
||||
throw new UnsupportedOperationException();
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import de.otto.edison.hal.Embedded;
|
||||
import de.otto.edison.hal.Links;
|
||||
import de.otto.edison.hal.paging.NumberedPaging;
|
||||
import de.otto.edison.hal.paging.PagingRel;
|
||||
import sonia.scm.PageResult;
|
||||
import sonia.scm.group.Group;
|
||||
import sonia.scm.group.GroupPermissions;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.damnhandy.uri.template.UriTemplate.fromTemplate;
|
||||
import static de.otto.edison.hal.Embedded.embeddedBuilder;
|
||||
import static de.otto.edison.hal.Link.link;
|
||||
import static de.otto.edison.hal.Links.linkingTo;
|
||||
import static de.otto.edison.hal.paging.NumberedPaging.zeroBasedNumberedPaging;
|
||||
import static sonia.scm.api.v2.resources.ResourceLinks.groupCollection;
|
||||
|
||||
public class GroupCollectionToDtoMapper {
|
||||
|
||||
private final GroupToGroupDtoMapper groupToDtoMapper;
|
||||
|
||||
@Inject
|
||||
public GroupCollectionToDtoMapper(GroupToGroupDtoMapper groupToDtoMapper) {
|
||||
this.groupToDtoMapper = groupToDtoMapper;
|
||||
}
|
||||
|
||||
public GroupCollectionDto map(UriInfo uriInfo, int pageNumber, int pageSize, PageResult<Group> pageResult) {
|
||||
NumberedPaging paging = zeroBasedNumberedPaging(pageNumber, pageSize, pageResult.hasMore());
|
||||
List<GroupDto> dtos = pageResult.getEntities().stream().map(user -> groupToDtoMapper.map(user, uriInfo)).collect(Collectors.toList());
|
||||
|
||||
GroupCollectionDto groupCollectionDto = new GroupCollectionDto(
|
||||
createLinks(uriInfo, paging),
|
||||
embedDtos(dtos)
|
||||
);
|
||||
groupCollectionDto.setPage(pageNumber);
|
||||
return groupCollectionDto;
|
||||
}
|
||||
|
||||
private static Links createLinks(UriInfo uriInfo, NumberedPaging page) {
|
||||
String baseUrl = groupCollection(uriInfo).self();
|
||||
|
||||
Links.Builder linksBuilder = linkingTo()
|
||||
.with(page.links(
|
||||
fromTemplate(baseUrl + "{?page,pageSize}"),
|
||||
EnumSet.allOf(PagingRel.class)));
|
||||
if (GroupPermissions.create().isPermitted()) {
|
||||
linksBuilder.single(link("create", groupCollection(uriInfo).create()));
|
||||
}
|
||||
return linksBuilder.build();
|
||||
}
|
||||
|
||||
private Embedded embedDtos(List<GroupDto> dtos) {
|
||||
return embeddedBuilder()
|
||||
.with("groups", dtos)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,26 @@ class ResourceLinks {
|
||||
}
|
||||
}
|
||||
|
||||
static GroupCollectionLinks groupCollection(UriInfo uriInfo) {
|
||||
return new GroupCollectionLinks(uriInfo);
|
||||
}
|
||||
|
||||
static class GroupCollectionLinks {
|
||||
private final LinkBuilder collectionLinkBuilder;
|
||||
|
||||
private GroupCollectionLinks(UriInfo uriInfo) {
|
||||
collectionLinkBuilder = new LinkBuilder(uriInfo, GroupV2Resource.class, GroupCollectionResource.class);
|
||||
}
|
||||
|
||||
String self() {
|
||||
return collectionLinkBuilder.method("getGroupCollectionResource").parameters().method("getAll").parameters().href();
|
||||
}
|
||||
|
||||
String create() {
|
||||
return collectionLinkBuilder.method("getGroupCollectionResource").parameters().method("create").parameters().href();
|
||||
}
|
||||
}
|
||||
|
||||
static UserLinks user(UriInfo uriInfo) {
|
||||
return new UserLinks(uriInfo);
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ public class UserCollectionResource extends AbstractManagerResource<User, UserEx
|
||||
*/
|
||||
@GET
|
||||
@Path("")
|
||||
@TypeHint(User[].class)
|
||||
@TypeHint(UserDto[].class)
|
||||
@StatusCodes({
|
||||
@ResponseCode(code = 200, condition = "success"),
|
||||
@ResponseCode(code = 403, condition = "forbidden, the current user has no admin privileges"),
|
||||
|
||||
Reference in New Issue
Block a user