Question

I am using 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 retrieves a Customer with a specified id number. And then I want to access this Customer's account balance and perform calculations on it.

This is the code I have so far:

     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 run it, I get an error though:

    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 I don't know how to access database entry accountBalance for the specified customer?

Was it helpful?

Solution

I did't actually need to extract any customer. I just needed to extract the value of accountBalance of customer with a specified id. This is how I did that:

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

And because accBal is of type Option[Long] I had to:

    val accBalAsLong: Long = accBal.getOrElse(0)

OTHER TIPS

Slick works like Scala collections. Customers is like a collection. I does not have a member accountBalance, only its elements have that. So you have to use .map. Or in your case it is probably easier to fetch the customer record right away.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top