Fix update segment when the name is the same
[MAILPOET-3343]
This commit is contained in:
@@ -16,18 +16,17 @@ class SegmentSaveController {
|
||||
}
|
||||
|
||||
public function save(array $data = []): SegmentEntity {
|
||||
$this->checkSegmenUniqueName($data['name'] ?? '');
|
||||
|
||||
$id = isset($data['id']) ? (int)$data['id'] : null;
|
||||
$name = $data['name'] ?? '';
|
||||
$description = $data['description'] ?? '';
|
||||
|
||||
$this->checkSegmenUniqueName($name, $id);
|
||||
|
||||
return $this->segmentsRepository->createOrUpdate($name, $description, $id);
|
||||
}
|
||||
|
||||
private function checkSegmenUniqueName(string $name): void {
|
||||
$segment = $this->segmentsRepository->findOneBy(['name' => $name]);
|
||||
if ($segment) {
|
||||
private function checkSegmenUniqueName(string $name, ?int $id): void {
|
||||
if (!$this->segmentsRepository->isNameUnique($name, $id)) {
|
||||
throw new InvalidArgumentException("Segment with name: '{$name}' already exists.");
|
||||
}
|
||||
}
|
||||
|
@@ -33,6 +33,23 @@ class SegmentsRepository extends Repository {
|
||||
return $countMap;
|
||||
}
|
||||
|
||||
public function isNameUnique(string $name, ?int $id): bool {
|
||||
$qb = $this->doctrineRepository->createQueryBuilder('s')
|
||||
->select('s')
|
||||
->where('s.name = :name')
|
||||
->setParameter('name', $name);
|
||||
|
||||
if ($id !== null) {
|
||||
$qb->andWhere('s.id != :id')
|
||||
->setParameter('id', $id);
|
||||
}
|
||||
|
||||
$results = $qb->getQuery()
|
||||
->getResult();
|
||||
|
||||
return count($results) === 0;
|
||||
}
|
||||
|
||||
public function createOrUpdate(
|
||||
string $name,
|
||||
string $description = '',
|
||||
|
Reference in New Issue
Block a user