Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Controller
- public class FavoriteController {
- @Autowired
- private final FavoriteService favoriteService;
- @Autowired
- public FavoriteController(FavoriteService favoriteService) {
- this.favoriteService = favoriteService;
- }
- @RequestMapping(value = "/rootScreen.do",method = RequestMethod.POST)
- public ModelAndView login(ModelMap modelMap,HttpServletRequest request)
- {
- ModelAndView modelAndView = new ModelAndView();
- String username = (String) request.getParameter("username");
- String password = (String) request.getParameter("password");
- if(favoriteService.checkLogin(username,password))
- {
- modelAndView.setViewName("normalScreen");
- }else if (favoriteService.checkRoot(username,password)) {
- modelAndView.setViewName("rootScreen");
- }else {
- modelMap.put("error", "Wrong username or password");
- modelAndView.setViewName("index");
- }
- User user = new User(username,password);
- request.getSession().setAttribute("rootUser",user);
- return modelAndView;
- }
- @RequestMapping(value = "/addUser.do",method = RequestMethod.POST)
- public ModelAndView addUser(HttpServletRequest request)
- {
- User rootUser = (User) request.getSession().getAttribute("rootUser");
- User normalUser= new User(request.getParameter("newusername"),request.getParameter("newpassword"));
- ModelAndView modelAndView = new ModelAndView();
- favoriteService.addUser(rootUser.getUsername(),rootUser.getPassword(),normalUser.getUsername(),normalUser.getPassword());
- if(favoriteService.checkLogin(normalUser.getUsername(),normalUser.getPassword()))
- {
- modelAndView.setViewName("normalScreen");
- }else modelAndView.setViewName("rootScreen");
- request.getSession().setAttribute("normalUser",normalUser);
- return modelAndView;
- }
- @RequestMapping(value = "/addFavorite.do",method = RequestMethod.POST)
- public ModelAndView addFavorite(ModelAndView mav,HttpServletRequest request)
- {
- User normalUser = (User) request.getSession().getAttribute("normalUser");
- favoriteService.addFavorite(normalUser.getUsername(),normalUser.getPassword(),request.getParameter("favorite"));
- mav.setViewName("redirect:/getAllFavorites.do");
- return mav;
- }
- @RequestMapping(value = "/getAllFavorites.do",method = RequestMethod.GET)
- public ModelAndView getAllFavorites(HttpServletRequest request,ModelAndView mav)
- {
- User user = (User) request.getSession().getAttribute("normalUser");
- List<String> favs = favoriteService.getFavorites(user.getUsername(),user.getPassword());
- mav.setViewName("normalScreen");
- mav.addObject("favs",favs);
- return mav;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement