Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact #113

Closed
wants to merge 10 commits into from
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.sgwannabe.playlistserver.playlist.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class AsyncDatabaseService {

private final PlaylistRepository playlistRepository;

@Async
@Transactional
public CompletableFuture<Playlist> savePlaylistAsync(Playlist playlist) {
Playlist savedPlaylist = playlistRepository.save(playlist);
return CompletableFuture.completedFuture(savedPlaylist);
}

@Async
@Transactional
public CompletableFuture<Void> deletePlaylistAsync(String id) {
playlistRepository.deleteById(id);
return CompletableFuture.completedFuture(null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.sgwannabe.playlistserver.playlist.service;

@Service
@RequiredArgsConstructor
public class CacheService {
private final RedisTemplate<String, Playlist> redisTemplate;

public void cachePlaylist(Playlist playlist) {
String key = KeyGenerator.playlistKeyGenerate(playlist.getId());
redisTemplate.opsForValue().set(key, playlist, Duration.ofMinutes(10));
}

public Optional<PlaylistResponseDto> getCachedPlaylist(String id) {
String key = KeyGenerator.playlistKeyGenerate(id);
Playlist cachedPlaylist = redisTemplate.opsForValue().get(key);
return Optional.ofNullable(cachedPlaylist).map(converter::convert);
}

public void evictPlaylist(String id) {
String key = KeyGenerator.playlistKeyGenerate(id);
redisTemplate.delete(key);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.sgwannabe.playlistserver.playlist.service;

@Service
@RequiredArgsConstructor
public class MusicService {
private final FeignMusicClient musicClient;

public List<Music> getMusicsFromIds(List<Long> musicIds) {
BaseResponse<List<MusicDTO>> response = musicClient.getMusicFromIds(new MusicRetrieveRequestDTO(musicIds));
return response.getData().stream()
.map(this::convertToMusic)
.toList();
}

public Music createMusic(MusicRequestDto dto) {
return Music.builder()
.title(dto.getTitle())
.artistId(dto.getArtistId())
.artist(dto.getArtist())
.albumId(dto.getAlbumId())
.album(dto.getAlbum())
.thumbnail(dto.getThumbnail())
.playtime(dto.getPlaytime())
.build();
}

private Music convertToMusic(MusicDTO dto) {
return Music.builder()
.id(dto.getId())
.title(dto.getTitle())
.artistId(dto.getArtist().getId())
.artist(dto.getArtist().getName())
.albumId(dto.getAlbum().getId())
.album(dto.getAlbum().getTitle())
.thumbnail(dto.getAlbum().getCoverUrl())
.playtime(formatPlayTime(dto.getPlayTime()))
.build();
}

private String formatPlayTime(Short playTime) {
int min = playTime / 60;
int sec = playTime % 60;
return String.format("%02d:%02d", min, sec);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.sgwannabe.playlistserver.playlist.service;

@Service
@RequiredArgsConstructor
public class PlaylistEventPublisher {
private final KafkaTemplate<String, PlaylistEvent> kafkaTemplate;

public void publishPlaylistCreated(Playlist playlist) {
PlaylistEvent event = new PlaylistEvent("CREATED", playlist.getId());
kafkaTemplate.send("playlist-events", event);
}

public void publishPlaylistDeleted(String playlistId) {
PlaylistEvent event = new PlaylistEvent("DELETED", playlistId);
kafkaTemplate.send("playlist-events", event);
}
}

public class PlaylistEvent {
private String eventType;
private String playlistId;

}
Loading