Cleaned home controller

This commit is contained in:
Rohan Sircar 2020-05-27 16:34:25 +05:30
parent a79d85f67f
commit 33bd91b5b7

View File

@ -1,7 +1,7 @@
package org.ros.chatto.controller; package org.ros.chatto.controller;
import java.security.Principal; import java.security.Principal;
import java.sql.SQLException; import java.util.Optional;
import org.ros.chatto.dto.ChatMessageDTO; import org.ros.chatto.dto.ChatMessageDTO;
import org.ros.chatto.service.UserService; import org.ros.chatto.service.UserService;
@ -9,85 +9,43 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/* import lombok.RequiredArgsConstructor;
@Controller import lombok.extern.slf4j.Slf4j;
@RequestMapping(value = "/test")
public class TestController {
@GetMapping
public ModelAndView getTestData() {
ModelAndView mv = new ModelAndView();
mv.setViewName("welcome");
mv.getModel().put("data", "Welcome home man");
return mv;
}
}*/
@Controller @Controller
@RequiredArgsConstructor
@Slf4j
public class Home { public class Home {
@Autowired @Autowired
private UserService userService; private final UserService userService;
// @Autowired
// private DBInitializerService dbInitializerService;
// private boolean installationChecked = false;
@RequestMapping("/") @RequestMapping("/")
public ModelAndView showPage(Principal principal) throws SQLException { public String showPage(Model model, Principal principal) {
ModelAndView mv = new ModelAndView("home"); final String welcomeMessage = Optional.of(principal).map(p -> {
String welcomeMesage = String.format("Welcome to chatto"); return String.format("Welcome back %s!", p.getName());
if (principal != null) { }).orElse("Welcome to chatto");
welcomeMesage = String.format("Welcome back %s!", principal.getName()); model.addAttribute("message", welcomeMessage);
} return "home";
mv.addObject("message", welcomeMesage);
// mv.addObject("userNames", userService.findAllOtherUsers(principal.getName()));
// if (!installationChecked) {
// dbInitializerService.connectDB();
// if(dbInitializerService.getNumTables() == 0)
// dbInitializerService.populateDB();
// dbInitializerService.closeConnection();
// installationChecked = true;
// }
return mv;
}
@GetMapping("/crypt")
public String showCrypt() {
return "crypto";
} }
@GetMapping("/chat") @GetMapping("/chat")
public ModelAndView showChat(Principal principal) { public String showChat(Model model, Principal principal) {
ModelAndView modelAndView = new ModelAndView("chat"); model.addAttribute(new ChatMessageDTO());
modelAndView.addObject(new ChatMessageDTO());
// modelAndView.addObject("userNames", userRepositoryCustom.getAllUserNames("hmm"));
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Authentication authentication = SecurityContextHolder.getContext()
.getAuthentication();
// Set<String> roles = authentication.getAuthorities().stream().map(r -> r.getAuthority())
// .collect(Collectors.toSet());
// for (String r : roles) {
// System.out.println(r);
// }
// boolean hasUserRole = authentication.getAuthorities().stream()
// .anyMatch(r -> r.getAuthority().equals("ROLE_USER"));
boolean isAdmin = authentication.getAuthorities().stream() boolean isAdmin = authentication.getAuthorities().stream()
.anyMatch(r -> r.getAuthority().equals("ROLE_ADMIN") || r.getAuthority().equals("ROLE_SUPER_USER")); .anyMatch(r -> r.getAuthority().equals("ROLE_ADMIN")
System.out.println("Is admin? " + isAdmin); || r.getAuthority().equals("ROLE_SUPER_USER"));
modelAndView.addObject("activeUsers", userService.getOtherActiveUsers(principal.getName())); log.trace("Is admin? " + isAdmin);
return modelAndView; model.addAttribute("activeUsers",
userService.getOtherActiveUsers(principal.getName()));
return "chat";
} }
// public String showHome(Model model)
// {
// model.addAttribute("message", "Welcome");
// return "home";
// }
} }