2020-05-19 15:18:48 +02:00
package org.hso.ecommerce.action.user ;
2020-05-27 14:56:15 +02:00
import org.hso.ecommerce.entities.booking.PaymentMethod ;
2020-05-19 15:18:48 +02:00
import org.hso.ecommerce.entities.user.User ;
import org.hso.ecommerce.repos.user.UserRepository ;
public class UpdateUserSettingsAction {
private User user ;
private UserRepository repository ;
2020-05-25 10:21:36 +02:00
public UpdateUserSettingsAction ( User user , UserRepository repository ) {
2020-05-19 15:18:48 +02:00
this . user = user ;
this . repository = repository ;
}
2020-05-25 10:21:36 +02:00
public UpdateResult updateEmail ( String newMail ) {
2020-05-20 12:08:57 +02:00
UpdateResult result = new UpdateResult ( false ) ;
2020-05-25 10:21:36 +02:00
if ( ! newMail . contains ( " @ " ) ) {
2020-05-20 12:08:57 +02:00
result . errorString = " Ändern der Email-Addresse nicht möglich. Bitte versuchen Sie es erneut. " ;
2020-05-25 10:21:36 +02:00
} else {
2020-05-19 15:18:48 +02:00
this . user . email = newMail ;
2020-05-20 12:08:57 +02:00
this . repository . save ( this . user ) ;
result . updated = true ;
2020-05-19 15:18:48 +02:00
}
2020-05-20 12:08:57 +02:00
return result ;
2020-05-19 15:18:48 +02:00
}
2020-05-25 10:21:36 +02:00
public UpdateResult updatePassword ( String oldPassword , String password1 , String password2 ) {
2020-05-20 12:08:57 +02:00
UpdateResult result = new UpdateResult ( false ) ;
2020-05-25 10:21:36 +02:00
if ( this . user . validatePassword ( oldPassword ) ) {
if ( password1 . equals ( password2 ) ) {
if ( ! password1 . equals ( oldPassword ) ) {
2020-05-25 10:10:42 +02:00
this . user . setPassword ( password1 ) ;
this . repository . save ( this . user ) ;
result . updated = true ;
2020-05-25 10:21:36 +02:00
} else {
2020-06-01 20:24:45 +02:00
result . errorString = " Das neue Passwort entspricht dem alten Passwort. " ;
2020-05-25 10:10:42 +02:00
}
2020-05-25 10:21:36 +02:00
} else {
2020-05-20 12:08:57 +02:00
result . errorString = " Die beiden neuen Passwörter stimmen nicht überein. Bitte versuchen Sie es erneut. " ;
2020-05-19 15:18:48 +02:00
}
2020-05-25 10:21:36 +02:00
} else {
2020-05-20 12:08:57 +02:00
result . errorString = " Das eingegebene alte Passwort stimmt nicht mit dem momentan gespeicherten Passwort überein. Bitte versuchen Sie es erneut. " ;
2020-05-19 15:18:48 +02:00
}
2020-05-20 12:08:57 +02:00
return result ;
2020-05-19 15:18:48 +02:00
}
2020-05-25 10:21:36 +02:00
public UpdateResult updateShippingInfo ( String salutation , String name , String address ) {
2020-05-20 12:08:57 +02:00
this . user . salutation = salutation ;
2020-06-21 00:13:10 +02:00
this . user . defaultDeliveryAddress . name = name ;
2020-05-20 12:08:57 +02:00
this . user . defaultDeliveryAddress . addressString = address ;
this . repository . save ( this . user ) ;
return new UpdateResult ( true ) ;
2020-05-19 15:18:48 +02:00
}
2020-05-25 10:21:36 +02:00
public UpdateResult updatePaymentInfo ( String creditCardNumber ) {
2020-05-20 12:08:57 +02:00
UpdateResult result = new UpdateResult ( false ) ;
2020-05-25 10:21:36 +02:00
if ( creditCardNumber . matches ( " [0-9]+ " ) ) {
2020-05-27 14:56:15 +02:00
this . user . defaultPayment = PaymentMethod . fromCreditCardNumber ( creditCardNumber ) ;
2020-05-20 12:08:57 +02:00
this . repository . save ( this . user ) ;
result . updated = true ;
2020-05-25 10:21:36 +02:00
} else {
2020-05-25 10:10:42 +02:00
result . errorString = " Kreditkartennummer darf nur Zahlen enthalten. Bitte versuchen Sie es erneut. " ;
2020-05-20 12:08:57 +02:00
}
return result ;
2020-05-19 15:18:48 +02:00
}
2020-05-25 10:21:36 +02:00
public class UpdateResult {
2020-05-19 15:18:48 +02:00
public boolean updated ; //if true worked, if false not worked
public String errorString ;
2020-05-25 10:21:36 +02:00
public UpdateResult ( boolean updated , String errorString ) {
2020-05-19 15:18:48 +02:00
this . updated = updated ;
this . errorString = errorString ;
}
2020-05-25 10:21:36 +02:00
public UpdateResult ( boolean updated ) {
2020-05-19 15:18:48 +02:00
this . updated = updated ;
this . errorString = " " ;
}
}
}