Last active
October 26, 2017 14:06
-
-
Save peterjurkovic/28df668abae54df6c89c88211a9b1f00 to your computer and use it in GitHub Desktop.
Payment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Controller | |
public class PaymentController { | |
public final UserService userService; | |
public final PaymentService paymentService; | |
public PaymentController(UserService userService, PaymentService paymentService) { | |
this.userService = userService; | |
this.paymentService = paymentService; | |
} | |
@GetMapping("users/{id}") | |
public String showPage(ModelMap model, @PathVariable long id) { | |
User user = userService.getUserById(id); | |
model.addAttribute( new PaymentForm(user) ); | |
return "payment_view"; | |
} | |
@PostMapping("createPayment") | |
public String showPage(PaymentForm form, BindingResult result) { | |
if ( ! result.hasErrors() ) { | |
User user = userService.getUserById(form.getdUserId()); | |
paymentService.createPayment(user, form.getAmount(), form.getMethod()); | |
} | |
return "payment_view"; | |
} | |
} | |
@Service | |
public class PaymentService { | |
private final static int CREDIT_CARD = 1; | |
private final static int BANK_TRANSFER = 2; | |
private User user; | |
private int amount; | |
public void createPayment(User user, int amount, int method) { | |
this.user = user; | |
this.amount = amount; | |
if(method == CREDIT_CARD){ | |
handleCreditCardPayment(); | |
} else { | |
handleBankTranswerPayment(); | |
} | |
} | |
private void handleCreditCardPayment() { | |
save( new Transaction(user, amount, CREDIT_CARD)); | |
} | |
private void handleBankTranswerPayment() { | |
save( new Transaction(user, amount, BANK_TRANSFER)); | |
} | |
private void save(Transaction transaction) {} | |
} | |
public class PaymentForm { | |
@Min(1) | |
@Max(100) | |
private int amount = 50; | |
private int method; | |
private long userId; | |
public PaymentForm() { | |
} | |
public PaymentForm(User user) { | |
this.userId = user.getId(); | |
} | |
public int getAmount() { | |
return amount; | |
} | |
public void setAmount(int amount) { | |
this.amount = amount; | |
} | |
public long getdUserId() { | |
return userId; | |
} | |
public void setUserId(long userId) { | |
this.userId = userId; | |
} | |
public int getMethod() { | |
return method; | |
} | |
public void setMethod(int method) { | |
this.method = method; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment