|
| 1 | +package pl.piomin.microservices.advanced.customer.api; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.logging.Logger; |
| 5 | + |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.web.bind.annotation.PathVariable; |
| 8 | +import org.springframework.web.bind.annotation.RequestBody; |
| 9 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 10 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 11 | +import org.springframework.web.bind.annotation.RestController; |
| 12 | + |
| 13 | +import pl.piomin.microservices.advanced.customer.contract.Account; |
| 14 | +import pl.piomin.microservices.advanced.customer.contract.AccountClient; |
| 15 | +import pl.piomin.microservices.advanced.customer.model.Customer; |
| 16 | +import pl.piomin.microservices.advanced.customer.repository.CustomerRepository; |
| 17 | + |
| 18 | +@RestController |
| 19 | +public class CustomerController { |
| 20 | + |
| 21 | + @Autowired |
| 22 | + private AccountClient accountClient; |
| 23 | + |
| 24 | + @Autowired |
| 25 | + CustomerRepository repository; |
| 26 | + |
| 27 | + protected Logger logger = Logger.getLogger(CustomerController.class.getName()); |
| 28 | + |
| 29 | + @RequestMapping("/customers/pesel/{pesel}") |
| 30 | + public Customer findByPesel(@PathVariable("pesel") String pesel) { |
| 31 | + logger.info(String.format("Customer.findByPesel(%s)", pesel)); |
| 32 | + return repository.findByPesel(pesel); |
| 33 | + } |
| 34 | + |
| 35 | + @RequestMapping("/customers") |
| 36 | + public List<Customer> findAll() { |
| 37 | + logger.info("Customer.findAll()"); |
| 38 | + return repository.findAll(); |
| 39 | + } |
| 40 | + |
| 41 | + @RequestMapping("/customers/{id}") |
| 42 | + public Customer findById(@PathVariable("id") String id) { |
| 43 | + logger.info(String.format("Customer.findById(%s)", id)); |
| 44 | + Customer customer = repository.findById(id); |
| 45 | + List<Account> accounts = accountClient.getAccounts(id); |
| 46 | + customer.setAccounts(accounts); |
| 47 | + return customer; |
| 48 | + } |
| 49 | + |
| 50 | + @RequestMapping(method = RequestMethod.POST, value = "/customers") |
| 51 | + public Customer add(@RequestBody Customer customer) { |
| 52 | + logger.info(String.format("Customer.add(%s)", customer)); |
| 53 | + return repository.save(customer); |
| 54 | + } |
| 55 | + |
| 56 | +} |
0 commit comments