Refactored node repository
This commit is contained in:
parent
2d0cdca3e7
commit
040a9d1fbe
@ -1,27 +1,24 @@
|
||||
package ru.dragonestia.picker.api.repository;
|
||||
|
||||
import ru.dragonestia.picker.api.exception.InvalidNodeIdentifierException;
|
||||
import ru.dragonestia.picker.api.exception.NodeAlreadyExistException;
|
||||
import ru.dragonestia.picker.api.model.node.NodeDetails;
|
||||
import ru.dragonestia.picker.api.repository.response.type.RNode;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import ru.dragonestia.picker.api.model.node.INode;
|
||||
import ru.dragonestia.picker.api.model.node.NodeDefinition;
|
||||
import ru.dragonestia.picker.api.repository.request.node.FindNodeById;
|
||||
import ru.dragonestia.picker.api.repository.request.node.GetAllNodes;
|
||||
import ru.dragonestia.picker.api.repository.request.node.RemoveNodesByIds;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
public interface NodeRepository {
|
||||
|
||||
Set<NodeDetails> ALL_DETAILS = Set.of(NodeDetails.PERSIST);
|
||||
@NotNull List<INode> allNodes(@NotNull GetAllNodes request);
|
||||
|
||||
void register(RNode node, boolean persist) throws InvalidNodeIdentifierException, NodeAlreadyExistException;
|
||||
@NotNull Optional<INode> findNodeById(@NotNull FindNodeById request);
|
||||
|
||||
default List<RNode> all() {
|
||||
return all(Set.of());
|
||||
}
|
||||
void removeNodesById(@NotNull RemoveNodesByIds removeNodesByIds);
|
||||
|
||||
List<RNode> all(Set<NodeDetails> details);
|
||||
void removeNode(@NotNull INode node);
|
||||
|
||||
Optional<RNode> find(String nodeId);
|
||||
|
||||
void remove(String nodeId);
|
||||
void saveNode(@NotNull NodeDefinition definition);
|
||||
}
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
package ru.dragonestia.picker.api.repository;
|
||||
|
||||
import ru.dragonestia.picker.api.exception.*;
|
||||
import ru.dragonestia.picker.api.model.node.ResponseNode;
|
||||
import ru.dragonestia.picker.api.model.room.RoomDetails;
|
||||
import ru.dragonestia.picker.api.repository.response.type.RNode;
|
||||
import ru.dragonestia.picker.api.repository.response.type.RRoom;
|
||||
|
||||
import java.util.List;
|
||||
@ -17,15 +17,15 @@ public interface RoomRepository {
|
||||
|
||||
void remove(RRoom room) throws NodeNotFoundException;
|
||||
|
||||
void remove(RNode node, RRoom.Short room) throws NodeNotFoundException;
|
||||
void remove(ResponseNode node, RRoom.Short room) throws NodeNotFoundException;
|
||||
|
||||
default List<RRoom.Short> all(RNode node) throws NodeNotFoundException {
|
||||
default List<RRoom.Short> all(ResponseNode node) throws NodeNotFoundException {
|
||||
return all(node, Set.of());
|
||||
}
|
||||
|
||||
List<RRoom.Short> all(RNode node, Set<RoomDetails> details) throws NodeNotFoundException;
|
||||
List<RRoom.Short> all(ResponseNode node, Set<RoomDetails> details) throws NodeNotFoundException;
|
||||
|
||||
Optional<RRoom> find(RNode node, String roomId) throws NodeNotFoundException;
|
||||
Optional<RRoom> find(ResponseNode node, String roomId) throws NodeNotFoundException;
|
||||
|
||||
void lock(RRoom room, boolean value) throws NodeNotFoundException, RoomNotFoundException;
|
||||
}
|
||||
|
||||
@ -0,0 +1,82 @@
|
||||
package ru.dragonestia.picker.api.repository.request.node;
|
||||
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import ru.dragonestia.picker.api.model.node.NodeDetails;
|
||||
import ru.dragonestia.picker.api.repository.type.NodeIdentifier;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class FindNodeById {
|
||||
|
||||
private final String id;
|
||||
private final Set<NodeDetails> details;
|
||||
|
||||
private FindNodeById(String id, Set<NodeDetails> details) {
|
||||
this.id = id;
|
||||
this.details = details;
|
||||
}
|
||||
|
||||
public @NotNull String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public @NotNull Set<NodeDetails> getDetails() {
|
||||
return details;
|
||||
}
|
||||
|
||||
@Contract("_ -> new")
|
||||
public static @NotNull FindNodeById justFind(@NotNull NodeIdentifier identifier) {
|
||||
return FindNodeById.builder().setId(identifier.getValue()).build();
|
||||
}
|
||||
|
||||
@Contract("_ -> new")
|
||||
public static @NotNull FindNodeById findWithAllDetails(@NotNull NodeIdentifier identifier) {
|
||||
return FindNodeById.builder()
|
||||
.setId(identifier.getValue())
|
||||
.setDetails(Stream.of(NodeDetails.values()).collect(Collectors.toSet()))
|
||||
.build();
|
||||
}
|
||||
|
||||
public static @NotNull Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
|
||||
private String id = null;
|
||||
private Set<NodeDetails> details = new HashSet<>();
|
||||
|
||||
private Builder() {}
|
||||
|
||||
@Contract("_ -> this")
|
||||
public @NotNull Builder setId(@NotNull String id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Contract("_ -> this")
|
||||
public @NotNull Builder setDetails(@NotNull Set<NodeDetails> details) {
|
||||
this.details = details;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Contract("_ -> this")
|
||||
public @NotNull Builder appendDetail(@NotNull NodeDetails detail) {
|
||||
this.details.add(detail);
|
||||
return this;
|
||||
}
|
||||
|
||||
public @NotNull FindNodeById build() {
|
||||
if (id == null) {
|
||||
throw new NullPointerException("Id is null");
|
||||
}
|
||||
|
||||
return new FindNodeById(id, Collections.unmodifiableSet(details));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,55 @@
|
||||
package ru.dragonestia.picker.api.repository.request.node;
|
||||
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import ru.dragonestia.picker.api.model.node.NodeDetails;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class GetAllNodes {
|
||||
|
||||
public static GetAllNodes JUST = GetAllNodes.builder().build();
|
||||
public static GetAllNodes WITH_ALL_DETAILS = GetAllNodes.builder()
|
||||
.setDetails(Stream.of(NodeDetails.values()).collect(Collectors.toSet())).build();
|
||||
|
||||
private final Set<NodeDetails> details;
|
||||
|
||||
private GetAllNodes(Set<NodeDetails> details) {
|
||||
this.details = details;
|
||||
}
|
||||
|
||||
public @NotNull Set<NodeDetails> getDetails() {
|
||||
return details;
|
||||
}
|
||||
|
||||
public static @NotNull Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
|
||||
private Set<NodeDetails> details = new HashSet<>();
|
||||
|
||||
private Builder() {}
|
||||
|
||||
@Contract("_ -> this")
|
||||
public @NotNull Builder setDetails(@NotNull Set<NodeDetails> details) {
|
||||
this.details = details;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Contract("_ -> this")
|
||||
public @NotNull Builder appendDetail(@NotNull NodeDetails detail) {
|
||||
this.details.add(detail);
|
||||
return this;
|
||||
}
|
||||
|
||||
public @NotNull GetAllNodes build() {
|
||||
return new GetAllNodes(Collections.unmodifiableSet(details));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,54 @@
|
||||
package ru.dragonestia.picker.api.repository.request.node;
|
||||
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import ru.dragonestia.picker.api.repository.type.NodeIdentifier;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class RemoveNodesByIds {
|
||||
|
||||
private final Set<String> nodeIds;
|
||||
|
||||
private RemoveNodesByIds(Set<String> nodeIds) {
|
||||
this.nodeIds = nodeIds;
|
||||
}
|
||||
|
||||
public @NotNull Set<String> getNodeIds() {
|
||||
return nodeIds;
|
||||
}
|
||||
|
||||
@Contract("_ -> new")
|
||||
public static @NotNull RemoveNodesByIds just(@NotNull NodeIdentifier nodeIdentifier) {
|
||||
return RemoveNodesByIds.builder().appendNodeId(nodeIdentifier).build();
|
||||
}
|
||||
|
||||
public static @NotNull Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
|
||||
private Set<NodeIdentifier> nodeIds = new HashSet<>();
|
||||
|
||||
private Builder() {}
|
||||
|
||||
@Contract("_ -> this")
|
||||
public @NotNull Builder setNodeIds(@NotNull HashSet<NodeIdentifier> nodeIds) {
|
||||
this.nodeIds = nodeIds;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Contract("_ -> this")
|
||||
public @NotNull Builder appendNodeId(@NotNull NodeIdentifier nodeId) {
|
||||
nodeIds.add(nodeId);
|
||||
return this;
|
||||
}
|
||||
|
||||
public @NotNull RemoveNodesByIds build() {
|
||||
return new RemoveNodesByIds(nodeIds.stream().map(obj -> obj.getValue()).collect(Collectors.toSet()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -2,7 +2,6 @@ package ru.dragonestia.picker.api.repository.response;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import ru.dragonestia.picker.api.model.node.ResponseNode;
|
||||
import ru.dragonestia.picker.api.repository.response.type.RNode;
|
||||
|
||||
@Schema(title = "Node details", hidden = true)
|
||||
public record NodeDetailsResponse(ResponseNode node) {}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user