2020-04-29 22:44:16 +02:00
package org.hso.ecommerce.controller ;
2020-04-28 22:41:29 +02:00
2020-06-18 00:50:43 +02:00
import org.hso.ecommerce.action.user.CreateDeliveryData ;
2020-05-19 15:18:48 +02:00
import org.hso.ecommerce.action.user.UpdateUserSettingsAction ;
2020-06-13 21:59:18 +02:00
import org.hso.ecommerce.api.RestServiceForDelivery ;
2020-06-24 01:19:37 +02:00
import org.hso.ecommerce.app.config.AppSettings ;
2020-05-19 11:52:41 +02:00
import org.hso.ecommerce.entities.shop.CustomerOrder ;
2020-05-18 14:41:33 +02:00
import org.hso.ecommerce.entities.user.User ;
2020-05-19 11:52:41 +02:00
import org.hso.ecommerce.repos.shop.CustomerOrderRepository ;
2020-05-18 14:41:33 +02:00
import org.hso.ecommerce.repos.user.UserRepository ;
2020-06-17 17:23:29 +02:00
import org.hso.ecommerce.uimodel.DeliveryData ;
2020-05-18 14:41:33 +02:00
import org.springframework.beans.factory.annotation.Autowired ;
2020-04-28 22:41:29 +02:00
import org.springframework.stereotype.Controller ;
2020-05-18 14:41:33 +02:00
import org.springframework.ui.Model ;
2020-06-21 18:58:15 +02:00
import org.springframework.web.bind.annotation.* ;
2020-05-18 14:41:33 +02:00
2020-05-20 12:08:57 +02:00
import javax.servlet.http.HttpServletRequest ;
2020-05-18 14:41:33 +02:00
import javax.servlet.http.HttpSession ;
2020-05-19 11:52:41 +02:00
import java.util.List ;
2020-06-13 21:59:18 +02:00
import java.util.stream.Collectors ;
2020-04-28 22:41:29 +02:00
@Controller
2020-05-18 14:41:33 +02:00
@RequestMapping ( " /user " )
2020-04-29 22:44:16 +02:00
public class UserController {
2020-05-18 14:41:33 +02:00
@Autowired
private final UserRepository userRepository = null ;
2020-05-19 11:52:41 +02:00
@Autowired
private final CustomerOrderRepository customerOrderRepository = null ;
2020-06-13 21:59:18 +02:00
@Autowired
private final RestServiceForDelivery restServiceForDelivery = null ;
2020-06-24 01:19:37 +02:00
@Autowired
private final AppSettings appSettings = null ;
2020-05-18 14:41:33 +02:00
@GetMapping ( " / " )
public String user ( ) {
return " redirect:/user/settings " ;
}
@GetMapping ( " /settings " )
public String userSettings ( Model model ,
2020-06-21 18:58:15 +02:00
@RequestAttribute ( " user " ) User user
2020-05-18 14:41:33 +02:00
) {
model . addAttribute ( " user " , user ) ;
return " user/settings " ;
}
@GetMapping ( " /orders/ " )
2020-06-21 18:58:15 +02:00
public String userOrders (
@RequestAttribute ( " user " ) User user ,
Model model
2020-05-20 12:08:57 +02:00
) {
2020-06-21 18:58:15 +02:00
List < CustomerOrder > orders = customerOrderRepository . getOrdersByUserId ( user . id ) ;
2020-06-13 21:59:18 +02:00
2020-06-21 18:58:15 +02:00
List < CustomerOrderDelivery > customerOrderDeliveryDataMap = orders
. stream ( )
. map ( o - > new CustomerOrderDelivery ( o , CreateDeliveryData . getDeliveryDataFromCustomerOrder ( o , customerOrderRepository , restServiceForDelivery ) ) )
. collect ( Collectors . toList ( ) ) ;
2020-06-13 21:59:18 +02:00
model . addAttribute ( " orderDeliveryDataMap " , customerOrderDeliveryDataMap ) ;
2020-06-24 01:19:37 +02:00
model . addAttribute ( " deliveryService " , appSettings . getParcelServiceName ( ) ) ;
2020-05-19 11:52:41 +02:00
2020-05-18 14:41:33 +02:00
return " user/orders/index " ;
}
2020-06-21 18:58:15 +02:00
static class CustomerOrderDelivery {
private CustomerOrder customerOrder ;
private DeliveryData deliveryData ;
public CustomerOrderDelivery ( CustomerOrder customerOrder , DeliveryData deliveryData ) {
this . customerOrder = customerOrder ;
this . deliveryData = deliveryData ;
}
public CustomerOrder getCustomerOrder ( ) {
return customerOrder ;
}
public DeliveryData getDeliveryData ( ) {
return deliveryData ;
}
}
2020-05-19 11:52:41 +02:00
@PostMapping ( " /settings/changeMail " )
2020-05-19 15:18:48 +02:00
public String changeMail ( HttpSession session ,
2020-05-20 12:08:57 +02:00
@RequestParam ( " email " ) String email ,
HttpServletRequest request
) {
2020-05-19 15:18:48 +02:00
User user = userRepository . findById ( ( long ) session . getAttribute ( " userId " ) ) . get ( ) ;
2020-05-19 11:52:41 +02:00
2020-05-19 15:18:48 +02:00
UpdateUserSettingsAction cusa = new UpdateUserSettingsAction ( user , userRepository ) ;
2020-05-20 12:08:57 +02:00
UpdateUserSettingsAction . UpdateResult result = cusa . updateEmail ( email ) ;
if ( result . updated = = false ) {
request . setAttribute ( " error " , result . errorString ) ;
2020-05-25 10:10:42 +02:00
return " user/settings " ;
2020-05-20 12:08:57 +02:00
}
2020-05-19 11:52:41 +02:00
2020-05-25 10:10:42 +02:00
return " redirect:/user/settings " ;
2020-05-19 11:52:41 +02:00
}
@PostMapping ( " /settings/changePwd " )
2020-05-19 15:18:48 +02:00
public String changePwd ( HttpSession session ,
@RequestParam ( " old-password " ) String oldPassword ,
@RequestParam ( " password1 " ) String password1 ,
2020-05-20 12:08:57 +02:00
@RequestParam ( " password2 " ) String password2 ,
HttpServletRequest request
) {
2020-05-19 15:18:48 +02:00
User user = userRepository . findById ( ( long ) session . getAttribute ( " userId " ) ) . get ( ) ;
2020-05-19 11:52:41 +02:00
2020-05-19 15:18:48 +02:00
UpdateUserSettingsAction cusa = new UpdateUserSettingsAction ( user , userRepository ) ;
2020-05-20 12:08:57 +02:00
UpdateUserSettingsAction . UpdateResult result = cusa . updatePassword ( oldPassword , password1 , password2 ) ;
if ( result . updated = = false ) {
request . setAttribute ( " error " , result . errorString ) ;
2020-05-25 10:10:42 +02:00
return " user/settings " ;
2020-05-20 12:08:57 +02:00
}
2020-05-19 11:52:41 +02:00
2020-05-25 10:10:42 +02:00
return " redirect:/user/settings " ;
2020-05-19 11:52:41 +02:00
}
@PostMapping ( " /settings/changeAddress " )
2020-05-19 15:18:48 +02:00
public String changeAddress ( HttpSession session ,
@RequestParam ( " salutation " ) String salutation ,
@RequestParam ( " name " ) String name ,
2020-05-20 12:08:57 +02:00
@RequestParam ( " address " ) String address ,
HttpServletRequest request
) {
2020-05-19 15:18:48 +02:00
User user = userRepository . findById ( ( long ) session . getAttribute ( " userId " ) ) . get ( ) ;
2020-05-19 11:52:41 +02:00
2020-05-19 15:18:48 +02:00
UpdateUserSettingsAction cusa = new UpdateUserSettingsAction ( user , userRepository ) ;
2020-05-20 12:08:57 +02:00
UpdateUserSettingsAction . UpdateResult result = cusa . updateShippingInfo ( salutation , name , address ) ;
if ( result . updated = = false ) {
request . setAttribute ( " error " , result . errorString ) ;
2020-05-25 10:10:42 +02:00
return " user/settings " ;
2020-05-20 12:08:57 +02:00
}
2020-05-19 11:52:41 +02:00
2020-05-25 10:10:42 +02:00
return " redirect:/user/settings " ;
2020-05-19 11:52:41 +02:00
}
@PostMapping ( " /settings/changePaymentInfo " )
2020-05-19 15:18:48 +02:00
public String changePaymentInfo ( HttpSession session ,
2020-05-20 12:08:57 +02:00
@RequestParam ( " creditCardNumber " ) String creditCardNumber ,
HttpServletRequest request
) {
2020-05-19 15:18:48 +02:00
User user = userRepository . findById ( ( long ) session . getAttribute ( " userId " ) ) . get ( ) ;
2020-05-19 11:52:41 +02:00
2020-05-19 15:18:48 +02:00
UpdateUserSettingsAction cusa = new UpdateUserSettingsAction ( user , userRepository ) ;
2020-05-20 12:08:57 +02:00
UpdateUserSettingsAction . UpdateResult result = cusa . updatePaymentInfo ( creditCardNumber ) ;
if ( result . updated = = false ) {
request . setAttribute ( " error " , result . errorString ) ;
2020-05-25 10:10:42 +02:00
return " user/settings " ;
2020-05-20 12:08:57 +02:00
}
2020-05-19 11:52:41 +02:00
2020-05-25 10:10:42 +02:00
return " redirect:/user/settings " ;
2020-05-19 11:52:41 +02:00
}
2020-04-28 22:41:29 +02:00
}