| 1 | package mk.ukim.finki.wp.db.controller;
|
|---|
| 2 |
|
|---|
| 3 | import lombok.RequiredArgsConstructor;
|
|---|
| 4 | import mk.ukim.finki.wp.db.service.SubscriptionPlanService;
|
|---|
| 5 | import mk.ukim.finki.wp.db.service.UserSubscriptionService;
|
|---|
| 6 | import org.springframework.security.core.Authentication;
|
|---|
| 7 | import org.springframework.stereotype.Controller;
|
|---|
| 8 | import org.springframework.ui.Model;
|
|---|
| 9 | import org.springframework.web.bind.annotation.GetMapping;
|
|---|
| 10 | import org.springframework.web.bind.annotation.PathVariable;
|
|---|
| 11 | import org.springframework.web.bind.annotation.PostMapping;
|
|---|
| 12 |
|
|---|
| 13 | @Controller
|
|---|
| 14 | @RequiredArgsConstructor
|
|---|
| 15 | public class UserSubscriptionController {
|
|---|
| 16 |
|
|---|
| 17 | private final UserSubscriptionService userSubscriptionService;
|
|---|
| 18 | private final SubscriptionPlanService subscriptionPlanService;
|
|---|
| 19 |
|
|---|
| 20 | @GetMapping("/user-subscription")
|
|---|
| 21 | public String getUserSubscriptionPage(Model model, Authentication authentication) {
|
|---|
| 22 | Object principal = authentication.getPrincipal();
|
|---|
| 23 | String email = ((org.springframework.security.core.userdetails.User) principal).getUsername();
|
|---|
| 24 | model.addAttribute("plans", subscriptionPlanService.findAll());
|
|---|
| 25 | model.addAttribute("alreadySubscribed", userSubscriptionService.hasUserSubscription(email));
|
|---|
| 26 | return "user_subscription/user_subscription";
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | @GetMapping("/user-subscription-payment/{id}")
|
|---|
| 30 | public String getPaymentPage(@PathVariable Integer id, Model model) {
|
|---|
| 31 | model.addAttribute("plan", subscriptionPlanService.findById(id));
|
|---|
| 32 | return "user_subscription/user_subscription_payment";
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | @PostMapping("/user-subscription-payment/{id}")
|
|---|
| 36 | public String subscribe(@PathVariable Integer id, Authentication authentication) {
|
|---|
| 37 | Object principal = authentication.getPrincipal();
|
|---|
| 38 | String email = ((org.springframework.security.core.userdetails.User) principal).getUsername();
|
|---|
| 39 | userSubscriptionService.addUserSubscription(email, id);
|
|---|
| 40 | return "redirect:/user-subscription";
|
|---|
| 41 | }
|
|---|
| 42 | }
|
|---|