Newer
Older
bremer / src / main / kotlin / controller / api / AudioApiController.kt
/*
 * Copyright (c) 2023. yo-saito. All Rights Reserved.
 */

package net.piedpiper.bremer.controller.api

import jakarta.validation.Valid
import net.piedpiper.bremer.model.api.AudioListResponse
import net.piedpiper.bremer.model.api.AudioRequest
import net.piedpiper.bremer.service.AudioService
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

@RestController("bremer.controller.AudioApiController")
@RequestMapping("bremer/api/audio")
class AudioApiController(
    @Qualifier("bremer.service.AudioService")
    private val audioService: AudioService
) {
    companion object {
        private const val PAGE_LIMIT = 50
    }

    // TODO 複数キーワード対応
    @GetMapping
    fun getAudioList(
        @RequestParam("audio", required = false) audioName: String?,
        @RequestParam("artist", required = false) artistName: String?,
        @RequestParam("album", required = false) albumName: String?,
        @RequestParam("tag", required = false) tagName: String?
    ): ResponseEntity<AudioListResponse> =
        ResponseEntity.ok(audioService.getByKeywords(
            audioName, artistName, albumName, tagName, PAGE_LIMIT))

    @GetMapping("history")
    fun getLeastRecentlyPlayedList(): ResponseEntity<AudioListResponse> =
        ResponseEntity.ok(audioService.getLeastRecentlyAccessedAudio(PAGE_LIMIT))

    @PutMapping("{slug}")
    fun updateAudio(
        @PathVariable("slug") slug: String,
        @Valid @RequestBody request: AudioRequest
    ): ResponseEntity<Void> {
        audioService.update(slug, request)
        return ResponseEntity.ok(null)
    }
}