mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 08:25:44 +01:00
Small cleanup
This commit is contained in:
@@ -8,10 +8,12 @@ import sonia.scm.PageResult;
|
|||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Predicate;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Facade for {@link SingleResourceManagerAdapter} and {@link CollectionResourceManagerAdapter}.
|
* Facade for {@link SingleResourceManagerAdapter} and {@link CollectionResourceManagerAdapter}
|
||||||
|
* for model objects handled by a single id.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("squid:S00119") // "MODEL_OBJECT" is much more meaningful than "M", right?
|
@SuppressWarnings("squid:S00119") // "MODEL_OBJECT" is much more meaningful than "M", right?
|
||||||
class IdResourceManagerAdapter<MODEL_OBJECT extends ModelObject,
|
class IdResourceManagerAdapter<MODEL_OBJECT extends ModelObject,
|
||||||
@@ -30,14 +32,14 @@ class IdResourceManagerAdapter<MODEL_OBJECT extends ModelObject,
|
|||||||
}
|
}
|
||||||
|
|
||||||
Response get(String id, Function<MODEL_OBJECT, DTO> mapToDto) {
|
Response get(String id, Function<MODEL_OBJECT, DTO> mapToDto) {
|
||||||
return singleAdapter.get(() -> manager.get(id), mapToDto);
|
return singleAdapter.get(loadBy(id), mapToDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Response update(String id, Function<MODEL_OBJECT, MODEL_OBJECT> applyChanges) {
|
public Response update(String id, Function<MODEL_OBJECT, MODEL_OBJECT> applyChanges) {
|
||||||
return singleAdapter.update(
|
return singleAdapter.update(
|
||||||
() -> manager.get(id),
|
loadBy(id),
|
||||||
applyChanges,
|
applyChanges,
|
||||||
changed -> changed.getId().equals(id)
|
idStaysTheSame(id)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,4 +54,12 @@ class IdResourceManagerAdapter<MODEL_OBJECT extends ModelObject,
|
|||||||
public Response delete(String id) {
|
public Response delete(String id) {
|
||||||
return singleAdapter.delete(id);
|
return singleAdapter.delete(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Supplier<MODEL_OBJECT> loadBy(String id) {
|
||||||
|
return () -> manager.get(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Predicate<MODEL_OBJECT> idStaysTheSame(String id) {
|
||||||
|
return changed -> changed.getId().equals(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ import javax.ws.rs.Path;
|
|||||||
import javax.ws.rs.PathParam;
|
import javax.ws.rs.PathParam;
|
||||||
import javax.ws.rs.Produces;
|
import javax.ws.rs.Produces;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class RepositoryResource {
|
public class RepositoryResource {
|
||||||
|
|
||||||
@@ -63,7 +65,7 @@ public class RepositoryResource {
|
|||||||
@ResponseCode(code = 500, condition = "internal server error")
|
@ResponseCode(code = 500, condition = "internal server error")
|
||||||
})
|
})
|
||||||
public Response get(@PathParam("namespace") String namespace, @PathParam("name") String name) {
|
public Response get(@PathParam("namespace") String namespace, @PathParam("name") String name) {
|
||||||
return adapter.get(() -> manager.getByNamespace(namespace, name), repositoryToDtoMapper::map);
|
return adapter.get(loadBy(namespace, name), repositoryToDtoMapper::map);
|
||||||
}
|
}
|
||||||
|
|
||||||
@DELETE
|
@DELETE
|
||||||
@@ -76,7 +78,7 @@ public class RepositoryResource {
|
|||||||
})
|
})
|
||||||
@TypeHint(TypeHint.NO_CONTENT.class)
|
@TypeHint(TypeHint.NO_CONTENT.class)
|
||||||
public Response delete(@PathParam("namespace") String namespace, @PathParam("name") String name) {
|
public Response delete(@PathParam("namespace") String namespace, @PathParam("name") String name) {
|
||||||
return adapter.delete(() -> manager.getByNamespace(namespace, name));
|
return adapter.delete(loadBy(namespace, name));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PUT
|
@PUT
|
||||||
@@ -93,9 +95,9 @@ public class RepositoryResource {
|
|||||||
@TypeHint(TypeHint.NO_CONTENT.class)
|
@TypeHint(TypeHint.NO_CONTENT.class)
|
||||||
public Response update(@PathParam("namespace") String namespace, @PathParam("name") String name, RepositoryDto repositoryDto) {
|
public Response update(@PathParam("namespace") String namespace, @PathParam("name") String name, RepositoryDto repositoryDto) {
|
||||||
return adapter.update(
|
return adapter.update(
|
||||||
() -> manager.getByNamespace(namespace, name),
|
loadBy(namespace, name),
|
||||||
existing -> dtoToRepositoryMapper.map(repositoryDto, existing.getId()),
|
existing -> dtoToRepositoryMapper.map(repositoryDto, existing.getId()),
|
||||||
changed -> changed.getName().equals(name) && changed.getNamespace().equals(namespace)
|
nameAndNamespaceStaysTheSame(namespace, name)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,4 +125,12 @@ public class RepositoryResource {
|
|||||||
public PermissionRootResource permissions() {
|
public PermissionRootResource permissions() {
|
||||||
return permissionRootResource.get();
|
return permissionRootResource.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Supplier<Repository> loadBy(String namespace, String name) {
|
||||||
|
return () -> manager.getByNamespace(namespace, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Predicate<Repository> nameAndNamespaceStaysTheSame(String namespace, String name) {
|
||||||
|
return changed -> changed.getName().equals(name) && changed.getNamespace().equals(namespace);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user