Refactored RoomRepository

This commit is contained in:
Andrey Terentev 2024-03-12 12:24:45 +07:00 committed by Andrey Terentev
parent 8f29d69ecc
commit 1a2dcebb1a
9 changed files with 391 additions and 17 deletions

View File

@ -2,6 +2,9 @@ package ru.dragonestia.picker.api.model.room;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import ru.dragonestia.picker.api.repository.type.NodeIdentifier;
import ru.dragonestia.picker.api.repository.type.RoomIdentifier;
import ru.dragonestia.picker.api.repository.type.RoomPath;
public interface IRoom {
@ -11,6 +14,10 @@ public interface IRoom {
@NotNull String getNodeIdentifier();
default @NotNull RoomPath getPath() {
return new RoomPath(NodeIdentifier.of(getNodeIdentifier()), RoomIdentifier.of(getIdentifier()));
}
int getMaxSlots();
default boolean hasUnlimitedSlots() {

View File

@ -3,6 +3,7 @@ package ru.dragonestia.picker.api.model.room;
import io.swagger.v3.oas.annotations.media.Schema;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import ru.dragonestia.picker.api.repository.type.RoomPath;
import java.beans.Transient;
import java.util.HashMap;
@ -50,6 +51,12 @@ public class ResponseRoom implements IRoom {
return nodeId;
}
@Transient
@Override
public @NotNull RoomPath getPath() {
return IRoom.super.getPath();
}
@Override
public int getMaxSlots() {
return slots;

View File

@ -0,0 +1,87 @@
package ru.dragonestia.picker.api.model.room;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import ru.dragonestia.picker.api.repository.type.NodeIdentifier;
import ru.dragonestia.picker.api.repository.type.RoomIdentifier;
import java.security.InvalidParameterException;
public class RoomDefinition implements IRoom {
private final String id;
private final String nodeId;
private int slots = 5;
private boolean locked = false;
private boolean persist = false;
private String payload = "";
public RoomDefinition(@NotNull NodeIdentifier nodeIdentifier, @NotNull RoomIdentifier roomIdentifier) {
nodeId = nodeIdentifier.getValue();
id = roomIdentifier.getValue();
}
@Override
public @NotNull String getIdentifier() {
return id;
}
@Override
public @NotNull String getNodeIdentifier() {
return nodeId;
}
@Override
public int getMaxSlots() {
return slots;
}
@Contract("_ -> this")
public @NotNull RoomDefinition setMaxSlots(int value) {
if (value == 0 || value < -1) {
throw new InvalidParameterException("Slots cannot be negative");
}
slots = value;
return this;
}
@Override
public boolean isLocked() {
return locked;
}
@Contract("_ -> this")
public @NotNull RoomDefinition setLocked(boolean value) {
locked = value;
return this;
}
@Override
public @Nullable Boolean isPersist() {
return persist;
}
@Contract("_ -> this")
public @NotNull RoomDefinition setPersist(boolean value) {
persist = value;
return this;
}
@Override
public @Nullable String getPayload() {
return payload;
}
@Contract("_ -> this")
public @NotNull RoomDefinition setPayload(@NotNull String value) {
payload = value;
return this;
}
@Override
public @Nullable String getDetail(@NotNull RoomDetails detail) {
throw new UnsupportedOperationException();
}
}

View File

@ -3,6 +3,7 @@ package ru.dragonestia.picker.api.model.room;
import io.swagger.v3.oas.annotations.media.Schema;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import ru.dragonestia.picker.api.repository.type.RoomPath;
import java.beans.Transient;
import java.util.HashMap;
@ -46,6 +47,12 @@ public class ShortResponseRoom implements IRoom {
return nodeId;
}
@Transient
@Override
public @NotNull RoomPath getPath() {
return IRoom.super.getPath();
}
@Override
public int getMaxSlots() {
return slots;

View File

@ -1,32 +1,26 @@
package ru.dragonestia.picker.api.repository;
import ru.dragonestia.picker.api.exception.*;
import ru.dragonestia.picker.api.model.node.ResponseNode;
import org.jetbrains.annotations.NotNull;
import ru.dragonestia.picker.api.model.room.ResponseRoom;
import ru.dragonestia.picker.api.model.room.RoomDetails;
import ru.dragonestia.picker.api.model.room.RoomDefinition;
import ru.dragonestia.picker.api.model.room.ShortResponseRoom;
import ru.dragonestia.picker.api.repository.request.room.FindRoomById;
import ru.dragonestia.picker.api.repository.request.room.GetAllRooms;
import ru.dragonestia.picker.api.repository.request.room.RemoveRoomsByIds;
import ru.dragonestia.picker.api.repository.type.RoomPath;
import java.util.List;
import java.util.Optional;
import java.util.Set;
public interface RoomRepository {
Set<RoomDetails> ALL_DETAILS = Set.of(RoomDetails.COUNT_USERS, RoomDetails.PERSIST);
void saveRoom(@NotNull RoomDefinition definition);
void register(ResponseRoom room, boolean persist) throws NodeNotFoundException, InvalidRoomIdentifierException, RoomAlreadyExistException;
void removeRooms(@NotNull RemoveRoomsByIds request);
void remove(ResponseRoom room) throws NodeNotFoundException;
@NotNull List<ShortResponseRoom> allRooms(@NotNull GetAllRooms request);
void remove(ResponseNode node, ShortResponseRoom room) throws NodeNotFoundException;
@NotNull Optional<ResponseRoom> find(@NotNull FindRoomById request);
default List<ShortResponseRoom> all(ResponseNode node) throws NodeNotFoundException {
return all(node, Set.of());
}
List<ShortResponseRoom> all(ResponseNode node, Set<RoomDetails> details) throws NodeNotFoundException;
Optional<ResponseRoom> find(ResponseNode node, String roomId) throws NodeNotFoundException;
void lock(ResponseRoom room, boolean value) throws NodeNotFoundException, RoomNotFoundException;
void lockRoom(@NotNull RoomPath path, boolean value);
}

View File

@ -0,0 +1,103 @@
package ru.dragonestia.picker.api.repository.request.room;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import ru.dragonestia.picker.api.model.room.RoomDetails;
import ru.dragonestia.picker.api.repository.type.NodeIdentifier;
import ru.dragonestia.picker.api.repository.type.RoomIdentifier;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
public class FindRoomById {
private final String nodeId;
private final String id;
private final Set<RoomDetails> details;
private FindRoomById(String nodeId, String id, Set<RoomDetails> details) {
this.id = id;
this.nodeId = nodeId;
this.details = details;
}
public @NotNull String getNodeId() {
return nodeId;
}
public @NotNull String getId() {
return id;
}
public @NotNull Set<RoomDetails> getDetails() {
return details;
}
@Contract("_, _ -> new")
public static @NotNull FindRoomById just(@NotNull NodeIdentifier nodeId, @NotNull RoomIdentifier roomId) {
return FindRoomById.builder()
.setNodeId(nodeId)
.setRoomId(roomId)
.build();
}
@Contract("_, _ -> new")
public static @NotNull FindRoomById withAllDetails(@NotNull NodeIdentifier nodeId, @NotNull RoomIdentifier roomId) {
return FindRoomById.builder()
.setNodeId(nodeId)
.setRoomId(roomId)
.setDetails(Arrays.stream(RoomDetails.values()).collect(Collectors.toSet()))
.build();
}
public static @NotNull Builder builder() {
return new Builder();
}
public static class Builder {
private String nodeId = null;
private String roomId = null;
private Set<RoomDetails> details = new HashSet<>();
private Builder() {}
@Contract("_ -> this")
public @NotNull Builder setNodeId(@NotNull NodeIdentifier identifier) {
nodeId = identifier.getValue();
return this;
}
@Contract("_ -> this")
public @NotNull Builder setRoomId(@NotNull RoomIdentifier identifier) {
roomId = identifier.getValue();
return this;
}
@Contract("_ -> this")
public @NotNull Builder setDetails(@NotNull Set<RoomDetails> details) {
this.details = details;
return this;
}
@Contract("_ -> this")
public @NotNull Builder appendDetail(@NotNull RoomDetails detail) {
details.add(detail);
return this;
}
public @NotNull FindRoomById build() {
if (nodeId == null) {
throw new NullPointerException("Node id is null");
}
if (roomId == null) {
throw new NullPointerException("Room id is null");
}
return new FindRoomById(nodeId, roomId, Collections.unmodifiableSet(details));
}
}
}

View File

@ -0,0 +1,82 @@
package ru.dragonestia.picker.api.repository.request.room;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import ru.dragonestia.picker.api.model.room.RoomDetails;
import ru.dragonestia.picker.api.repository.type.NodeIdentifier;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
public class GetAllRooms {
private final String nodeId;
private final Set<RoomDetails> details;
private GetAllRooms(String nodeId, Set<RoomDetails> details) {
this.nodeId = nodeId;
this.details = details;
}
public @NotNull String getNodeId() {
return nodeId;
}
public @NotNull Set<RoomDetails> getDetails() {
return details;
}
@Contract("_ -> new")
public static @NotNull GetAllRooms just(@NotNull NodeIdentifier nodeIdentifier) {
return GetAllRooms.builder().setNodeId(nodeIdentifier).build();
}
@Contract("_ -> new")
public static @NotNull GetAllRooms withAllDetails(@NotNull NodeIdentifier nodeIdentifier) {
return GetAllRooms.builder()
.setNodeId(nodeIdentifier)
.setDetails(Arrays.stream(RoomDetails.values()).collect(Collectors.toSet()))
.build();
}
public static @NotNull Builder builder() {
return new Builder();
}
public static class Builder {
private String nodeId = null;
private Set<RoomDetails> details = new HashSet<>();
private Builder() {}
@Contract("_ -> this")
public @NotNull Builder setNodeId(@NotNull NodeIdentifier identifier) {
nodeId = identifier.getValue();
return this;
}
@Contract("_ -> this")
public @NotNull Builder setDetails(@NotNull Set<RoomDetails> details) {
this.details = details;
return this;
}
@Contract("_ -> this")
public @NotNull Builder appendDetail(@NotNull RoomDetails detail) {
details.add(detail);
return this;
}
public @NotNull GetAllRooms build() {
if (nodeId == null) {
throw new NullPointerException("Node id is null");
}
return new GetAllRooms(nodeId, Collections.unmodifiableSet(details));
}
}
}

View File

@ -0,0 +1,53 @@
package ru.dragonestia.picker.api.repository.request.room;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import ru.dragonestia.picker.api.repository.type.RoomPath;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class RemoveRoomsByIds {
private final Set<RoomPath> paths;
private RemoveRoomsByIds(Set<RoomPath> paths) {
this.paths = paths;
}
public @NotNull Set<RoomPath> getPaths() {
return paths;
}
public static @NotNull RemoveRoomsByIds just(@NotNull RoomPath path) {
return RemoveRoomsByIds.builder().appendPath(path).build();
}
public static @NotNull Builder builder() {
return new Builder();
}
public static class Builder {
private Set<RoomPath> paths = new HashSet<>();
private Builder() {}
@Contract("_ -> this")
public @NotNull Builder setPaths(@NotNull Set<RoomPath> paths) {
this.paths = paths;
return this;
}
@Contract("_ -> this")
public @NotNull Builder appendPath(@NotNull RoomPath path) {
paths.add(path);
return this;
}
public @NotNull RemoveRoomsByIds build() {
return new RemoveRoomsByIds(Collections.unmodifiableSet(paths));
}
}
}

View File

@ -0,0 +1,34 @@
package ru.dragonestia.picker.api.repository.type;
import org.jetbrains.annotations.NotNull;
public record RoomPath(@NotNull NodeIdentifier nodeIdentifier, @NotNull RoomIdentifier roomIdentifier) {
public @NotNull String getNodeId() {
return nodeIdentifier.getValue();
}
public @NotNull String getRoomId() {
return roomIdentifier.getValue();
}
@Override
public String toString() {
return nodeIdentifier.getValue() + "/" + roomIdentifier.getValue();
}
@Override
public int hashCode() {
return toString().hashCode();
}
@Override
public boolean equals(Object object) {
if (object == this) return true;
if (object == null) return false;
if (object instanceof RoomPath other) {
return toString().equals(other.toString());
}
return false;
}
}