Skip to content

Commit

Permalink
fix: add key for fix update method to the database
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
  • Loading branch information
otaviojava committed May 19, 2023
1 parent 3319edd commit 700b295
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,18 @@ public DocumentEntity insert(DocumentEntity entity) throws NullPointerException
public DocumentEntity update(DocumentEntity entity) {
String collectionName = entity.name();
checkCollection(collectionName);
String id = entity.find(ID, String.class)
.orElseThrow(() -> new IllegalArgumentException("The document does not provide" +
" the _id column"));
feedKey(entity, id);
BaseDocument baseDocument = ArangoDBUtil.getBaseDocument(entity);
DocumentUpdateEntity<BaseDocument> arandoDocument = arangoDB.db(DbName.of(database))
.collection(collectionName).updateDocument(baseDocument.getKey(), baseDocument);
updateEntity(entity, arandoDocument.getKey(), arandoDocument.getId(), arandoDocument.getRev());
return entity;
}


@Override
public Iterable<DocumentEntity> update(Iterable<DocumentEntity> entities) {
Objects.requireNonNull(entities, "entities is required");
Expand Down Expand Up @@ -192,6 +197,17 @@ private void updateEntity(DocumentEntity entity, String key, String id, String r
entity.add(Document.of(REV, rev));
}

private static void feedKey(DocumentEntity entity, String id) {
if (entity.find(KEY).isEmpty()) {
String[] values = id.split("/");
if (values.length == 2) {
entity.add(KEY, values[1]);
} else {
entity.add(KEY, values[0]);
}
}
}

ArangoDB getArangoDB() {
return arangoDB;
}
Expand Down

0 comments on commit 700b295

Please sign in to comment.