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 ;
import java.util.Optional ;
@Controller
public class RegisterController {
@Autowired
private final UserRepository userRepository = null ;
@PostMapping ( " /register " )
2020-05-24 21:51:40 +02:00
public String registerPost (
2020-05-19 15:41:43 +02:00
HttpServletRequest request ,
HttpServletResponse response ,
@RequestParam ( " username " ) String username ,
@RequestParam ( " password " ) String password ,
@RequestParam ( " password2 " ) String password2 ,
@RequestParam ( " salutation " ) String salutation ,
@RequestParam ( " name " ) String name ,
2020-05-27 14:55:51 +02:00
@RequestParam ( " address " ) String address
2020-05-19 15:41:43 +02:00
)
{
Optional < User > user = userRepository . findByEmail ( username ) ;
if ( user . isPresent ( ) ) {
request . setAttribute ( " error " , " Email Adresse existiert bereits! " ) ;
2020-05-24 21:51:40 +02:00
response . setStatus ( HttpServletResponse . SC_EXPECTATION_FAILED ) ;
2020-05-19 15:41:43 +02:00
return " register " ;
}
if ( ! password . equals ( password2 ) ) {
request . setAttribute ( " error " , " Passwörter sind nicht gleich " ) ;
response . setStatus ( HttpServletResponse . SC_BAD_REQUEST ) ;
return " register " ;
}
2020-05-19 18:47:15 +02:00
//set values for new user
2020-05-19 15:41:43 +02:00
User newUser = new User ( ) ;
2020-05-19 18:47:15 +02:00
newUser . email = username ;
newUser . setPassword ( password ) ;
newUser . email = username ;
2020-05-24 21:51:40 +02:00
newUser . isEmployee = false ;
2020-05-27 14:55:51 +02:00
newUser . salutation = salutation ;
2020-05-24 21:51:40 +02:00
2020-05-19 18:47:15 +02:00
newUser . isActive = true ;
newUser . created = new java . sql . Timestamp ( System . currentTimeMillis ( ) ) ;
2020-05-19 15:41:43 +02:00
2020-05-19 18:47:15 +02:00
Address newAddress = new Address ( ) ;
newAddress . name = name ;
newAddress . addressString = address ;
newUser . defaultDeliveryAddress = newAddress ;
2020-05-19 15:41:43 +02:00
2020-05-19 18:47:15 +02:00
userRepository . save ( newUser ) ; // save newUser
2020-05-27 12:22:24 +02:00
return " redirect:/login " ;
2020-05-19 15:41:43 +02:00
}
@GetMapping ( " /register " )
public String register ( ) {
return " register " ;
}
}