package hirondelle.predict.pub.register;
import static hirondelle.predict.pub.register.RegisterAction.ADD_NEW_USER;
import static hirondelle.predict.pub.register.RegisterAction.ADD_NEW_USER_ROLE;
import hirondelle.web4j.database.DAOException;
import hirondelle.web4j.database.DuplicateException;
import hirondelle.web4j.database.SqlId;
import hirondelle.web4j.database.TxSimple;
import hirondelle.web4j.model.Id;
final class RegisterDAO {
void add(Register aRegister) throws DAOException, DuplicateException {
SqlId[] sqls = new SqlId[]{ADD_NEW_USER, ADD_NEW_USER_ROLE};
String hashedPassword = PasswordHasher.hash(aRegister.getPassword().getRawString());
Object[] params = new Object[]{
aRegister.getLoginName(), aRegister.getScreenName(), Id.from(hashedPassword), aRegister.getEmail(),
aRegister.getLoginName()
};
TxSimple tx = new TxSimple(sqls, params);
int numRecords = tx.executeTx();
if ( numRecords != 2 ){
String msg = "Should have added 2 records (user and single role), but added this many: " + numRecords;
throw new AssertionError(msg);
}
}
}