add Errorhandling to UpdateSettingsAction
This commit is contained in:
		| @ -1,6 +1,7 @@ | |||||||
| package org.hso.ecommerce.action.user; | package org.hso.ecommerce.action.user; | ||||||
|  |  | ||||||
| import com.sun.xml.bind.v2.TODO; | import com.sun.xml.bind.v2.TODO; | ||||||
|  | import org.hibernate.sql.Update; | ||||||
| import org.hso.ecommerce.entities.user.User; | import org.hso.ecommerce.entities.user.User; | ||||||
| import org.hso.ecommerce.repos.user.UserRepository; | import org.hso.ecommerce.repos.user.UserRepository; | ||||||
| import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||||
| @ -15,49 +16,59 @@ public class UpdateUserSettingsAction { | |||||||
|         this.repository = repository; |         this.repository = repository; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public void updateEmail(String newMail){ |     public UpdateResult updateEmail(String newMail){ | ||||||
|         if(newMail.equals("")){ |         UpdateResult result = new UpdateResult(false); | ||||||
|             //TODO: Errorhandling |         if(!newMail.contains("@")){ | ||||||
|  |             result.errorString = "Ändern der Email-Addresse nicht möglich. Bitte versuchen Sie es erneut."; | ||||||
|         }else{ |         }else{ | ||||||
|             this.user.email = newMail; |             this.user.email = newMail; | ||||||
|             this.repository.save(this.user); //TODO: Errorhandling |             this.repository.save(this.user); | ||||||
|  |             result.updated = true; | ||||||
|         } |         } | ||||||
|  |         return result; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public void updatePassword(String oldPassword, String password1, String password2){ |     public UpdateResult updatePassword(String oldPassword, String password1, String password2){ | ||||||
|  |         UpdateResult result = new UpdateResult(false); | ||||||
|         if(this.user.validatePassword(oldPassword)) |         if(this.user.validatePassword(oldPassword)) | ||||||
|         { |         { | ||||||
|             if(password1.equals(password2)){ |             if(password1.equals(password2)){ | ||||||
|                 this.user.setPassword(password1); |                 this.user.setPassword(password1); | ||||||
|                 this.repository.save(this.user); |                 this.repository.save(this.user); | ||||||
|  |                 result.updated = true; | ||||||
|             }else{ |             }else{ | ||||||
|                 //TODO Errorhandling |                 result.errorString = "Die beiden neuen Passwörter stimmen nicht überein. Bitte versuchen Sie es erneut."; | ||||||
|             } |             } | ||||||
|         }else{ |         }else{ | ||||||
|             //TODO: Errorhandling |             result.errorString = "Das eingegebene alte Passwort stimmt nicht mit dem momentan gespeicherten Passwort überein. Bitte versuchen Sie es erneut."; | ||||||
|         } |         } | ||||||
|  |         return result; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public void updateShippingInfo(String salutation, String name, String address){ |     public UpdateResult updateShippingInfo(String salutation, String name, String address){ | ||||||
|         if(salutation.equals("") || name.equals("") || address.equals("")){ |         this.user.salutation = salutation; | ||||||
|             //TODO: Errorhandling |         this.user.name = name; | ||||||
|         }else{ |         this.user.defaultDeliveryAddress.addressString = address; | ||||||
|             this.user.salutation = salutation; |         this.repository.save(this.user); | ||||||
|             this.user.name = name; |         return new UpdateResult(true); | ||||||
|             this.user.defaultDeliveryAddress.addressString = address; |     } | ||||||
|  |  | ||||||
|  |     public UpdateResult updateAdvertisementFlag(boolean advertisementFlag){ | ||||||
|  |         this.user.isAdvertisementActivated = advertisementFlag; | ||||||
|  |         this.repository.save(this.user); | ||||||
|  |         return new UpdateResult(true); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public UpdateResult updatePaymentInfo(String creditCardNumber){ | ||||||
|  |         UpdateResult result = new UpdateResult(false); | ||||||
|  |         if(creditCardNumber.matches("[0-9]+")){ | ||||||
|  |             this.user.defaultPayment.creditCardNumber = creditCardNumber; | ||||||
|             this.repository.save(this.user); |             this.repository.save(this.user); | ||||||
|  |             result.updated = true; | ||||||
|  |         }else{ | ||||||
|  |             result.errorString = "Kreditkartennummer enthält Buchstaben. Bitte versuchen Sie es erneut."; | ||||||
|         } |         } | ||||||
|     } |         return result; | ||||||
|  |  | ||||||
|     public void updateAdvertisementFlag(boolean advertisementFlag){ |  | ||||||
|         this.user.isAdvertisementActivated = advertisementFlag; //TODO: Errodhandling |  | ||||||
|         this.repository.save(this.user); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     public void updatePaymentInfo(String creditCardNumber){ |  | ||||||
|         //TODO: Errorhandling |  | ||||||
|         this.user.defaultPayment.creditCardNumber = creditCardNumber; |  | ||||||
|         this.repository.save(this.user); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public class UpdateResult{ |     public class UpdateResult{ | ||||||
|  | |||||||
| @ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.PostMapping; | |||||||
| import org.springframework.web.bind.annotation.RequestMapping; | import org.springframework.web.bind.annotation.RequestMapping; | ||||||
| import org.springframework.web.bind.annotation.RequestParam; | import org.springframework.web.bind.annotation.RequestParam; | ||||||
|  |  | ||||||
|  | import javax.servlet.http.HttpServletRequest; | ||||||
| import javax.servlet.http.HttpSession; | import javax.servlet.http.HttpSession; | ||||||
| import java.util.List; | import java.util.List; | ||||||
|  |  | ||||||
| @ -46,9 +47,9 @@ public class UserController { | |||||||
|  |  | ||||||
|     @GetMapping("/orders/") |     @GetMapping("/orders/") | ||||||
|     public String userOrdeers(HttpSession session, |     public String userOrdeers(HttpSession session, | ||||||
|                               Model model) { |                               Model model | ||||||
|  |     ) { | ||||||
|         List<CustomerOrder> orders = customerOrderRepository.getOrdersByUserId((long) session.getAttribute("userId")); |         List<CustomerOrder> orders = customerOrderRepository.getOrdersByUserId((long) session.getAttribute("userId")); | ||||||
|  |  | ||||||
|         model.addAttribute("orders", orders); |         model.addAttribute("orders", orders); | ||||||
|  |  | ||||||
|         return "user/orders/index"; |         return "user/orders/index"; | ||||||
| @ -56,12 +57,16 @@ public class UserController { | |||||||
|  |  | ||||||
|     @PostMapping("/settings/changeMail") |     @PostMapping("/settings/changeMail") | ||||||
|     public String changeMail(HttpSession session, |     public String changeMail(HttpSession session, | ||||||
|                              @RequestParam("email") String email |                              @RequestParam("email") String email, | ||||||
|     ){ |                              HttpServletRequest request | ||||||
|  |     ) { | ||||||
|         User user = userRepository.findById((long) session.getAttribute("userId")).get(); |         User user = userRepository.findById((long) session.getAttribute("userId")).get(); | ||||||
|  |  | ||||||
|         UpdateUserSettingsAction cusa = new UpdateUserSettingsAction(user, userRepository); |         UpdateUserSettingsAction cusa = new UpdateUserSettingsAction(user, userRepository); | ||||||
|         cusa.updateEmail(email); |         UpdateUserSettingsAction.UpdateResult result = cusa.updateEmail(email); | ||||||
|  |         if (result.updated == false) { | ||||||
|  |             request.setAttribute("error", result.errorString); | ||||||
|  |         } | ||||||
|  |  | ||||||
|         return "user/settings"; |         return "user/settings"; | ||||||
|     } |     } | ||||||
| @ -70,12 +75,16 @@ public class UserController { | |||||||
|     public String changePwd(HttpSession session, |     public String changePwd(HttpSession session, | ||||||
|                             @RequestParam("old-password") String oldPassword, |                             @RequestParam("old-password") String oldPassword, | ||||||
|                             @RequestParam("password1") String password1, |                             @RequestParam("password1") String password1, | ||||||
|                             @RequestParam("password2") String password2 |                             @RequestParam("password2") String password2, | ||||||
|     ){ |                             HttpServletRequest request | ||||||
|  |     ) { | ||||||
|         User user = userRepository.findById((long) session.getAttribute("userId")).get(); |         User user = userRepository.findById((long) session.getAttribute("userId")).get(); | ||||||
|  |  | ||||||
|         UpdateUserSettingsAction cusa = new UpdateUserSettingsAction(user, userRepository); |         UpdateUserSettingsAction cusa = new UpdateUserSettingsAction(user, userRepository); | ||||||
|         cusa.updatePassword(oldPassword, password1, password2); |         UpdateUserSettingsAction.UpdateResult result = cusa.updatePassword(oldPassword, password1, password2); | ||||||
|  |         if (result.updated == false) { | ||||||
|  |             request.setAttribute("error", result.errorString); | ||||||
|  |         } | ||||||
|  |  | ||||||
|         return "user/settings"; |         return "user/settings"; | ||||||
|     } |     } | ||||||
| @ -84,36 +93,48 @@ public class UserController { | |||||||
|     public String changeAddress(HttpSession session, |     public String changeAddress(HttpSession session, | ||||||
|                                 @RequestParam("salutation") String salutation, |                                 @RequestParam("salutation") String salutation, | ||||||
|                                 @RequestParam("name") String name, |                                 @RequestParam("name") String name, | ||||||
|                                 @RequestParam("address") String address |                                 @RequestParam("address") String address, | ||||||
|     ){ |                                 HttpServletRequest request | ||||||
|  |     ) { | ||||||
|         User user = userRepository.findById((long) session.getAttribute("userId")).get(); |         User user = userRepository.findById((long) session.getAttribute("userId")).get(); | ||||||
|  |  | ||||||
|         UpdateUserSettingsAction cusa = new UpdateUserSettingsAction(user, userRepository); |         UpdateUserSettingsAction cusa = new UpdateUserSettingsAction(user, userRepository); | ||||||
|         cusa.updateShippingInfo(salutation, name, address); |         UpdateUserSettingsAction.UpdateResult result = cusa.updateShippingInfo(salutation, name, address); | ||||||
|  |         if (result.updated == false) { | ||||||
|  |             request.setAttribute("error", result.errorString); | ||||||
|  |         } | ||||||
|  |  | ||||||
|         return "user/settings"; |         return "user/settings"; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @PostMapping("/settings/changeAdSettings") |     @PostMapping("/settings/changeAdSettings") | ||||||
|     public String changeAdSettings(HttpSession session, |     public String changeAdSettings(HttpSession session, | ||||||
|                                    @RequestParam("ad") String ad |                                    @RequestParam("ad") String ad, | ||||||
|     ){ |                                    HttpServletRequest request | ||||||
|  |     ) { | ||||||
|         User user = userRepository.findById((long) session.getAttribute("userId")).get(); |         User user = userRepository.findById((long) session.getAttribute("userId")).get(); | ||||||
|  |  | ||||||
|         UpdateUserSettingsAction cusa = new UpdateUserSettingsAction(user, userRepository); |         UpdateUserSettingsAction cusa = new UpdateUserSettingsAction(user, userRepository); | ||||||
|         cusa.updateAdvertisementFlag(ad.equals("y")); |         UpdateUserSettingsAction.UpdateResult result = cusa.updateAdvertisementFlag(ad.equals("y")); | ||||||
|  |         if (result.updated == false) { | ||||||
|  |             request.setAttribute("error", result.errorString); | ||||||
|  |         } | ||||||
|  |  | ||||||
|         return "user/settings"; |         return "user/settings"; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @PostMapping("/settings/changePaymentInfo") |     @PostMapping("/settings/changePaymentInfo") | ||||||
|     public String changePaymentInfo(HttpSession session, |     public String changePaymentInfo(HttpSession session, | ||||||
|                                     @RequestParam("creditCardNumber") String creditCardNumber |                                     @RequestParam("creditCardNumber") String creditCardNumber, | ||||||
|     ){ |                                     HttpServletRequest request | ||||||
|  |     ) { | ||||||
|         User user = userRepository.findById((long) session.getAttribute("userId")).get(); |         User user = userRepository.findById((long) session.getAttribute("userId")).get(); | ||||||
|  |  | ||||||
|         UpdateUserSettingsAction cusa = new UpdateUserSettingsAction(user, userRepository); |         UpdateUserSettingsAction cusa = new UpdateUserSettingsAction(user, userRepository); | ||||||
|         cusa.updatePaymentInfo(creditCardNumber); |         UpdateUserSettingsAction.UpdateResult result = cusa.updatePaymentInfo(creditCardNumber); | ||||||
|  |         if (result.updated == false) { | ||||||
|  |             request.setAttribute("error", result.errorString); | ||||||
|  |         } | ||||||
|  |  | ||||||
|         return "user/settings"; |         return "user/settings"; | ||||||
|     } |     } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user