Created
March 10, 2017 19:54
-
-
Save vcatalano/c1af2a258a9417b749c52e675ab01c7c to your computer and use it in GitHub Desktop.
Simple registration service using a JDBI Transaction.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.example.exceptions.RegistrationException; | |
import com.example.User; | |
import com.example.UserRole; | |
import com.example.AccountDAO; | |
import com.example.UserDAO; | |
import com.example.UserRoleDAO; | |
import org.skife.jdbi.v2.sqlobject.CreateSqlObject; | |
import org.skife.jdbi.v2.sqlobject.Transaction; | |
/** | |
* Simple registration service using a JDBI Transaction. | |
*/ | |
public abstract class RegistrationExampleService { | |
@CreateSqlObject | |
abstract AccountDAO accountDAO(); | |
@CreateSqlObject | |
abstract UserDAO userDAO(); | |
@CreateSqlObject | |
abstract UserRoleDAO userRoleDAO(); | |
@Transaction | |
public User createUser(String website, String email, String password) throws RegistrationException { | |
User user = userDAO().getByEmail(email); | |
if (user != null) { | |
throw new RegistrationException("Email already exists"); | |
} | |
Account account = new Account(website); | |
user = new User(email, password, account.getId()); | |
account.setOwnerId(user.getId()); | |
accountDAO().insert(account); | |
userDAO().insert(user); | |
userRoleDAO().insert(user.getId(), UserRole.ACCOUNT_ADMIN); | |
return user; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment