/* * Copyright (c) 2023. yo-saito. All Rights Reserved. */ package net.piedpiper.bremer.service.domain import net.piedpiper.bremer.config.Log import net.piedpiper.bremer.entity.AudioEntity import net.piedpiper.bremer.entity.AudioPlayHistoryEntity import net.piedpiper.bremer.repository.AudioRepository import org.springframework.beans.factory.annotation.Qualifier import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @Service("bremer.service.AudioDomainService") class AudioDomainService( @Qualifier("bremer.repository.AudioRepository") private val audioRepository: AudioRepository, ) { private val log by Log() @Transactional fun findOneBySlug(slug: String): AudioEntity? { val a = audioRepository.findOneBySlug(slug) log.info("audio: {}", a) return a } @Transactional fun findAllByOrderedIds(ids: List<Long>): List<AudioEntity> { val id2idx = ids.mapIndexed { index, id -> id to index }.toMap() return audioRepository.findAllByIdIn(ids) .sortedBy { id2idx[it.id] } } @Transactional fun findAllByOrderedSlugs(slugs: List<String>): List<AudioEntity> { val slug2idx = slugs.mapIndexed { index, slug -> slug to index }.toMap() return audioRepository.findAllBySlugIn(slugs) .sortedBy { slug2idx[it.slug] } } @Transactional fun findAllLruLimit(limit: Int): List<AudioEntity> = audioRepository.findAllLruLimit(limit) @Transactional fun findAllByKeyword( nameLike: String?, artistNameLike: String?, albumNameLike: String?, limit: Int ): List<AudioEntity> { val set = mutableSetOf<AudioEntity>() if (nameLike?.isNotEmpty() == true) { set.addAll(audioRepository.findAllByNameLikeLimit(nameLike, limit)) } if (artistNameLike?.isNotEmpty() == true) { set.addAll(audioRepository.findAllByArtistNameLikeLimit(artistNameLike, limit)) } if (albumNameLike?.isNotEmpty() == true) { set.addAll(audioRepository.findAllByAlbumNameLikeLimit(albumNameLike, limit)) } return set.take(limit).sortedByDescending { it.id }.toList() } fun savePlayedHistory(entity: AudioPlayHistoryEntity) = audioRepository.saveAudioPlayHistory(entity) }