Slick filter nscala-time DateTime by greaterEqual or smallerEqual

87 views Asked by At

I am trying to filter nscala-time DateTime with Slick

package models.repositories.tables

import java.sql.Timestamp
import com.github.nscala_time.time.Imports._
import models.entities.User
import slick.lifted.ProvenShape
import slick.jdbc.MySQLProfile.api._

class UserTable (tag: Tag)  extends Table[User](tag, "users") {
  def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
  def name = column[String]("name")
  def createdAt = column[DateTime]("createdAt", O.SqlType("TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP"))
  def updatedAt = column[DateTime]("updatedAt", O.SqlType("TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP"))
  override def * : ProvenShape[User] = (
    id,
    name,
    createdAt,
    updatedAt
  ) <> (User.tupled, User.unapply)
}

object UserTable extends GenericTableQuery[User, UserTable] {
  val userQuery = tableQuery

  implicit def dateTimeMapping: BaseColumnType[DateTime] = MappedColumnType.base[DateTime, Timestamp](
    dateTime => new Timestamp(dateTime.getMillis),
    timeStamp => new DateTime(timeStamp.getTime)
  )

}

But when I try to filter with

def getUsers(from: DateTime, end: DateTime, offset: Int) = {
  val usersQuery = UserTable.userQuery.filter(p => p.createdAt >= from && p.createdAt <= end).sortBy(_.createdAt.desc)
  users.drop(offset).take(25)
}

I get the symbol >= <= cannot be resolved

1

There are 1 answers

0
agusgambina On BEST ANSWER

All I need was to explicitly import the implicit def dateTimeMapping where I need to use it

import models.repositories.tables.UserTable.dateTimeMapping