Skip to content

Commit

Permalink
fix update
Browse files Browse the repository at this point in the history
  • Loading branch information
charlie67 committed Sep 21, 2022
1 parent 37fe7e1 commit 9a62f37
Showing 1 changed file with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@
import to.charlie.spotifyplayhistory.domain.entity.ArtistEntity;
import to.charlie.spotifyplayhistory.domain.entity.PlayEntity;
import to.charlie.spotifyplayhistory.domain.entity.Token;
import to.charlie.spotifyplayhistory.domain.entity.TrackEntity;
import to.charlie.spotifyplayhistory.domain.repository.ArtistRepository;
import to.charlie.spotifyplayhistory.domain.repository.MigrationRepository;
import to.charlie.spotifyplayhistory.domain.repository.PlayRepository;
import to.charlie.spotifyplayhistory.domain.repository.TokenRepository;
import to.charlie.spotifyplayhistory.domain.repository.TrackRepository;


@Service
Expand All @@ -63,17 +65,21 @@ public class SpotifyApiService

private final MigrationRepository migrationRepository;

private final TrackRepository trackRepository;

public SpotifyApiService(PlayRepository playRepository,
ArtistRepository artistRepository,
SpotifyProperties spotifyProperties,
TokenRepository tokenRepository,
FlywayMigrator flywayMigrator,
MigrationRepository migrationRepository) throws URISyntaxException
MigrationRepository migrationRepository,
TrackRepository trackRepository) throws URISyntaxException
{
this.playRepository = playRepository;
this.artistRepository = artistRepository;
this.flywayMigrator = flywayMigrator;
this.migrationRepository = migrationRepository;
this.trackRepository = trackRepository;

Optional<Token> token = tokenRepository.findById(1);
token.ifPresent(value -> {
Expand Down Expand Up @@ -223,6 +229,24 @@ private void savePlayHistory(PagingCursorbased<PlayHistory> history)
}
}

Optional<TrackEntity> optionalTrack = trackRepository.findById(trackId);
TrackEntity trackEntity;
if (optionalTrack.isPresent())
{
trackEntity = optionalTrack.get();
}
else
{
trackEntity = TrackEntity.builder()
.id(trackId)
.trackName(track.getName())
.popularity(-1)
.songLength(track.getDurationMs())
.artists(artistEntities)
.build();
trackRepository.save(trackEntity);
}

PlayEntity playEntity = PlayEntity.builder().id(timePlayed)
.trackId(trackId)
.build();
Expand Down

0 comments on commit 9a62f37

Please sign in to comment.