How to access a database record in a row using Scala Slick

I use the code from the following tutorial: sysgears.com/articles/building-rest-service-with- scala / # 16

My database schema:

    /**
     * Customer entity.
     *
     * @param id        unique id
     * @param firstName first name
     * @param lastName  last name
     * @param accountBalance account balance
     * @param birthday  date of birth
     */
     case class Customer(id: Option[Long], firstName: String, lastName: String, accountBalance: Int, birthday: Option[java.util.Date])

     /**
      * Mapped customers table object.
      */
     object Customers extends Table[Customer]("customers") {

       def id = column[Long]("id", O.PrimaryKey, O.AutoInc)

       def firstName = column[String]("first_name")

       def lastName = column[String]("last_name")

       def accountBalance = column[Int]("account_balance")

       def birthday = column[java.util.Date]("birthday", O.Nullable)

       def * = id.? ~ firstName ~ lastName ~ accountBalance ~ birthday.? <>(Customer, Customer.unapply _)

       implicit val dateTypeMapper = MappedTypeMapper.base[java.util.Date, java.sql.Date](
       {
        ud => new java.sql.Date(ud.getTime)
       }, {
         sd => new java.util.Date(sd.getTime)
       })

       val findById = for {
         id <- Parameters[Long]
         c <- this if c.id is id
       } yield c
     }

I am writing a function that returns a client with the specified identifier number. And then I want to access this customer account balance and perform calculations on it.

This is the code that I still have:

     def withdraw(id: Long, amountToWithdraw: Long): Either[Failure, Customer] = {
         try {
           db.withSession {
            val customerRow = Customers.filter{ a => a.id === id}
            var amount = customerRow.accountBalance
             if (amountToWithdraw < accountBalance){
                accountBalance -= amountToWithdraw
                    update(customerId, customer)
                Right(customer)
             }
             else{
                 Left(insufficientFundsError(id))
                }
          }  
         } catch {
           case e: SQLException =>
             Left(databaseError(e))
         }
       }

When I try to start it, I get an error message:

    value accountBalance is not a member of scala.slick.lifted.Query[com.sysgears.example.domain.Customers.type,scala.slick.lifted.NothingContainer#TableNothing]
    var amount = customerRow.accountBalance
                             ^

So, I suppose my problem is that I do not know how to access the entryBalance database for the specified client?

+4
source share
2 answers

. accountBalance id. :

    val accBal = Customers.filter{ a => a.id === id}.map(_.accountBalance).firstOption

accBal Option [Long], :

    val accBalAsLong: Long = accBal.getOrElse(0)
+2

Scala. Customers - . accountBalance, . .map. , , .

val customerRow = Customers.filter{ a => a.id === id}.run.head

+1

Source: https://habr.com/ru/post/1531754/


All Articles