Newer
Older
bremer / src / main / kotlin / dao / PlaylistDao.kt
yhornisse on 21 Aug 2023 2 KB refactor
/*
 * Copyright (c) 2023. yo-saito. All Rights Reserved.
 */

package net.piedpiper.bremer.dao

import net.piedpiper.bremer.entity.PlaylistAudioEntity
import net.piedpiper.bremer.entity.PlaylistEntity
import net.piedpiper.bremer.utils.DaoUtils
import net.piedpiper.bremer.utils.SelectSQLBuilder
import org.apache.ibatis.annotations.*
import org.springframework.stereotype.Repository

@Repository("bremer.dao.PlaylistDao")
@Mapper
interface PlaylistDao {

    @ResultMap("net.piedpiper.bremer.PlaylistEntity")
    @SelectProvider(type = Sql::class, method = "findOneById")
    fun findOneById(@Param("id") id: Long): PlaylistEntity?

    @ResultMap("net.piedpiper.bremer.PlaylistEntity")
    @Select("SELECT * FROM playlist WHERE slug = #{slug} FOR UPDATE")
    fun findOneBySlugWithLock(@Param("slug") slug: String): PlaylistEntity?

    @ResultMap("net.piedpiper.bremer.PlaylistEntity")
    @SelectProvider(type = Sql::class, method = "findOneBySlug")
    fun findOneBySlug(@Param("slug") slug: String): PlaylistEntity?

    @ResultMap("net.piedpiper.bremer.PlaylistEntity")
    @Select("SELECT * FROM playlist ORDER BY sequence ASC")
    fun findAllOrderBySequence(): List<PlaylistEntity>

    @InsertProvider(type = Sql::class, method = "insertOne")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    fun insertOne(@Param("entity") entity: PlaylistEntity): Boolean

    @Delete("DELETE FROM playlist WHERE slug = #{slug}")
    fun deleteOneBySlug(@Param("slug") slug: String): Boolean

    @UpdateProvider(type = Sql::class, method = "updateOne")
    fun updateOne(@Param("entity") entity: PlaylistEntity)

    class Sql {
        companion object {
            @JvmStatic
            fun findOneById(): String =
                SelectSQLBuilder(PlaylistEntity::class)
                    .leftOuterJoin(
                        PlaylistAudioEntity::class,
                        "playlist.id = playlist_audio.playlist_id"
                    )
                    .toSql()
                    .WHERE("playlist.id = #{id}")
                    .ORDER_BY("playlist_audio.sequence ASC")
                    .toString()

            @JvmStatic
            fun findOneBySlug(): String =
                SelectSQLBuilder(PlaylistEntity::class)
                    .leftOuterJoin(
                        PlaylistAudioEntity::class,
                        "playlist.id = playlist_audio.playlist_id"
                    )
                    .toSql()
                    .WHERE("playlist.slug = #{slug}")
                    .ORDER_BY("playlist_audio.sequence ASC")
                    .toString()

            @JvmStatic
            @Options(useGeneratedKeys = true, keyColumn = "id")
            fun insertOne(@Param("entity") entity: PlaylistEntity): String =
                DaoUtils.insertOne(entity)

            @JvmStatic
            fun updateOne(@Param("entity") entity: PlaylistEntity): String =
                DaoUtils.updateOne(entity)
        }
    }
}