|
1 | 1 | package com.boardapplication.controller;
|
2 | 2 |
|
| 3 | +import com.boardapplication.configuration.JoinValidator; |
| 4 | +import com.boardapplication.dto.JoinDTO; |
| 5 | +import com.boardapplication.repository.UserRepository; |
| 6 | +import com.boardapplication.service.UserService; |
| 7 | +import com.core.entity.User; |
| 8 | +import lombok.RequiredArgsConstructor; |
| 9 | +import org.springframework.security.core.Authentication; |
3 | 10 | import org.springframework.stereotype.Controller;
|
| 11 | +import org.springframework.ui.Model; |
| 12 | +import org.springframework.validation.Errors; |
| 13 | +import org.springframework.web.bind.WebDataBinder; |
4 | 14 | import org.springframework.web.bind.annotation.GetMapping;
|
| 15 | +import org.springframework.web.bind.annotation.InitBinder; |
| 16 | +import org.springframework.web.bind.annotation.PostMapping; |
| 17 | + |
| 18 | +import javax.validation.Valid; |
5 | 19 |
|
6 | 20 | @Controller
|
| 21 | +@RequiredArgsConstructor |
7 | 22 | public class UserController {
|
8 |
| - @GetMapping |
9 |
| - public String index() { |
10 |
| - return "test"; |
| 23 | + |
| 24 | + private final JoinValidator joinValidator; |
| 25 | + private final UserService userService; |
| 26 | + private final UserRepository userRepository; |
| 27 | + |
| 28 | + @InitBinder("joinDTO") |
| 29 | + public void initBinder(WebDataBinder webDataBinder) { |
| 30 | + webDataBinder.addValidators(joinValidator); |
| 31 | + } |
| 32 | + |
| 33 | + @GetMapping("/") |
| 34 | + public String index(Authentication authentication) { |
| 35 | + |
| 36 | + if (authentication != null && authentication.isAuthenticated()) { |
| 37 | + return "redirect:/profile"; |
| 38 | + } else { |
| 39 | + return "/login"; |
| 40 | + } |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + @GetMapping("/login") |
| 46 | + public String login(){ |
| 47 | + return "login"; |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + @GetMapping("/join") |
| 52 | + public String join(Model model) { |
| 53 | + model.addAttribute("joinDTO", new JoinDTO()); |
| 54 | + return "/join"; |
11 | 55 | }
|
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + @PostMapping("/join") |
| 60 | + public String joinDTO(@Valid JoinDTO joinDTO, Errors errors) { |
| 61 | + if (errors.hasErrors()) { |
| 62 | + return "/join"; |
| 63 | + } |
| 64 | + User user = userService.processNewUser(joinDTO); |
| 65 | + |
| 66 | + return "/"; |
| 67 | + } |
| 68 | + |
| 69 | + /* |
| 70 | + @GetMapping("/checkusername") |
| 71 | + public ResponseEntity<String> checkUsername(@RequestParam String username) { |
| 72 | + if (userRepository.existsByUsername(username)) { |
| 73 | + return ResponseEntity.ok("이미 사용중인 아이디입니다."); |
| 74 | + } else { |
| 75 | + return ResponseEntity.ok("사용 가능한 아이디입니다."); |
| 76 | + } |
| 77 | + } |
| 78 | +
|
| 79 | + */ |
| 80 | + |
| 81 | + |
| 82 | + @GetMapping("/profile") |
| 83 | + public String profile() { |
| 84 | + return "profile"; |
| 85 | + } |
| 86 | + |
| 87 | + |
12 | 88 | }
|
0 commit comments