2019-11-19 19:34:57 +01:00
package org.hso.ecommerce.app ;
2020-04-29 22:44:16 +02:00
import org.hso.ecommerce.entities.user.User ;
2020-05-10 16:57:33 +02:00
import org.hso.ecommerce.repos.user.UserRepository ;
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 ;
2020-05-10 16:57:33 +02:00
import org.springframework.web.bind.annotation.GetMapping ;
import org.springframework.web.bind.annotation.PostMapping ;
import org.springframework.web.bind.annotation.RequestParam ;
2019-12-08 12:24:28 +01:00
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 ;
2020-05-01 14:53:35 +02:00
static int notSoRandom = 0 ;
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-05-02 14:38:40 +02:00
if ( ! user . isPresent ( ) ) {
2020-04-08 18:27:46 +02:00
request . setAttribute ( " error " , " Email Adresse falsch. " ) ;
response . setStatus ( HttpServletResponse . SC_EXPECTATION_FAILED ) ;
return " login " ;
}
2020-05-24 21:51:40 +02:00
if ( ! user . get ( ) . validatePassword ( password ) ) {
2020-04-08 18:27:46 +02:00
request . setAttribute ( " error " , " Passwort falsch. " ) ;
response . setStatus ( HttpServletResponse . SC_EXPECTATION_FAILED ) ;
return " login " ;
}
2020-05-24 21:51:40 +02:00
if ( ! user . get ( ) . isActive ) {
request . setAttribute ( " error " , " User ist deaktiviert. " ) ;
response . setStatus ( HttpServletResponse . SC_EXPECTATION_FAILED ) ;
return " login " ;
}
2020-05-11 15:32:39 +02:00
2020-04-08 18:27:46 +02:00
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-05-01 14:53:35 +02:00
@GetMapping ( " /intern/ " )
public String intern ( ) {
return " intern/index " ;
}
2020-05-15 19:45:10 +02:00
2020-05-01 14:53:35 +02:00
@GetMapping ( " /intern/customers/ " )
public String internCustomers ( ) {
return " intern/customers/index " ;
}
@GetMapping ( " /intern/customers/{id} " )
public String internCustomersId ( ) {
return " intern/customers/id " ;
}
@GetMapping ( " /intern/customerOrders/ " )
public String internCustomerOrder ( ) {
return " intern/customerOrders/index " ;
}
@GetMapping ( " /intern/customerOrders/{id} " )
public String internCustomerOrdersId ( ) {
return " intern/customerOrders/id " ;
}
@GetMapping ( " /intern/suppliers/ " )
public String internSuppliers ( ) {
return " intern/suppliers/index " ;
}
@GetMapping ( " /intern/suppliers/{id} " )
public String internSuppliersId ( ) {
return " intern/suppliers/id " ;
}
@GetMapping ( " /intern/supplierOrders/ " )
public String internSupplierOrders ( ) {
return " intern/supplierOrders/index " ;
}
@GetMapping ( " /intern/supplierOrders/{id} " )
public String internSupplierOrdersId ( ) {
return " intern/supplierOrders/id " ;
}
@GetMapping ( " /intern/accounting/ " )
public String accounting ( ) {
return " intern/accounting/index " ;
}
@GetMapping ( " /intern/accounting/vat " )
public String accountingVat ( ) {
return " intern/accounting/vat " ;
}
@GetMapping ( " /intern/accounting/main " )
public String accountingIntern ( ) {
return " intern/accounting/main " ;
}
@GetMapping ( " /intern/accounting/addManual " )
public String accountingAddManual ( ) {
return " intern/accounting/addManual " ;
}
2019-11-19 19:34:57 +01:00
}