scala - How to reduce code duplication when using pattern matching and Future mappings? -


I have this code in my controller:

  def addPayment (ID: String , Zodiac: string) = action. Sinkk (implicit request = & gt; Upyogkrtakisbllensrekordaksisting (ID) Kflatmap {yesOrNo = & gt; Haanorno match {case true = & gt; user. GetCurrentPaymentBalance (id) .flatMap (left = & gt; User.updatePayment (id , balance + amount.toDouble)) .flatMap (result = & gt; User.putBonus (ID, Rashikupyog, 1)) Kmap (result = & gt; redirect (Margon.aplikeshnkyujrbons ())) case False = & gt; User paid payment (id, zodiac.devil). Flatmap (result => User.putbones (id, zodiac.wm 1)) Kmap (result = & gt; redirect (routes. App. Users benefit logic is simple: I'm checks whether or not a record in the DB, and if he is not there, then I will add it, If it is there then I will update it, after that I repeat the same series of action in both cases altogether, so I am not happy that I have repeated the code in both the flows. There are some future [T] return types in all business logic methods.   

in fact, you do not use the results of the Add / update, but to keep your logic:

  def addPayment (ID: string amount: string) = {Diaif process results [ T] (result: t) = user. Portbones (id, amount. Dual, 1)) .map (result => redirects (routes. Application.usbusiness ())) verb. Async (implicit request = & gt; Upyogkrtakisbllensrekordaksisting (ID) Flatmap {case true = & gt; User.getCurrentPaymentBalance (id) flatMap {balance = & gt; User.updatePayment (ID, the remaining amount.) Dblboks) flatMap process Result case false = & gt; User.addPayment (id, zodiac double) flatmap process usage}}}   

updated

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -