This commit is contained in:
fogelito
2023-11-16 15:56:52 +02:00
parent 7cce033682
commit 04afeb4a8d
2 changed files with 206 additions and 0 deletions
+47
View File
@@ -1640,8 +1640,55 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/attributes/relati
Event $queueForEvents
) {
$key ??= $relatedCollectionId;
$twoWayKeyNull = is_null($twoWayKey);
$twoWayKey ??= $collectionId;
$databaseDocument = Authorization::skip(fn() => $dbForProject->getDocument('databases', $databaseId));
if ($databaseDocument->isEmpty()) {
throw new Exception(Exception::DATABASE_NOT_FOUND);
}
$collectionDocument = $dbForProject->getDocument('database_' . $databaseDocument->getInternalId(), $collectionId);
$collection = $dbForProject->getCollection('database_' . $databaseDocument->getInternalId() . '_collection_' . $collectionDocument->getInternalId());
if ($collection->isEmpty()) {
throw new Exception(Exception::COLLECTION_NOT_FOUND);
}
$relatedCollectionDocument = $dbForProject->getDocument('database_' . $databaseDocument->getInternalId(), $relatedCollectionId);
$relatedCollection = $dbForProject->getCollection('database_' . $databaseDocument->getInternalId() . '_collection_' . $relatedCollectionDocument->getInternalId());
if ($relatedCollection->isEmpty()) {
throw new Exception(Exception::COLLECTION_NOT_FOUND);
}
$attributes = $collection->getAttribute('attributes', []);
/** @var Document[] $attributes */
foreach ($attributes as $attribute) {
if (\strtolower($attribute->getId()) === \strtolower($key)) {
throw new Exception(Exception::ATTRIBUTE_ALREADY_EXISTS);
}
if (
$attribute->getAttribute('type') === Database::VAR_RELATIONSHIP &&
\strtolower($attribute->getAttribute('options')['twoWayKey']) === \strtolower($twoWayKey) &&
$attribute->getAttribute('options')['relatedCollection'] === $relatedCollection->getId()
) {
// Currently, we always throw an Exception even when, We do not want to change $twoWayKsy on $twoWayKeyNull
throw new Exception(Exception::ATTRIBUTE_ALREADY_EXISTS);
}
if (
$type === Database::RELATION_MANY_TO_MANY &&
$attribute->getAttribute('type') === Database::VAR_RELATIONSHIP &&
$attribute->getAttribute('options')['relationType'] === Database::RELATION_MANY_TO_MANY &&
$attribute->getAttribute('options')['relatedCollection'] === $relatedCollection->getId()
) {
throw new Exception(Exception::ATTRIBUTE_ALREADY_EXISTS, 'Creating more than one "manyToMany" relationship on the same collection is currently not permitted.');
}
}
$attribute = createAttribute(
$databaseId,
$collectionId,