package org.nstart.dep265.requestservice.repository

import org.nstart.dep265.requestservice.entity.RequestCellEntity
import org.springframework.data.jpa.repository.EntityGraph
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param
import org.springframework.transaction.annotation.Transactional
import java.util.UUID

interface RequestCellRepository : JpaRepository<RequestCellEntity, Long> {
    @Modifying
    @Transactional
    @Query("delete from RequestCellEntity requestCell where requestCell.requestId = :requestId")
    fun deleteByRequestId(@Param("requestId") requestId: UUID)

    fun findByRequestId(requestId: UUID): List<RequestCellEntity>

    @EntityGraph(attributePaths = ["cell"])
    @Query(
        """
        select requestCell
        from RequestCellEntity requestCell
        where requestCell.requestId = :requestId
        order by requestCell.id asc
        """
    )
    fun findWithCellByRequestId(@Param("requestId") requestId: UUID): List<RequestCellEntity>
}
