fix: [metaTemplateDirectory:create] Added gracefully handle uniqueness

develop-unstable
Sami Mokaddem 2023-02-13 15:14:43 +01:00
parent 25e63b710a
commit 0ac81af82a
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
1 changed files with 18 additions and 0 deletions

View File

@ -5,6 +5,7 @@ namespace App\Model\Table;
use App\Model\Entity\MetaTemplate;
use App\Model\Entity\MetaTemplateNameDirectory;
use App\Model\Table\AppTable;
use Cake\ORM\RulesChecker;
use Cake\Validation\Validator;
class MetaTemplateNameDirectoryTable extends AppTable
@ -33,6 +34,15 @@ class MetaTemplateNameDirectoryTable extends AppTable
return $validator;
}
public function buildRules(RulesChecker $rules): RulesChecker
{
$rules->add($rules->isUnique(
['uuid', 'version'],
__('This meta-template already exists.')
));
return $rules;
}
public function createFromMetaTemplate(MetaTemplate $metaTemplate): MetaTemplateNameDirectory
{
$metaTemplateDirectory = $this->newEntity([
@ -41,6 +51,14 @@ class MetaTemplateNameDirectoryTable extends AppTable
'uuid' => $metaTemplate['uuid'],
'version' => $metaTemplate['version'],
]);
$existingTemplate = $this->find()
->where([
'uuid' => $metaTemplate['uuid'],
'version' => $metaTemplate['version'],
])->first();
if (!empty($existingTemplate)) {
return $existingTemplate;
}
$this->save($metaTemplateDirectory);
return $metaTemplateDirectory;
}