From 33bd91b5b70dc4d1d23b022973504dd17b70f818 Mon Sep 17 00:00:00 2001 From: Rohan Sircar Date: Wed, 27 May 2020 16:34:25 +0530 Subject: [PATCH] Cleaned home controller --- .../java/org/ros/chatto/controller/Home.java | 88 +++++-------------- 1 file changed, 23 insertions(+), 65 deletions(-) diff --git a/chatto/src/main/java/org/ros/chatto/controller/Home.java b/chatto/src/main/java/org/ros/chatto/controller/Home.java index a092b7d..4291214 100644 --- a/chatto/src/main/java/org/ros/chatto/controller/Home.java +++ b/chatto/src/main/java/org/ros/chatto/controller/Home.java @@ -1,7 +1,7 @@ package org.ros.chatto.controller; import java.security.Principal; -import java.sql.SQLException; +import java.util.Optional; import org.ros.chatto.dto.ChatMessageDTO; 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.context.SecurityContextHolder; import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.servlet.ModelAndView; -/* -@Controller -@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; - } -}*/ +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; @Controller +@RequiredArgsConstructor +@Slf4j public class Home { @Autowired - private UserService userService; -// @Autowired -// private DBInitializerService dbInitializerService; - -// private boolean installationChecked = false; + private final UserService userService; @RequestMapping("/") - public ModelAndView showPage(Principal principal) throws SQLException { - ModelAndView mv = new ModelAndView("home"); - String welcomeMesage = String.format("Welcome to chatto"); - if (principal != null) { - welcomeMesage = String.format("Welcome back %s!", principal.getName()); - } - 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"; + public String showPage(Model model, Principal principal) { + final String welcomeMessage = Optional.of(principal).map(p -> { + return String.format("Welcome back %s!", p.getName()); + }).orElse("Welcome to chatto"); + model.addAttribute("message", welcomeMessage); + return "home"; } @GetMapping("/chat") - public ModelAndView showChat(Principal principal) { - ModelAndView modelAndView = new ModelAndView("chat"); - modelAndView.addObject(new ChatMessageDTO()); -// modelAndView.addObject("userNames", userRepositoryCustom.getAllUserNames("hmm")); - - Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - -// Set roles = authentication.getAuthorities().stream().map(r -> r.getAuthority()) -// .collect(Collectors.toSet()); -// for (String r : roles) { -// System.out.println(r); -// } + public String showChat(Model model, Principal principal) { + model.addAttribute(new ChatMessageDTO()); -// boolean hasUserRole = authentication.getAuthorities().stream() -// .anyMatch(r -> r.getAuthority().equals("ROLE_USER")); + Authentication authentication = SecurityContextHolder.getContext() + .getAuthentication(); boolean isAdmin = authentication.getAuthorities().stream() - .anyMatch(r -> r.getAuthority().equals("ROLE_ADMIN") || r.getAuthority().equals("ROLE_SUPER_USER")); - System.out.println("Is admin? " + isAdmin); - modelAndView.addObject("activeUsers", userService.getOtherActiveUsers(principal.getName())); - return modelAndView; + .anyMatch(r -> r.getAuthority().equals("ROLE_ADMIN") + || r.getAuthority().equals("ROLE_SUPER_USER")); + log.trace("Is admin? " + isAdmin); + model.addAttribute("activeUsers", + userService.getOtherActiveUsers(principal.getName())); + return "chat"; } -// public String showHome(Model model) -// { -// model.addAttribute("message", "Welcome"); -// return "home"; -// } }