Advertisement
Guest User

Untitled

a guest
Dec 19th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.83 KB | None | 0 0
  1. @Controller
  2. public class FavoriteController {
  3.  
  4.     @Autowired
  5.     private final FavoriteService favoriteService;
  6.  
  7.     @Autowired
  8.     public FavoriteController(FavoriteService favoriteService) {
  9.         this.favoriteService = favoriteService;
  10.     }
  11.  
  12.     @RequestMapping(value = "/rootScreen.do",method = RequestMethod.POST)
  13.     public ModelAndView login(ModelMap modelMap,HttpServletRequest request)
  14.     {
  15.         ModelAndView modelAndView = new ModelAndView();
  16.         String username = (String) request.getParameter("username");
  17.         String password = (String) request.getParameter("password");
  18.  
  19.         if(favoriteService.checkLogin(username,password))
  20.         {
  21.             modelAndView.setViewName("normalScreen");
  22.         }else if (favoriteService.checkRoot(username,password)) {
  23.             modelAndView.setViewName("rootScreen");
  24.         }else {
  25.             modelMap.put("error", "Wrong username or password");
  26.             modelAndView.setViewName("index");
  27.         }
  28.         User user = new User(username,password);
  29.         request.getSession().setAttribute("rootUser",user);
  30.         return modelAndView;
  31.     }
  32.     @RequestMapping(value = "/addUser.do",method = RequestMethod.POST)
  33.     public ModelAndView addUser(HttpServletRequest request)
  34.     {
  35.         User rootUser = (User) request.getSession().getAttribute("rootUser");
  36.         User normalUser= new User(request.getParameter("newusername"),request.getParameter("newpassword"));
  37.  
  38.         ModelAndView modelAndView = new ModelAndView();
  39.         favoriteService.addUser(rootUser.getUsername(),rootUser.getPassword(),normalUser.getUsername(),normalUser.getPassword());
  40.         if(favoriteService.checkLogin(normalUser.getUsername(),normalUser.getPassword()))
  41.         {
  42.             modelAndView.setViewName("normalScreen");
  43.         }else  modelAndView.setViewName("rootScreen");
  44.  
  45.         request.getSession().setAttribute("normalUser",normalUser);
  46.         return modelAndView;
  47.     }
  48.     @RequestMapping(value = "/addFavorite.do",method = RequestMethod.POST)
  49.     public ModelAndView addFavorite(ModelAndView mav,HttpServletRequest request)
  50.     {
  51.         User normalUser = (User) request.getSession().getAttribute("normalUser");
  52.         favoriteService.addFavorite(normalUser.getUsername(),normalUser.getPassword(),request.getParameter("favorite"));
  53.         mav.setViewName("redirect:/getAllFavorites.do");
  54.         return mav;
  55.     }
  56.     @RequestMapping(value = "/getAllFavorites.do",method = RequestMethod.GET)
  57.     public ModelAndView getAllFavorites(HttpServletRequest request,ModelAndView mav)
  58.     {
  59.         User user = (User) request.getSession().getAttribute("normalUser");
  60.         List<String> favs = favoriteService.getFavorites(user.getUsername(),user.getPassword());
  61.         mav.setViewName("normalScreen");
  62.         mav.addObject("favs",favs);
  63.         return mav;
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement