2019-11-19 19:34:57 +01:00
package org.hso.ecommerce.app ;
2020-04-08 18:27:46 +02:00
import org.hso.ecommerce.db.repos.UserRepository ;
import org.hso.ecommerce.entities.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 ;
static int notSoRandom = 0 ;
2020-02-03 20:53:02 +01:00
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 ) ;
if ( user . isEmpty ( ) ) {
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-02-11 23:36:34 +01: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
) {
return " redirect:/ " ;
}
2020-01-23 19:08:04 +01:00
@GetMapping ( " /shop/ " )
public String shop ( ) {
2020-02-09 15:00:07 +01:00
return " shop/index " ;
2020-01-18 15:34:54 +01:00
}
2020-01-23 19:08:04 +01:00
@GetMapping ( " /shop/search " )
public String shopSearch ( ) {
2020-02-09 15:00:07 +01:00
return " shop/search " ;
2020-01-15 10:55:24 +01:00
}
2020-01-11 16:39:34 +01:00
2020-01-25 20:30:26 +01:00
@GetMapping ( " /shop/checkout " )
2020-04-08 18:27:46 +02:00
public String shopCheckout ( HttpSession session , HttpServletRequest request ) {
session . setAttribute ( " afterLogin " , request . getRequestURI ( ) ) ;
2020-02-09 15:00:07 +01:00
return " shop/checkout " ;
2020-01-25 20:30:26 +01:00
}
@PostMapping ( " /shop/checkoutFinish " )
public String shopCheckoutFinish ( ) {
2020-02-09 15:00:07 +01:00
return " shop/checkoutFinish " ;
2020-01-25 20:30:26 +01:00
}
2020-04-08 18:27:46 +02:00
2020-01-25 20:30:26 +01:00
@GetMapping ( " /shop/checkoutFinish " )
public String shopCheckoutFinishGET ( ) {
2020-02-09 15:00:07 +01:00
return " shop/checkoutFinish " ;
2020-01-25 20:30:26 +01:00
}
2020-01-23 19:08:04 +01:00
@GetMapping ( " /shop/articles/{id} " )
public String shopArticlesById ( ) {
2020-02-09 15:00:07 +01:00
return " shop/articles/id " ;
2020-01-22 19:27:45 +01:00
}
2020-01-16 21:01:15 +01:00
2020-01-25 20:30:26 +01:00
@PostMapping ( " /shop/articles/{id} " )
2020-04-08 18:27:46 +02:00
public String shopArticlesByIdBuy ( HttpSession session ,
@RequestAttribute ( value = " user " , required = false ) User customer ,
@PathVariable ( " id " ) Integer id ,
@RequestParam ( " fastcheckout " ) Boolean fastcheckout
) {
if ( customer ! = null ) {
2020-03-06 15:19:56 +01:00
if ( ! fastcheckout ) {
return " shop/articles/post_add " ;
} else {
return " shop/checkout " ;
}
2020-02-11 23:36:34 +01:00
} else {
2020-04-08 18:27:46 +02:00
session . setAttribute ( " afterLogin " , " /shop/articles/ " + id ) ;
return " redirect:/login " ;
2020-02-11 23:36:34 +01:00
}
2020-01-25 20:30:26 +01:00
}
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
2020-01-23 19:08:04 +01:00
@GetMapping ( " /intern/ " )
public String intern ( ) {
2020-02-09 15:00:07 +01:00
return " intern/index " ;
2020-01-22 19:27:45 +01:00
}
2020-01-20 20:59:35 +01:00
2020-01-23 19:08:04 +01:00
@GetMapping ( " /intern/listedArticles/ " )
public String internListedArticles ( ) {
2020-02-09 15:00:07 +01:00
return " intern/listedArticles/index " ;
2020-01-22 19:27:45 +01:00
}
2020-02-01 23:43:44 +01:00
@GetMapping ( " /intern/listedArticles/{id} " )
public String internListedArticlesId ( ) {
2020-02-09 15:00:07 +01:00
return " intern/listedArticles/id " ;
2020-02-01 23:43:44 +01:00
}
2020-01-23 19:08:04 +01:00
@GetMapping ( " /intern/articles/ " )
public String internArticles ( ) {
2020-02-09 15:00:07 +01:00
return " intern/articles/index " ;
2020-01-22 19:27:45 +01:00
}
2020-01-20 20:59:35 +01:00
2020-01-23 19:08:04 +01:00
@GetMapping ( " /intern/articles/{id} " )
public String internArticlesId ( ) {
2020-02-09 15:00:07 +01:00
return " intern/articles/id " ;
2020-01-22 19:27:45 +01:00
}
2020-01-20 20:59:35 +01:00
2020-01-23 19:08:04 +01:00
@GetMapping ( " /intern/customers/ " )
public String internCustomers ( ) {
2020-02-09 15:00:07 +01:00
return " intern/customers/index " ;
2020-01-22 19:27:45 +01:00
}
2020-01-20 20:59:35 +01:00
2020-01-23 19:08:04 +01:00
@GetMapping ( " /intern/customers/{id} " )
public String internCustomersId ( ) {
2020-02-09 15:00:07 +01:00
return " intern/customers/id " ;
2020-01-15 10:55:24 +01:00
}
2020-01-23 19:08:04 +01:00
@GetMapping ( " /intern/customerOrders/ " )
public String internCustomerOrder ( ) {
2020-02-09 15:00:07 +01:00
return " intern/customerOrders/index " ;
2020-01-22 19:27:45 +01:00
}
2020-01-23 19:08:04 +01:00
@GetMapping ( " /intern/customerOrders/{id} " )
public String internCustomerOrdersId ( ) {
2020-02-09 15:00:07 +01:00
return " intern/customerOrders/id " ;
2020-01-22 19:27:45 +01:00
}
2020-01-23 19:08:04 +01:00
@GetMapping ( " /intern/suppliers/ " )
public String internSuppliers ( ) {
2020-02-09 15:00:07 +01:00
return " intern/suppliers/index " ;
2020-01-15 10:55:24 +01:00
}
2020-01-12 19:42:04 +01:00
2020-01-23 19:08:04 +01:00
@GetMapping ( " /intern/suppliers/{id} " )
public String internSuppliersId ( ) {
2020-02-09 15:00:07 +01:00
return " intern/suppliers/id " ;
2019-12-08 17:16:17 +01:00
}
2020-01-22 19:27:45 +01:00
2020-01-23 19:08:04 +01:00
@GetMapping ( " /intern/supplierOrders/ " )
public String internSupplierOrders ( ) {
2020-02-09 15:00:07 +01:00
return " intern/supplierOrders/index " ;
2019-12-08 17:16:17 +01:00
}
2019-12-08 12:24:28 +01:00
2020-01-23 19:08:04 +01:00
@GetMapping ( " /intern/supplierOrders/{id} " )
public String internSupplierOrdersId ( ) {
2020-02-09 15:00:07 +01:00
return " intern/supplierOrders/id " ;
2019-12-08 17:16:17 +01:00
}
2020-01-23 19:08:04 +01:00
@GetMapping ( " /intern/accounting/ " )
public String accounting ( ) {
2020-02-09 15:00:07 +01:00
return " intern/accounting/index " ;
2020-01-23 19:08:04 +01:00
}
2019-12-08 17:16:17 +01:00
2020-01-23 19:08:04 +01:00
@GetMapping ( " /intern/accounting/vat " )
public String accountingVat ( ) {
2020-02-09 15:00:07 +01:00
return " intern/accounting/vat " ;
2019-12-08 12:24:28 +01:00
}
2020-01-27 20:37:54 +01:00
@GetMapping ( " /intern/accounting/main " )
2020-01-23 19:08:04 +01:00
public String accountingIntern ( ) {
2020-02-09 15:00:07 +01:00
return " intern/accounting/main " ;
2020-01-15 10:55:24 +01:00
}
2020-01-23 19:08:04 +01:00
@GetMapping ( " /intern/accounting/addManual " )
public String accountingAddManual ( ) {
2020-02-09 15:00:07 +01:00
return " intern/accounting/addManual " ;
2020-01-15 10:55:24 +01:00
}
2020-01-23 19:08:04 +01:00
@GetMapping ( " /intern/warehouse/ " )
public String accountingWarehouse ( ) {
2020-02-09 15:00:07 +01:00
return " intern/warehouse/index " ;
2020-02-03 20:53:02 +01:00
}
@GetMapping ( " /intern/warehouse/todo " )
public String accountingWarehouseTodo ( ) {
2020-02-09 15:00:07 +01:00
return " intern/warehouse/todo " ;
2020-01-15 10:55:24 +01:00
}
2020-01-23 19:08:04 +01:00
@GetMapping ( " /intern/warehouse/addManual " )
public String accountingWarehouseAddManual ( ) {
2020-02-09 15:00:07 +01:00
return " intern/warehouse/addManual " ;
2020-01-23 19:08:04 +01:00
}
2020-02-03 20:53:02 +01:00
@PostMapping ( " /intern/warehouse/progress/{id} " )
public String accountingWarehouseProgressIdPost ( HttpServletResponse response ) {
if ( ( notSoRandom + + ) % 2 = = 1 ) {
return " redirect:/intern/warehouse/progress/450 " ;
} else {
response . setStatus ( 409 ) ;
2020-02-09 15:00:07 +01:00
return " intern/warehouse/error_progress_failed " ;
2020-02-03 20:53:02 +01:00
}
}
@GetMapping ( " /intern/warehouse/progress/{id} " )
public String accountingWarehouseProgressId ( ) {
2020-02-09 15:00:07 +01:00
return " intern/warehouse/id_progress " ;
2020-02-03 20:53:02 +01:00
}
@GetMapping ( " /intern/warehouse/slots/ " )
public String accountingWarehouseSlots ( ) {
2020-02-09 15:00:07 +01:00
return " intern/warehouse/slots/index " ;
2020-02-03 20:53:02 +01:00
}
2019-11-19 19:34:57 +01:00
}