I use the code from the following tutorial: sysgears.com/articles/building-rest-service-with- scala / # 16
My database schema:
case class Customer(id: Option[Long], firstName: String, lastName: String, accountBalance: Int, birthday: Option[java.util.Date])
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
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?
Marta source
share