WIP
[demos/pact] / pwreset / src / main / java / de / juplo / demos / pwreset / adapters / in / WebController.java
1 package de.juplo.demos.pwreset.adapters.in;
2
3 import de.juplo.demos.pwreset.domain.ports.in.PasswordResetRequestProgressDTO;
4 import de.juplo.demos.pwreset.domain.ports.in.PasswordResetPort;
5 import lombok.RequiredArgsConstructor;
6 import org.springframework.stereotype.Controller;
7 import org.springframework.web.bind.annotation.*;
8 import org.springframework.web.servlet.ModelAndView;
9
10 import javax.servlet.http.HttpServletResponse;
11 import java.io.IOException;
12 import java.util.UUID;
13
14
15 @Controller
16 @RequiredArgsConstructor
17 public class WebController
18 {
19   private final PasswordResetPort passwordResetPort;
20
21
22   @GetMapping("/")
23   public String welcome()
24   {
25     return "request";
26   }
27
28   @PostMapping("/")
29   public void createResetRequest(
30       @RequestParam String email,
31       HttpServletResponse httpServletResponse)
32       throws
33       IOException
34   {
35     PasswordResetRequestProgressDTO progress = passwordResetPort.createRequest(email);
36     httpServletResponse.sendRedirect(progress.getRequestId().toString());
37   }
38
39   @GetMapping("/{id}")
40   public ModelAndView getResetRequest(@PathVariable UUID id)
41   {
42     PasswordResetRequestProgressDTO progress = passwordResetPort.getRequest(id);
43     return createModelAndView(progress);
44   }
45
46   @PutMapping("/{id}")
47   public ModelAndView confirmResetRequest(
48       @PathVariable UUID id,
49       @RequestParam String code,
50       @RequestParam String password)
51   {
52     PasswordResetRequestProgressDTO progress =
53         passwordResetPort.confirmRequest(
54             id,
55             code,
56             password);
57
58     ModelAndView mav = createModelAndView(progress);
59
60     mav.addObject("code", code);
61     mav.addObject("password", password);
62
63     return mav;
64   }
65
66   ModelAndView createModelAndView(PasswordResetRequestProgressDTO progress)
67   {
68     ModelAndView mav = new ModelAndView();
69     mav.addObject("progress", progress);
70
71     if (progress.isAccepted())
72     {
73       if (progress.getConfirmed().isEmpty())
74       {
75         mav.setViewName("confirm");
76       }
77       else
78       {
79         mav.setViewName("confirmed");
80       }
81     }
82     else
83     {
84       mav.setViewName("denied");
85     }
86
87     return mav;
88   }
89 }