Newer
Older
bremer / src / main / kotlin / service / app / AudioAppService.kt
yhornisse on 22 Jul 2023 1 KB add alias_name
/*
 * Copyright (c) 2023. yo-saito. All Rights Reserved.
 */

package net.piedpiper.bremer.service.app

import net.piedpiper.bremer.entity.AudioPlayHistoryEntity
import net.piedpiper.bremer.exception.NotFoundException
import net.piedpiper.bremer.model.api.AudioListResponse
import net.piedpiper.bremer.service.domain.AudioDomainService
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.io.File
import java.time.LocalDateTime
import java.time.ZoneId

@Service("bremer.service.AudioAppService")
class AudioAppService(
    @Qualifier("bremer.service.AudioDomainService")
    private val audioDomainService: AudioDomainService,
) {
    companion object {
        private const val PAGE_LIMIT = 50
    }

    @Transactional
    fun getAudioFile(slug: String): File {
        val audio = audioDomainService.findOneBySlug(slug) ?: throw NotFoundException()
        val file = File(audio.path)
        if (!file.exists()) {
            throw NotFoundException()
        }
        audioDomainService.savePlayedHistory(
            AudioPlayHistoryEntity(
                audioId = audio.id,
                lastPlayedAt = LocalDateTime.now(ZoneId.of("Asia/Tokyo"))
            )
        )
        return file
    }

    @Transactional
    fun getLeastRecentlyAccessedAudio(): AudioListResponse =
        AudioListResponse(audioDomainService.findAllLruLimit(PAGE_LIMIT))

    @Transactional
    fun getByKeywords(
        audioName: String?,
        artistName: String?,
        albumName: String?
    ): AudioListResponse =
        AudioListResponse(
            audioDomainService.findAllByKeyword(
                audioName,
                artistName,
                albumName,
                PAGE_LIMIT
            )
        )
}