1

I am having following Case class

case class BIC( id: Option[Int], name: String, description: Option[String], bId: Option[Int], amount: BigDecimal, createdBy: Option[Int], createdAt: Option[DateTime] ) 

my REST-API url is like following

POST /api/v3/user/2/bid/23/create 

Now userId = 2 and bId = 23 are coming From URL path params I am also having List of BIC incoming from POST Request JSON which does not contain userId and bId

I am directly injecting incoming JSON to List[BIC], but all the objects in this list doesn't have createdBy (userId) and bId set.

if I want to set these 2 parameters in each BIC objects of List[BIC] what is the approach I should follow?

 val lsttemp:List[BIC] = bicList val lst:List[BIC] lsttemp.foreach(x=>{ x.bId=inId x.createdBy = userId lst.add(x) }) 

I am trying above code but its not working.

1 Answer 1

3

Case classes provide copy method which helps you to achieve your goal:

val lst = bicList.map( _.copy(bId=inId, createdBy = userId)) 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Sergey Lagutin

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.