package com.pollistics.controllers;

import com.pollistics.models.SecUserDetails;
import com.pollistics.models.User;
import com.pollistics.models.validators.UserValidator;
import com.pollistics.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Controller
public class AccountController {
	@Autowired
	private UserService userService;

	@GetMapping(value = "/account")
	public String account(Model model) {
		return "account/index";
	}

	@GetMapping(value = "/login")
	public String login(HttpServletRequest request, Model model) {
		model.addAttribute("request", request);
		return "account/login";
	}

	@GetMapping(value = "/logout")
	public String logout(HttpServletRequest request, HttpServletResponse response, Model model) {
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		if (auth != null){
			new SecurityContextLogoutHandler().logout(request, response, auth);
		}
		return "redirect:/login?logout";
	}

	@GetMapping(value = "/register")
	public String register(Model model) {
		return "account/register";
	}

	@PostMapping(value = "/register")
	public String postRegister(HttpServletRequest request, @Valid @ModelAttribute("user") User user, BindingResult result, Model model) {
		if(!request.getParameter("password1").equals(request.getParameter("password"))) {
			result.addError(new FieldError("user", "password1", "Passwords don't match"));
		}
		if(userService.userExists(user.getUsername())) {
			result.addError(new FieldError("user", "username", "Username is already taken"));
		}

		UserValidator userValidator = new UserValidator();
		userValidator.validate(user, result);

		if (result.hasErrors()){
			model.addAttribute("errors", result);
			return "account/register";
		}
		else {
			userService.createUser(user);
			return "redirect:/";
		}
	}
	
	@GetMapping(value = "/account/polls")
	public String polls(Model model) {
		SecUserDetails user = (SecUserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		model.addAttribute("polls", user.getUser().getPolls());
		return "account/polls";
	}
}

@@ -34,53 +34,8 @@
 s;%0A%0A
-import com.pollistics.models.SecUserDetails;%0A
 impo
@@ -2570,191 +2570,11 @@
 lls(
-Model model) %7B%0A%09%09SecUserDetails user = (SecUserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();%0A%09%09model.addAttribute(%22polls%22, user.getUser().getPolls());
+) %7B
 %0A%09%09r

Remove unused code