Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

- [#179](https://github.com/os2display/display-api-service/pull/179)
- Fixed how playlists are added/removed from slides.
- [#184](https://github.com/os2display/display-api-service/pull/184)
- Added RelationsModifiedTrait to serialization groups.
- [#186](https://github.com/os2display/display-api-service/pull/186)
Expand Down
30 changes: 22 additions & 8 deletions src/Repository/PlaylistSlideRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,37 +132,51 @@ private function getWeight($ulid)

public function updateSlidePlaylistRelations(Ulid $slideUlid, ArrayCollection $collection)
{
$playlistIdsToAdd = array_map(fn ($entry) => $entry->playlist, $collection->toArray());

$user = $this->security->getUser();
$tenant = $user->getActiveTenant();
$slideRepos = $this->entityManager->getRepository(Slide::class);
$slide = $slideRepos->findOneBy(['id' => $slideUlid, 'tenant' => $tenant]);

if (is_null($slide)) {
throw new InvalidArgumentException('Slide not found');
}

$playlistRepos = $this->entityManager->getRepository(Playlist::class);

$this->entityManager->getConnection()->beginTransaction();

try {
if ($collection->isEmpty()) {
$entities = $this->findBy(['slide' => $slideUlid]);
foreach ($entities as $entity) {
$this->entityManager->remove($entity);
$this->entityManager->flush();
$entities = $this->findBy(['slide' => $slideUlid]);

$entitiesToRemove = array_reduce($entities, function (array $carry, PlaylistSlide $item) use ($playlistIdsToAdd) {
$playlist = $item->getPlaylist();
$id = $playlist->getId();
if (!in_array($id, $playlistIdsToAdd)) {
$carry[] = $item;
}

return $carry;
}, []);

foreach ($entitiesToRemove as $entity) {
$this->entityManager->remove($entity);
}

foreach ($collection as $entity) {
$playlist = $playlistRepos->findOneBy(['id' => $entity->playlist, 'tenant' => $tenant]);
$this->entityManager->flush();

foreach ($playlistIdsToAdd as $playlistIdToAdd) {
$playlist = $playlistRepos->findOneBy(['id' => $playlistIdToAdd, 'tenant' => $tenant]);

if (is_null($playlist)) {
throw new InvalidArgumentException('Playlist not found');
}

$playlistSlideRelations = $this->findOneBy(['slide' => $slide, 'playlist' => $playlist]);
$ulid = $this->validationUtils->validateUlid($playlist->getId());

if (is_null($playlistSlideRelations)) {
$ulid = $this->validationUtils->validateUlid($playlist->getId());
$weight = $this->getWeight($ulid);

// Create new relation.
Expand Down