Implement base to read groups

This commit is contained in:
René Pfeuffer
2018-06-11 08:27:25 +02:00
parent fb27c0d40c
commit 470ebaff7e
9 changed files with 274 additions and 2 deletions

View File

@@ -0,0 +1,91 @@
package sonia.scm.api.v2.resources;
import com.google.inject.Inject;
import de.otto.edison.hal.Link;
import de.otto.edison.hal.Links;
import org.mapstruct.AfterMapping;
import org.mapstruct.Context;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.Qualifier;
import sonia.scm.PageResult;
import sonia.scm.group.Group;
import sonia.scm.group.GroupPermissions;
import sonia.scm.user.User;
import sonia.scm.util.AssertUtil;
import javax.ws.rs.core.UriInfo;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import static de.otto.edison.hal.Link.link;
import static de.otto.edison.hal.Links.linkingTo;
import static java.util.Arrays.asList;
@Mapper
public abstract class Group2GroupDtoMapper {
public abstract GroupDto groupToGroupDto(Group group, @Context UriInfo uriInfo);
@Inject
private User2UserDtoMapper user2UserDtoMapper;
@AfterMapping
void appendLinks(Group group, @MappingTarget GroupDto target, @Context UriInfo uriInfo) {
LinkBuilder groupLinkBuilder = new LinkBuilder(uriInfo, GroupV2Resource.class, GroupSubResource.class);
Links.Builder linksBuilder = linkingTo()
.self(groupLinkBuilder.method("getGroupSubResource").parameters(target.getName()).method("get").parameters().href());
if (GroupPermissions.delete(group).isPermitted()) {
linksBuilder
.single(link("delete", groupLinkBuilder.method("getGroupSubResource").parameters(target.getName()).method("delete").parameters().href()));
}
if (GroupPermissions.modify(group).isPermitted()) {
linksBuilder
.single(link("update", groupLinkBuilder.method("getGroupSubResource").parameters(target.getName()).method("update").parameters().href()));
}
target.add(linksBuilder.build());
}
@AfterMapping
void appendUserLinks(Group group, @MappingTarget GroupDto target, @Context UriInfo uriInfo) {
Links.Builder linksBuilder = linkingTo();
LinkBuilder userLinkBuilder = new LinkBuilder(uriInfo, UserV2Resource.class, UserSubResource.class);
group.getMembers().forEach(name -> linksBuilder.array(Link.link("users", userLinkBuilder.method("getUserSubResource").parameters(name).method("get").parameters().href())));
target.add(linksBuilder.build());
}
@AfterMapping
void embedUsers(Group group, @MappingTarget GroupDto target, @Context UriInfo uriInfo) {
List<UserDto> users = group.getMembers().stream().map(this::createUser).map(u -> user2UserDtoMapper.userToUserDto(u, uriInfo)).collect(Collectors.toList());
target.withEmbedded("users", users);
}
private User createUser(String ich) {
User user = new User(ich);
user.setCreationDate(0L);
return user;
}
@Mapping(target = "creationDate")
Instant mapTime(Long epochMilli) {
AssertUtil.assertIsNotNull(epochMilli);
return Instant.ofEpochMilli(epochMilli);
}
@Mapping(target = "lastModified")
Optional<Instant> mapOptionalTime(Long epochMilli) {
return Optional
.ofNullable(epochMilli)
.map(Instant::ofEpochMilli);
}
}

View File

@@ -0,0 +1,4 @@
package sonia.scm.api.v2.resources;
public class GroupCollectionResource {
}

View File

@@ -0,0 +1,31 @@
package sonia.scm.api.v2.resources;
import de.otto.edison.hal.HalRepresentation;
import de.otto.edison.hal.Links;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
@Data @Accessors @NoArgsConstructor
public class GroupDto extends HalRepresentation {
private Instant creationDate;
private String description;
private Optional<Instant> lastModified;
private String name;
private String type;
@Override
protected HalRepresentation add(Links links) {
return super.add(links);
}
@Override
protected HalRepresentation withEmbedded(String rel, List<? extends HalRepresentation> embeddedItems) {
return super.withEmbedded(rel, embeddedItems);
}
}

View File

@@ -0,0 +1,52 @@
package sonia.scm.api.v2.resources;
import sonia.scm.group.Group;
import sonia.scm.web.VndMediaType;
import javax.inject.Inject;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;
import static java.util.stream.Collectors.toList;
@Produces(VndMediaType.GROUP)
public class GroupSubResource {
private final Group2GroupDtoMapper groupToGroupDtoMapper;
@Inject
public GroupSubResource(Group2GroupDtoMapper groupToGroupDtoMapper) {
this.groupToGroupDtoMapper = groupToGroupDtoMapper;
}
@Path("")
@GET
public Response get(@Context Request request, @Context UriInfo uriInfo, @PathParam("id") String id) {
Group group = new Group("admin", "admin");
group.setCreationDate(System.currentTimeMillis());
group.setMembers(IntStream.range(1, 10).mapToObj(n -> "user" + n).collect(toList()));
return Response.ok(groupToGroupDtoMapper.groupToGroupDto(group, uriInfo)).build();
}
@Path("")
@DELETE
public Response delete(@PathParam("id") String id) {
throw new RuntimeException();
}
@Path("")
@PUT
public Response update(@PathParam("id") String id) {
throw new RuntimeException();
}
}

View File

@@ -0,0 +1,30 @@
package sonia.scm.api.v2.resources;
import com.google.inject.Inject;
import javax.ws.rs.Path;
@Path(GroupV2Resource.GROUPS_PATH_V2)
public class GroupV2Resource {
public static final String GROUPS_PATH_V2 = "v2/groups/";
private final GroupCollectionResource groupCollectionResource;
private final GroupSubResource groupSubResource;
@Inject
public GroupV2Resource(GroupCollectionResource groupCollectionResource, GroupSubResource groupSubResource) {
this.groupCollectionResource = groupCollectionResource;
this.groupSubResource = groupSubResource;
}
@Path("")
public GroupCollectionResource getGroupCollectionResource() {
return groupCollectionResource;
}
@Path("{id}")
public GroupSubResource getGroupSubResource() {
return groupSubResource;
}
}

View File

@@ -2,11 +2,14 @@ package sonia.scm.api.v2.resources;
import com.google.inject.AbstractModule;
import org.mapstruct.factory.Mappers;
import sonia.scm.group.Group;
public class MapperModule extends AbstractModule {
@Override
protected void configure() {
bind(UserDto2UserMapper.class).to(Mappers.getMapper(UserDto2UserMapper.class).getClass());
bind(User2UserDtoMapper.class).to(Mappers.getMapper(User2UserDtoMapper.class).getClass());
bind(Group2GroupDtoMapper.class).to(Mappers.getMapper(Group2GroupDtoMapper.class).getClass());
}
}