2019-11-19 19:34:57 +01:00
package org.hso.ecommerce.app ;
2020-04-29 23:07:47 +02:00
import org.hso.ecommerce.repos.user.UserRepository ;
2020-04-29 22:44:16 +02:00
import org.hso.ecommerce.entities.user.User ;
2020-01-05 01:41:45 +01:00
import org.springframework.beans.factory.annotation.Autowired ;
2019-11-19 19:34:57 +01:00
import org.springframework.stereotype.Controller ;
2019-12-08 12:24:28 +01:00
import org.springframework.web.bind.annotation.* ;
2020-03-06 15:19:56 +01:00
import javax.servlet.http.HttpServletRequest ;
2019-12-08 12:24:28 +01:00
import javax.servlet.http.HttpServletResponse ;
2020-04-08 18:27:46 +02:00
import javax.servlet.http.HttpSession ;
import java.util.Optional ;
2019-11-19 19:34:57 +01:00
2020-01-15 10:55:24 +01:00
/ * *
* TODO clean up this class
* /
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
2020-04-08 18:27:46 +02:00
@Autowired
private final UserRepository userRepository = null ;
2019-11-26 16:21:29 +01:00
@GetMapping ( " / " )
2020-01-23 19:08:04 +01:00
public String home ( ) {
return " redirect:/shop/ " ;
2019-11-19 19:34:57 +01:00
}
2020-01-23 19:08:04 +01:00
@GetMapping ( " /login " )
public String login ( ) {
2020-02-09 15:00:07 +01:00
return " login " ;
2019-11-25 16:15:36 +01:00
}
2020-01-25 20:30:26 +01:00
@PostMapping ( " /login " )
2020-04-08 18:27:46 +02:00
public String loginPost (
HttpServletRequest request ,
HttpServletResponse response ,
@RequestParam ( " username " ) String username ,
@RequestParam ( " password " ) String password ,
HttpSession session
) {
String gto = ( String ) session . getAttribute ( " afterLogin " ) ;
Optional < User > user = userRepository . findByEmail ( username ) ;
2020-04-27 09:48:24 +02:00
if ( user . isEmpty ( ) ) {
2020-04-08 18:27:46 +02:00
request . setAttribute ( " error " , " Email Adresse falsch. " ) ;
response . setStatus ( HttpServletResponse . SC_EXPECTATION_FAILED ) ;
return " login " ;
}
if ( ! user . get ( ) . validatePassword ( password ) ) {
request . setAttribute ( " error " , " Passwort falsch. " ) ;
response . setStatus ( HttpServletResponse . SC_EXPECTATION_FAILED ) ;
return " login " ;
}
session . setAttribute ( " userId " , user . get ( ) . getId ( ) ) ;
2020-02-11 23:36:34 +01:00
2020-04-08 18:27:46 +02:00
if ( gto ! = null & & gto . startsWith ( " / " ) ) {
2020-04-27 09:48:24 +02:00
return " redirect: " + gto ;
} else {
return " redirect:/ " ;
}
2020-01-25 20:30:26 +01:00
}
2020-01-25 22:13:26 +01:00
@PostMapping ( " /logout " )
2020-04-08 18:27:46 +02:00
public String logoutPost ( HttpServletResponse response ,
HttpSession session
) {
session . removeAttribute ( " userId " ) ;
2020-01-25 22:13:26 +01:00
return " redirect:/ " ;
}
2020-01-23 19:08:04 +01:00
@GetMapping ( " /register " )
public String register ( ) {
2020-02-09 15:00:07 +01:00
return " register " ;
2020-01-15 10:55:24 +01:00
}
2020-01-11 16:39:34 +01:00
2020-04-08 18:27:46 +02:00
@PostMapping ( " /register " )
public String registerPost (
@RequestParam ( " username " ) String username ,
@RequestParam ( " password " ) String password ,
@RequestParam ( " password2 " ) String password2 ,
@RequestParam ( " type " ) String type
2020-04-27 09:48:24 +02:00
) {
2020-04-08 18:27:46 +02:00
return " redirect:/ " ;
}
2020-01-23 19:08:04 +01:00
@GetMapping ( " /about " )
public String about ( ) {
2020-02-09 15:00:07 +01:00
return " about " ;
2020-01-15 10:55:24 +01:00
}
2020-01-23 19:08:04 +01:00
@GetMapping ( " /terms " )
public String terms ( ) {
2020-02-09 15:00:07 +01:00
return " terms " ;
2020-01-19 18:46:32 +01:00
}
2020-01-23 19:08:04 +01:00
@GetMapping ( " /privacy " )
public String privacy ( ) {
2020-02-09 15:00:07 +01:00
return " privacy " ;
2020-01-22 19:27:45 +01:00
}
2020-01-20 20:59:35 +01:00
2019-11-19 19:34:57 +01:00
}