package com.thoughtworks.training.banking;

import com.thoughtworks.training.banking.common.IdGenerator;
import com.thoughtworks.training.banking.model.Account;
import com.thoughtworks.training.banking.model.Balance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by yqin on 7/18/16.
 */
@RestController
@RequestMapping("/{userName}/accounts")
public class AccountController {
    private final IdGenerator idGenerator;

    private final Map<Long, Account> accounts;

    private void fillFakeAccounts() {
        Account heaton1 = new Account(1L, "1001", "Heaton", "heaton");
        heaton1.addBalance(new Balance(heaton1.getId(), "cny", new BigDecimal(100000)));
        heaton1.addBalance(new Balance(heaton1.getId(), "usd", new BigDecimal(30000)));
        Account heaton2 = new Account(2L, "2001", "Heaton", "heaton");
        heaton2.addBalance(new Balance(heaton2.getId(), "cny", new BigDecimal(200000)));
        Account qinyu = new Account(3L, "1002", "Qinyu", "qinyu");
        qinyu.addBalance(new Balance(qinyu.getId(), "cny", new BigDecimal(80000)));
        qinyu.addBalance(new Balance(qinyu.getId(), "usd", new BigDecimal(0)));

        accounts.put(heaton1.getId(), heaton1);
        accounts.put(heaton2.getId(), heaton2);
        accounts.put(qinyu.getId(), qinyu);
    }

    @Autowired
    public AccountController(IdGenerator idGenerator) {
        accounts = new HashMap<>();
        this.idGenerator = idGenerator;
        fillFakeAccounts();
    }

    @RequestMapping(method = RequestMethod.POST)
    public Account createAccount(@RequestParam("name") String name, @PathVariable String userName) {
        Account account = new Account(idGenerator.nextId(), null, name, userName);
        accounts.put(account.getId(), account);
        return account;
    }

    @RequestMapping(method = RequestMethod.GET)
    public List<Account> getAccountByUserName(@PathVariable String userName) {
        return accounts.values().stream().filter(account -> account.getUserName().equals(userName)).collect(Collectors.toList());
    }
}

@@ -465,46 +465,35 @@
 s;%0A%0A
-/**%0A * Created by yqin on 7/18/16.%0A */
+@CrossOrigin(origins = %22*%22)
 %0A@Re
@@ -578,18 +578,16 @@
 oller %7B%0A
-  
   privat
@@ -620,18 +620,16 @@
 rator;%0A%0A
-  
   privat
@@ -666,18 +666,16 @@
 ounts;%0A%0A
-  
   privat
@@ -698,36 +698,32 @@
 ccounts() %7B%0A    
- 
-   
 Account heaton1 
@@ -769,28 +769,24 @@
 aton%22);%0A    
-   
- 
 heaton1.addB
@@ -854,28 +854,24 @@
 000)));%0A    
-  
-  
 heaton1.addB
@@ -934,36 +934,32 @@
 l(30000)));%0A    
- 
-   
 Account heaton2 
@@ -1009,20 +1009,16 @@
 %22);%0A    
-   
- 
 heaton2.
@@ -1086,28 +1086,24 @@
 (200000)));%0A
-    
     Account 
@@ -1153,28 +1153,24 @@
 inyu%22);%0A    
- 
-   
 qinyu.addBal
@@ -1237,20 +1237,16 @@
 ));%0A    
-   
- 
 qinyu.ad
@@ -1310,20 +1310,16 @@
 (0)));%0A%0A
-    
     acco
@@ -1354,28 +1354,24 @@
 aton1);%0A    
- 
-   
 accounts.put
@@ -1398,28 +1398,24 @@
 aton2);%0A    
-   
- 
 accounts.put
@@ -1438,25 +1438,21 @@
 qinyu);%0A
-  
   %7D%0A%0A
-  
   @Autow
@@ -1456,18 +1456,16 @@
 towired%0A
-  
   public
@@ -1506,28 +1506,24 @@
 enerator) %7B%0A
-    
     accounts
@@ -1546,20 +1546,16 @@
 ();%0A    
-  
-  
 this.idG
@@ -1578,20 +1578,16 @@
 erator;%0A
-    
     fill
@@ -1602,25 +1602,21 @@
 unts();%0A
-  
   %7D%0A%0A
-  
   @Reque
@@ -1654,18 +1654,16 @@
 d.POST)%0A
-  
   public
@@ -1749,28 +1749,24 @@
 userName) %7B%0A
-    
     Account 
@@ -1832,20 +1832,16 @@
 rName);%0A
-    
     acco
@@ -1876,20 +1876,16 @@
 count);%0A
-    
     retu
@@ -1900,17 +1900,13 @@
 nt;%0A
-  
   %7D%0A%0A
-  
   @R
@@ -1947,18 +1947,16 @@
 od.GET)%0A
-  
   public
@@ -2024,20 +2024,16 @@
 Name) %7B%0A
-    
     retu
@@ -2150,16 +2150,14 @@
 ist());%0A
-  
   %7D%0A%7D%0A

add cros