implement change User Settings

This commit is contained in:
Hannes Huber 2020-05-19 15:18:48 +02:00
parent adcd840154
commit 0256c19e8f
4 changed files with 119 additions and 25 deletions

View File

@ -1,7 +0,0 @@
package org.hso.ecommerce.action.user;
import org.hso.ecommerce.entities.user.User;
public class ChangeUserAction {
}

View File

@ -0,0 +1,77 @@
package org.hso.ecommerce.action.user;
import com.sun.xml.bind.v2.TODO;
import org.hso.ecommerce.entities.user.User;
import org.hso.ecommerce.repos.user.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
public class UpdateUserSettingsAction {
private User user;
private UserRepository repository;
public UpdateUserSettingsAction(User user, UserRepository repository){
this.user = user;
this.repository = repository;
}
public void updateEmail(String newMail){
if(newMail.equals("")){
//TODO: Errorhandling
}else{
this.user.email = newMail;
this.repository.save(this.user); //TODO: Errorhandling
}
}
public void updatePassword(String oldPassword, String password1, String password2){
if(this.user.validatePassword(oldPassword))
{
if(password1.equals(password2)){
this.user.setPassword(password1);
this.repository.save(this.user);
}else{
//TODO Errorhandling
}
}else{
//TODO: Errorhandling
}
}
public void updateShippingInfo(String salutation, String name, String address){
if(salutation.equals("") || name.equals("") || address.equals("")){
//TODO: Errorhandling
}else{
this.user.salutation = salutation;
this.user.name = name;
this.user.defaultDeliveryAddress.addressString = address;
this.repository.save(this.user);
}
}
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 boolean updated; //if true worked, if false not worked
public String errorString;
public UpdateResult(boolean updated, String errorString){
this.updated = updated;
this.errorString = errorString;
}
public UpdateResult(boolean updated){
this.updated = updated;
this.errorString = "";
}
}
}

View File

@ -1,5 +1,6 @@
package org.hso.ecommerce.controller; package org.hso.ecommerce.controller;
import org.hso.ecommerce.action.user.UpdateUserSettingsAction;
import org.hso.ecommerce.entities.shop.CustomerOrder; import org.hso.ecommerce.entities.shop.CustomerOrder;
import org.hso.ecommerce.entities.user.User; import org.hso.ecommerce.entities.user.User;
import org.hso.ecommerce.repos.shop.CustomerOrderRepository; import org.hso.ecommerce.repos.shop.CustomerOrderRepository;
@ -46,7 +47,6 @@ 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);
@ -55,41 +55,65 @@ public class UserController {
} }
@PostMapping("/settings/changeMail") @PostMapping("/settings/changeMail")
public String changeMail(@RequestParam("email") String email){ public String changeMail(HttpSession session,
@RequestParam("email") String email
){
User user = userRepository.findById((long) session.getAttribute("userId")).get();
//TODO: implement this UpdateUserSettingsAction cusa = new UpdateUserSettingsAction(user, userRepository);
cusa.updateEmail(email);
return "user/settings"; return "user/settings";
} }
@PostMapping("/settings/changePwd") @PostMapping("/settings/changePwd")
public String changePwd(){ public String changePwd(HttpSession session,
@RequestParam("old-password") String oldPassword,
@RequestParam("password1") String password1,
@RequestParam("password2") String password2
){
User user = userRepository.findById((long) session.getAttribute("userId")).get();
//TODO: implement this UpdateUserSettingsAction cusa = new UpdateUserSettingsAction(user, userRepository);
cusa.updatePassword(oldPassword, password1, password2);
return "user/settings"; return "user/settings";
} }
@PostMapping("/settings/changeAddress") @PostMapping("/settings/changeAddress")
public String changeAddress(){ public String changeAddress(HttpSession session,
@RequestParam("salutation") String salutation,
@RequestParam("name") String name,
@RequestParam("address") String address
){
User user = userRepository.findById((long) session.getAttribute("userId")).get();
//TODO: implement this UpdateUserSettingsAction cusa = new UpdateUserSettingsAction(user, userRepository);
cusa.updateShippingInfo(salutation, name, address);
return "user/settings"; return "user/settings";
} }
@PostMapping("/settings/changeAdSettings") @PostMapping("/settings/changeAdSettings")
public String changeAdSettings(){ public String changeAdSettings(HttpSession session,
@RequestParam("ad") String ad
){
User user = userRepository.findById((long) session.getAttribute("userId")).get();
//TODO: implement this UpdateUserSettingsAction cusa = new UpdateUserSettingsAction(user, userRepository);
cusa.updateAdvertisementFlag(ad.equals("y"));
return "user/settings"; return "user/settings";
} }
@PostMapping("/settings/changePaymentInfo") @PostMapping("/settings/changePaymentInfo")
public String changePaymentInfo(){ public String changePaymentInfo(HttpSession session,
@RequestParam("creditCardNumber") String creditCardNumber
){
User user = userRepository.findById((long) session.getAttribute("userId")).get();
//TODO: implement this UpdateUserSettingsAction cusa = new UpdateUserSettingsAction(user, userRepository);
cusa.updatePaymentInfo(creditCardNumber);
return "user/settings"; return "user/settings";
} }

View File

@ -33,7 +33,7 @@
</div> </div>
</form> </form>
<form class="detailflex"> <form class="detailflex" method="POST" th:action="@{/user/settings/changePwd}">
<div> <div>
<h2> Sicherheit </h2> <h2> Sicherheit </h2>
</div> </div>
@ -42,8 +42,8 @@
<input class="full-width" type="password" name="old-password" placeholder="Passwort" id="password" <input class="full-width" type="password" name="old-password" placeholder="Passwort" id="password"
required> required>
<label for="password">Neues Passwort</label> <label for="password1">Neues Passwort</label>
<input class="full-width" type="password" name="password" placeholder="Passwort" id="password" required> <input class="full-width" type="password" name="password1" placeholder="Passwort" id="password1" required>
<label for="password2">Neues Passwort wiederholen</label> <label for="password2">Neues Passwort wiederholen</label>
<input class="full-width" type="password" name="password2" placeholder="Passwort" id="password2" <input class="full-width" type="password" name="password2" placeholder="Passwort" id="password2"
required> required>
@ -51,7 +51,7 @@
</div> </div>
</form> </form>
<form class="detailflex"> <form class="detailflex" method="POST" th:action="@{/user/settings/changeAddress}">
<div> <div>
<h2> Rechungs- und Lieferinformation </h2> <h2> Rechungs- und Lieferinformation </h2>
</div> </div>
@ -91,7 +91,7 @@
</div> </div>
</form> </form>
<form class="detailflex"> <form class="detailflex" method="POST" th:action="@{/user/settings/changeAdSettings}">
<div> <div>
<h2> Werbung </h2> <h2> Werbung </h2>
</div> </div>
@ -106,13 +106,13 @@
</div> </div>
</form> </form>
<form class="detailflex"> <form class="detailflex" method="POST" th:action="@{/user/settings/changePaymentInfo}">
<div> <div>
<h2> Zahlungsinformation</h2> <h2> Zahlungsinformation</h2>
</div> </div>
<div> <div>
<div class="input-icon"> <div class="input-icon">
<input class="full-width" type="text" name="payment-card" th:value="${user.defaultPayment.creditCardNumber}" required/> <input class="full-width" type="text" name="creditCardNumber" th:value="${user.defaultPayment.creditCardNumber}" required/>
<button> Kreditkartennummer ändern</button> <button> Kreditkartennummer ändern</button>
</div> </div>
</div> </div>