1 | package com.example.eatys_app.controller;
|
---|
2 |
|
---|
3 |
|
---|
4 | import com.example.eatys_app.model.exceptions.InvalidArgumentsException;
|
---|
5 | import com.example.eatys_app.model.exceptions.PasswordsDoNotMatchException;
|
---|
6 | import com.example.eatys_app.service.AuthService;
|
---|
7 | import com.example.eatys_app.service.KorisnikService;
|
---|
8 | import org.springframework.stereotype.Controller;
|
---|
9 | import org.springframework.ui.Model;
|
---|
10 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
11 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
12 | import org.springframework.web.bind.annotation.RequestMapping;
|
---|
13 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
14 |
|
---|
15 | @Controller
|
---|
16 | @RequestMapping("/register")
|
---|
17 | public class RegisterController {
|
---|
18 |
|
---|
19 | private final AuthService authService;
|
---|
20 | private final KorisnikService korisnikService;
|
---|
21 |
|
---|
22 | public RegisterController(AuthService authService, KorisnikService korisnikService) {
|
---|
23 | this.authService = authService;
|
---|
24 | this.korisnikService = korisnikService;
|
---|
25 | }
|
---|
26 |
|
---|
27 |
|
---|
28 | @GetMapping
|
---|
29 | public String getRegisterPage(@RequestParam(required = false) String error, Model model) {
|
---|
30 | if (error != null && !error.isEmpty()) {
|
---|
31 | model.addAttribute("hasError", true);
|
---|
32 | model.addAttribute("error", error);
|
---|
33 | }
|
---|
34 |
|
---|
35 | return "register.html";
|
---|
36 | }
|
---|
37 |
|
---|
38 |
|
---|
39 | @PostMapping
|
---|
40 | public String register(@RequestParam String ime,
|
---|
41 | @RequestParam String prezime,
|
---|
42 | @RequestParam String password,
|
---|
43 | @RequestParam String repeatedPassword) {
|
---|
44 | try {
|
---|
45 | this.korisnikService.register(ime, prezime, password, repeatedPassword);
|
---|
46 | return "redirect:/login";
|
---|
47 | } catch (InvalidArgumentsException | PasswordsDoNotMatchException exception) {
|
---|
48 | return "redirect:/register?error=" + exception.getMessage();
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|