Skip to content

Commit 6fe43f4

Browse files
committed
feat: include validation to find by either id or key column
Signed-off-by: Otavio Santana <[email protected]>
1 parent 559167a commit 6fe43f4

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/DefaultArangoDBDocumentManager.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.time.Duration;
2929
import java.util.Map;
30+
import java.util.Optional;
3031
import java.util.logging.Level;
3132
import java.util.logging.Logger;
3233
import java.util.stream.Collectors;
@@ -76,12 +77,23 @@ public CommunicationEntity update(CommunicationEntity entity) {
7677
requireNonNull(entity, "entity is required");
7778
String collectionName = entity.name();
7879
checkCollection(collectionName);
79-
entity.find(KEY, String.class)
80-
.orElseThrow(() -> new IllegalArgumentException("The document does not provide" +
81-
" the _key column"));
80+
Optional<String> keyElement = entity.find(KEY, String.class);
81+
Optional<String> idElement = entity.find(ID, String.class);
82+
if (keyElement.isEmpty() && idElement.isEmpty()) {
83+
throw new IllegalArgumentException("The entity requires either key or id");
84+
}
85+
var key = keyElement.orElseGet(() -> {
86+
String id = idElement.orElseThrow();
87+
var elements = id.split("/");
88+
if (elements.length == 2) {
89+
return elements[1];
90+
} else {
91+
return elements[0];
92+
}
93+
});
8294
JsonObject jsonObject = ArangoDBUtil.toJsonObject(entity);
8395
DocumentUpdateEntity<Void> arangoDocument = arangoDB.db(database)
84-
.collection(collectionName).updateDocument(jsonObject.getString(KEY), jsonObject);
96+
.collection(collectionName).updateDocument(key, jsonObject);
8597
updateEntity(entity, arangoDocument.getKey(), arangoDocument.getId(), arangoDocument.getRev());
8698
return entity;
8799
}

0 commit comments

Comments
 (0)