Throwing exception with non persist node
This commit is contained in:
parent
b95d2f3953
commit
d00eaa0a7d
@ -24,6 +24,7 @@ public class ExceptionFactory {
|
|||||||
factory.put(RoomAlreadyExistException.ERROR_ID, RoomAlreadyExistException::new);
|
factory.put(RoomAlreadyExistException.ERROR_ID, RoomAlreadyExistException::new);
|
||||||
factory.put(RoomAreFullException.ERROR_ID, RoomAreFullException::new);
|
factory.put(RoomAreFullException.ERROR_ID, RoomAreFullException::new);
|
||||||
factory.put(RoomNotFoundException.ERROR_ID, RoomNotFoundException::new);
|
factory.put(RoomNotFoundException.ERROR_ID, RoomNotFoundException::new);
|
||||||
|
factory.put(NotPersistedNodeException.ERROR_ID, NotPersistedNodeException::new);
|
||||||
|
|
||||||
return factory;
|
return factory;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,46 @@
|
|||||||
|
package ru.dragonestia.picker.api.exception;
|
||||||
|
|
||||||
|
import ru.dragonestia.picker.api.repository.response.ErrorResponse;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public final class NotPersistedNodeException extends ApiException {
|
||||||
|
|
||||||
|
public final static String ERROR_ID = "rr.room.node_not_persisted";
|
||||||
|
|
||||||
|
private final String nodeId;
|
||||||
|
private final String roomId;
|
||||||
|
|
||||||
|
public NotPersistedNodeException(String nodeId, String roomId) {
|
||||||
|
this.nodeId = nodeId;
|
||||||
|
this.roomId = roomId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public NotPersistedNodeException(ErrorResponse errorResponse) {
|
||||||
|
this(errorResponse.details().get("node"), errorResponse.details().get("room"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getMessage() {
|
||||||
|
return "Cannot create persist room '%s' in non persist node '%s'".formatted(getRoomId(), getNodeId());
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNodeId() {
|
||||||
|
return nodeId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRoomId() {
|
||||||
|
return roomId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getErrorId() {
|
||||||
|
return ERROR_ID;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void appendDetailsToErrorResponse(Map<String, String> details) {
|
||||||
|
details.put("node", getNodeId());
|
||||||
|
details.put("room", getRoomId());
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -13,7 +13,7 @@ public interface NodeRepository {
|
|||||||
|
|
||||||
Set<NodeDetails> ALL_DETAILS = Set.of();
|
Set<NodeDetails> ALL_DETAILS = Set.of();
|
||||||
|
|
||||||
void register(RNode node) throws InvalidNodeIdentifierException, NodeAlreadyExistException;
|
void register(RNode node, boolean persist) throws InvalidNodeIdentifierException, NodeAlreadyExistException;
|
||||||
|
|
||||||
default List<RNode> all() {
|
default List<RNode> all() {
|
||||||
return all(Set.of());
|
return all(Set.of());
|
||||||
|
|||||||
@ -13,7 +13,7 @@ public interface RoomRepository {
|
|||||||
|
|
||||||
Set<RoomDetails> ALL_DETAILS = Set.of(RoomDetails.COUNT_USERS);
|
Set<RoomDetails> ALL_DETAILS = Set.of(RoomDetails.COUNT_USERS);
|
||||||
|
|
||||||
void register(RRoom room) throws NodeNotFoundException, InvalidRoomIdentifierException, RoomAlreadyExistException;
|
void register(RRoom room, boolean persist) throws NodeNotFoundException, InvalidRoomIdentifierException, RoomAlreadyExistException;
|
||||||
|
|
||||||
void remove(RRoom room) throws NodeNotFoundException;
|
void remove(RRoom room) throws NodeNotFoundException;
|
||||||
|
|
||||||
|
|||||||
@ -56,6 +56,11 @@ public class ExceptionHandlerController {
|
|||||||
return create(400, ex);
|
return create(400, ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(NotPersistedNodeException.class)
|
||||||
|
ResponseEntity<?> notPersistedNode(NotPersistedNodeException ex) {
|
||||||
|
return create(400, ex);
|
||||||
|
}
|
||||||
|
|
||||||
private ResponseEntity<ErrorResponse> create(int code, ApiException ex) {
|
private ResponseEntity<ErrorResponse> create(int code, ApiException ex) {
|
||||||
var details = new HashMap<String, String>();
|
var details = new HashMap<String, String>();
|
||||||
ex.appendDetailsToErrorResponse(details);
|
ex.appendDetailsToErrorResponse(details);
|
||||||
|
|||||||
@ -4,12 +4,15 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import ru.dragonestia.picker.api.exception.InvalidRoomIdentifierException;
|
import ru.dragonestia.picker.api.exception.InvalidRoomIdentifierException;
|
||||||
|
import ru.dragonestia.picker.api.exception.NodeNotFoundException;
|
||||||
|
import ru.dragonestia.picker.api.exception.NotPersistedNodeException;
|
||||||
import ru.dragonestia.picker.api.exception.RoomAlreadyExistException;
|
import ru.dragonestia.picker.api.exception.RoomAlreadyExistException;
|
||||||
import ru.dragonestia.picker.api.repository.details.RoomDetails;
|
import ru.dragonestia.picker.api.repository.details.RoomDetails;
|
||||||
import ru.dragonestia.picker.api.repository.response.type.RRoom;
|
import ru.dragonestia.picker.api.repository.response.type.RRoom;
|
||||||
import ru.dragonestia.picker.model.Room;
|
import ru.dragonestia.picker.model.Room;
|
||||||
import ru.dragonestia.picker.model.Node;
|
import ru.dragonestia.picker.model.Node;
|
||||||
import ru.dragonestia.picker.model.User;
|
import ru.dragonestia.picker.model.User;
|
||||||
|
import ru.dragonestia.picker.repository.NodeRepository;
|
||||||
import ru.dragonestia.picker.repository.RoomRepository;
|
import ru.dragonestia.picker.repository.RoomRepository;
|
||||||
import ru.dragonestia.picker.service.RoomService;
|
import ru.dragonestia.picker.service.RoomService;
|
||||||
import ru.dragonestia.picker.storage.NodeAndRoomStorage;
|
import ru.dragonestia.picker.storage.NodeAndRoomStorage;
|
||||||
@ -27,13 +30,20 @@ import java.util.Set;
|
|||||||
public class RoomServiceImpl implements RoomService {
|
public class RoomServiceImpl implements RoomService {
|
||||||
|
|
||||||
private final RoomRepository roomRepository;
|
private final RoomRepository roomRepository;
|
||||||
|
private final NodeRepository nodeRepository;
|
||||||
private final DetailsExtractor detailsExtractor;
|
private final DetailsExtractor detailsExtractor;
|
||||||
private final NamingValidator namingValidator;
|
private final NamingValidator namingValidator;
|
||||||
private final NodeAndRoomStorage storage;
|
private final NodeAndRoomStorage storage;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void create(Room room) throws InvalidRoomIdentifierException, RoomAlreadyExistException {
|
public void create(Room room) throws InvalidRoomIdentifierException, RoomAlreadyExistException, NotPersistedNodeException {
|
||||||
namingValidator.validateRoomId(room.getNodeId(), room.getId());
|
namingValidator.validateRoomId(room.getNodeId(), room.getId());
|
||||||
|
|
||||||
|
var node = nodeRepository.find(room.getNodeId()).orElseThrow(() -> new NodeNotFoundException(room.getNodeId()));
|
||||||
|
if (!node.persist() && room.isPersist()) {
|
||||||
|
throw new NotPersistedNodeException(node.id(), room.getId());
|
||||||
|
}
|
||||||
|
|
||||||
roomRepository.create(room);
|
roomRepository.create(room);
|
||||||
storage.saveRoom(room);
|
storage.saveRoom(room);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user