Implement update and delete for repository

This commit is contained in:
René Pfeuffer
2018-07-05 10:42:09 +02:00
parent 68e196d576
commit c2effbe9c5
6 changed files with 113 additions and 6 deletions

View File

@@ -16,6 +16,7 @@ public class MapperModule extends AbstractModule {
bind(GroupCollectionToDtoMapper.class);
bind(RepositoryToRepositoryDtoMapper.class).to(Mappers.getMapper(RepositoryToRepositoryDtoMapper.class).getClass());
bind(RepositoryDtoToRepositoryMapper.class).to(Mappers.getMapper(RepositoryDtoToRepositoryMapper.class).getClass());
bind(UriInfoStore.class).in(ServletScopes.REQUEST);
}

View File

@@ -0,0 +1,18 @@
package sonia.scm.api.v2.resources;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import sonia.scm.repository.Repository;
@Mapper
public abstract class RepositoryDtoToRepositoryMapper {
@Mapping(target = "creationDate", ignore = true)
@Mapping(target = "lastModified", ignore = true)
@Mapping(target = "id", ignore = true)
@Mapping(target = "publicReadable", ignore = true)
@Mapping(target = "healthCheckFailures", ignore = true)
@Mapping(target = "permissions", ignore = true)
public abstract Repository map(RepositoryDto repositoryDto);
}

View File

@@ -21,6 +21,7 @@ import javax.ws.rs.core.Response;
public class RepositoryResource {
private final RepositoryToRepositoryDtoMapper repositoryToDtoMapper;
private final RepositoryDtoToRepositoryMapper dtoToRepositoryMapper;
private final RepositoryManager manager;
private final SingleResourceManagerAdapter<Repository, RepositoryDto, RepositoryException> adapter;
@@ -33,11 +34,12 @@ public class RepositoryResource {
@Inject
public RepositoryResource(
RepositoryToRepositoryDtoMapper repositoryToDtoMapper,
RepositoryManager manager,
RepositoryDtoToRepositoryMapper dtoToRepositoryMapper, RepositoryManager manager,
Provider<TagRootResource> tagRootResource,
Provider<BranchRootResource> branchRootResource,
Provider<ChangesetRootResource> changesetRootResource,
Provider<SourceRootResource> sourceRootResource, Provider<PermissionRootResource> permissionRootResource) {
this.dtoToRepositoryMapper = dtoToRepositoryMapper;
this.manager = manager;
this.repositoryToDtoMapper = repositoryToDtoMapper;
this.adapter = new SingleResourceManagerAdapter<>(manager);
@@ -65,14 +67,25 @@ public class RepositoryResource {
@DELETE
@Path("")
@StatusCodes({
@ResponseCode(code = 204, condition = "delete success or nothing to delete"),
@ResponseCode(code = 401, condition = "not authenticated / invalid credentials"),
@ResponseCode(code = 403, condition = "not authorized, the current user does not have the \"repository\" privilege"),
@ResponseCode(code = 500, condition = "internal server error")
})
@TypeHint(TypeHint.NO_CONTENT.class)
public Response delete(@PathParam("namespace") String namespace, @PathParam("name") String name) {
throw new UnsupportedOperationException();
return adapter.delete(() -> manager.getByNamespace(namespace, name));
}
@PUT
@Path("")
public Response update(@PathParam("namespace") String namespace, @PathParam("name") String name) {
throw new UnsupportedOperationException();
public Response update(@PathParam("namespace") String namespace, @PathParam("name") String name, RepositoryDto repositoryDto) {
return adapter.update(() -> manager.getByNamespace(namespace, name), existing -> {
Repository repository = dtoToRepositoryMapper.map(repositoryDto);
repository.setId(existing.getId());
return repository;
});
}
@Path("tags/")

View File

@@ -60,6 +60,11 @@ class SingleResourceManagerAdapter<MODEL_OBJECT extends ModelObject,
return update(getId(existingModelObject), changedModelObject);
}
public Response delete(Supplier<MODEL_OBJECT> reader) {
MODEL_OBJECT existingModelObject = reader.get();
return delete(existingModelObject.getId());
}
@Override
protected GenericEntity<Collection<MODEL_OBJECT>> createGenericEntity(Collection<MODEL_OBJECT> modelObjects) {
throw new UnsupportedOperationException();