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

package net.piedpiper.bremer.service.app

import net.piedpiper.bremer.config.Log
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.slf4j.Logger
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.findAllByKeywords(
                audioName,
                artistName,
                albumName,
                PAGE_LIMIT
            )
        )
}