Skip to content

Commit

Permalink
Merge pull request #179 from os2display/feature/support-1328-attach-p…
Browse files Browse the repository at this point in the history
…laylists-to-slide-bugs

Bugfix: Attach playlists to slide bugs
  • Loading branch information
turegjorup authored Feb 26, 2024
2 parents c1059ca + 94e54d3 commit 3cd3a09
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
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

0 comments on commit 3cd3a09

Please sign in to comment.