2020-05-19 15:41:43 +02:00
package org.hso.ecommerce.controller ;
2020-05-19 18:47:15 +02:00
import org.hso.ecommerce.entities.shop.Address ;
2020-05-19 15:41:43 +02:00
import org.hso.ecommerce.entities.user.User ;
import org.hso.ecommerce.repos.user.UserRepository ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Controller ;
import org.springframework.web.bind.annotation.GetMapping ;
import org.springframework.web.bind.annotation.PostMapping ;
import org.springframework.web.bind.annotation.RequestParam ;
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;
2020-06-01 11:13:03 +02:00
import javax.servlet.http.HttpSession ;
2020-05-19 15:41:43 +02:00
import java.util.Optional ;
@Controller
2020-06-12 23:48:28 +02:00
public class RegisterController {
2020-05-19 15:41:43 +02:00
2020-06-12 23:48:28 +02:00
@Autowired
private final UserRepository userRepository = null ;
2020-05-19 15:41:43 +02:00
2020-06-12 23:48:28 +02:00
@PostMapping ( " /register " )
public String registerPost ( HttpServletRequest request , HttpServletResponse response ,
@RequestParam ( " username " ) String username , @RequestParam ( " password " ) String password ,
@RequestParam ( " password2 " ) String password2 , @RequestParam ( " salutation " ) String salutation ,
@RequestParam ( " name " ) String name , @RequestParam ( " address " ) String address ,
HttpSession session ) {
Optional < User > user = userRepository . findByEmail ( username ) ;
if ( user . isPresent ( ) ) {
2020-06-13 10:41:59 +02:00
request . setAttribute ( " error " , " Die Email Adresse existiert bereits. " ) ;
2020-06-12 23:48:28 +02:00
response . setStatus ( HttpServletResponse . SC_EXPECTATION_FAILED ) ;
return " register " ;
}
2020-05-19 15:41:43 +02:00
2020-06-12 23:48:28 +02:00
if ( ! password . equals ( password2 ) ) {
2020-06-13 10:41:59 +02:00
request . setAttribute ( " error " , " Die Passwörter stimmen nicht überein. " ) ;
2020-06-12 23:48:28 +02:00
response . setStatus ( HttpServletResponse . SC_BAD_REQUEST ) ;
return " register " ;
}
2020-05-19 15:41:43 +02:00
2020-06-12 23:48:28 +02:00
// set values for new user
User newUser = new User ( ) ;
newUser . email = username ;
newUser . setPassword ( password ) ;
newUser . email = username ;
newUser . isEmployee = false ;
newUser . salutation = salutation ;
2020-06-25 11:46:01 +02:00
newUser . defaultPayment = null ;
2020-05-24 21:51:40 +02:00
2020-06-12 23:48:28 +02:00
newUser . isActive = true ;
newUser . created = new java . sql . Timestamp ( System . currentTimeMillis ( ) ) ;
2020-05-19 15:41:43 +02:00
2020-06-12 23:48:28 +02:00
Address newAddress = new Address ( ) ;
newAddress . name = name ;
newAddress . addressString = address ;
newUser . defaultDeliveryAddress = newAddress ;
2020-05-19 15:41:43 +02:00
2020-06-12 23:48:28 +02:00
userRepository . save ( newUser ) ; // save newUser
2020-05-19 18:47:15 +02:00
2020-06-12 23:48:28 +02:00
user = userRepository . findByEmail ( username ) ;
session . setAttribute ( " userId " , user . get ( ) . getId ( ) ) ;
2020-06-01 11:11:34 +02:00
2020-06-12 23:48:28 +02:00
String gto = ( String ) session . getAttribute ( " afterLogin " ) ;
2020-05-19 15:41:43 +02:00
2020-06-12 23:48:28 +02:00
//login after register
if ( gto ! = null & & gto . startsWith ( " / " ) ) {
return " redirect: " + gto ;
} else {
return " redirect:/ " ;
}
}
@GetMapping ( " /register " )
public String register ( ) {
return " register " ;
}
2020-05-19 15:41:43 +02:00
}