2019-11-19 19:34:57 +01:00
package org.hso.ecommerce.app ;
2019-12-08 17:16:17 +01:00
import org.hso.ecommerce.db.CustomerRepository ;
import org.hso.ecommerce.entities.Customer ;
2019-11-19 19:34:57 +01:00
import org.springframework.stereotype.Controller ;
import org.springframework.ui.Model ;
2019-12-08 12:24:28 +01:00
import org.springframework.web.bind.annotation.* ;
import javax.servlet.http.Cookie ;
import javax.servlet.http.HttpServletResponse ;
2019-12-08 17:16:17 +01:00
import java.util.List ;
2019-12-08 12:24:28 +01:00
import java.util.UUID ;
2019-11-19 19:34:57 +01:00
@Controller
2019-11-26 16:21:29 +01:00
public class RequestController {
2019-11-19 19:34:57 +01:00
2019-12-08 17:16:17 +01:00
private final CustomerRepository customerRepo ;
public RequestController ( CustomerRepository customerRepo ) {
this . customerRepo = customerRepo ;
}
2019-11-26 16:21:29 +01:00
@GetMapping ( " / " )
public String greeting ( ) {
return " redirect:/home " ;
2019-11-19 19:34:57 +01:00
}
2019-11-25 16:15:36 +01:00
@GetMapping ( " /home " )
2019-12-08 12:24:28 +01:00
public String home ( Model model ) {
2019-12-08 17:16:17 +01:00
model . addAttribute ( new Customer ( ) ) ;
2019-11-25 16:15:36 +01:00
return " home " ;
}
2019-11-26 16:21:29 +01:00
@GetMapping ( " /greeting " )
public String greeting ( @RequestParam ( name = " name " , required = false , defaultValue = " World " ) String name , Model model ) {
model . addAttribute ( " name " , name ) ;
return " greeting " ;
}
2019-12-08 17:16:17 +01:00
@GetMapping ( " /login " )
public String login ( @CookieValue ( value = " loginToken " , defaultValue = " " ) String loginToken , Model model ) {
model . addAttribute ( new Customer ( ) ) ;
System . out . println ( loginToken ) ; // TODO if cookie is present, redirect to home
return " login " ;
}
@RequestMapping ( value = " /login " , method = RequestMethod . POST , params = " action=login " )
public String loginAction ( @ModelAttribute Customer customer , HttpServletResponse response ) {
2019-12-08 12:24:28 +01:00
// do the login magic and get a loginToken
2019-12-08 17:16:17 +01:00
System . out . println ( customer . username ) ;
System . out . println ( customer . password ) ;
List < Customer > customers = customerRepo . findByUsername ( customer . username ) ;
if ( customers . size ( ) = = 1 & & ( customers . get ( 0 ) . username . equals ( customer . username ) & & customers . get ( 0 ) . password . equals ( customer . password ) ) ) {
System . out . println ( " The login data is valid " ) ;
String loginToken = UUID . randomUUID ( ) . toString ( ) ;
// set the loginToken as session cookie
Cookie cookie = new Cookie ( " loginToken " , loginToken ) ;
response . addCookie ( cookie ) ;
} else {
System . out . println ( " The login data is invalid! " ) ;
return " redirect:/login " ; // redirect so the input files get cleared, otherwise only pwd gets cleared
}
return " redirect:/home " ;
}
2019-12-08 12:24:28 +01:00
2019-12-08 17:16:17 +01:00
@GetMapping ( " /register " )
public String register ( @CookieValue ( value = " loginToken " , defaultValue = " " ) String loginToken , Model model ) {
model . addAttribute ( new Customer ( ) ) ;
System . out . println ( loginToken ) ; // TODO if cookie is present, redirect to home
return " register " ;
}
@RequestMapping ( value = " /register " , method = RequestMethod . POST , params = " action=register " )
public String registerAction ( @ModelAttribute Customer customer , HttpServletResponse response ) {
// do the register magic and get a loginToken
System . out . println ( customer . username ) ;
System . out . println ( customer . password ) ;
if ( customerRepo . findByUsername ( customer . username ) . size ( ) ! = 0 ) {
// TODO
System . out . println ( " The customer exists already " ) ;
return " register " ;
} else {
customerRepo . save ( customer ) ;
System . out . println ( customerRepo . findByUsername ( customer . username ) . size ( ) ) ;
}
// return a login token after successful registration
2019-12-08 12:24:28 +01:00
String loginToken = UUID . randomUUID ( ) . toString ( ) ;
// set the loginToken as session cookie
Cookie cookie = new Cookie ( " loginToken " , loginToken ) ;
response . addCookie ( cookie ) ;
return " redirect:/home " ;
}
2019-11-19 19:34:57 +01:00
}