__PACKAGE__

import io.getquill.*
import io.getquill.context.sql.idiom.SqlIdiom
__BEAN_CLASS_IMPORT__
__ID_CLASS_IMPORT__
__REPOSITORY_TRAIT_IMPORT__
__CUSTOM_IMPORTS__

final class __REPOSITORY_NAME__[__ALIAS_GENERIC_DECLARATION__](
    protected val context: C)(
    implicit meta: SchemaMeta[__BEAN__]
) extends __REPOSITORY_TRAIT_SIMPLE_NAME__ {

  import context.*

  protected def quoteQuery = quote {
    query[__BEAN__]
  }

  protected inline def find(id: __ID__): Quoted[EntityQuery[__BEAN__]] = quote {
    quoteQuery.__FIND_BY_KEY__
  }

  override def all: __MONAD__[Seq[__BEAN__]] =
   for {
     all <- __TRY_START__run(quoteQuery)__TRY_END__
   } yield {
     all
   }


  override def create(entity: __BEAN__): __MONAD__[__ID__] =
    for {
      _ <- __TRY_START__run(quoteQuery.insertValue(lift(entity)))__TRY_END__
    } yield {
      entity.id
    }

__CREATE_OR_UPDATE__


  override def read(id: __ID__): __MONAD__[Option[__BEAN__]] =
    for {
      seq <- __TRY_START__run(find(id))__TRY_END__
    } yield {
      seq.headOption
    }

  override def update(entity: __BEAN__): __MONAD__[__UP__] =
    __TRY_START__run(find(entity.id).updateValue(lift(entity)))__TRY_END__

  override def delete(id: __ID__): __MONAD__[__UP__] =
    __TRY_START__run(find(id).delete)__TRY_END__

  override def deleteAll(): __MONAD__[__UP__] =
    __TRY_START__run(quoteQuery.delete)__TRY_END__

}
