Skip to content

Commit

Permalink
fix(user): fixed user account deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
zZHorizonZz committed Aug 23, 2024
1 parent 91532b9 commit 8fd4da3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public User handle(CreateUserInput createUserInput) {
checkExistingUsername(user.getUsername());
checkExistingEmail(user.getPrimaryEmailAddress().getEmail());

userAccountManager.createUserBlocking(new BasicUserAccount(user.getUsername(), "test"));
BasicUserAccount basic = userAccountManager.createUserBlocking(new BasicUserAccount(user.getUsername(), "test"));

userRepository.createUser(user);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,22 @@
import dev.cloudeko.zenei.user.runtime.BasicUserAccountRecorder;
import dev.cloudeko.zenei.user.runtime.BasicUserAccountRepository;
import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.BeanContainerBuildItem;
import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
import io.quarkus.arc.deployment.SyntheticBeansRuntimeInitBuildItem;
import io.quarkus.builder.item.SimpleBuildItem;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.Consume;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.runtime.configuration.ConfigurationException;
import jakarta.enterprise.context.Dependent;
import jakarta.inject.Singleton;
import org.jboss.jandex.DotName;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

import static io.quarkus.deployment.Capability.*;
import static io.quarkus.deployment.annotations.ExecutionTime.RUNTIME_INIT;
import static io.quarkus.deployment.annotations.ExecutionTime.STATIC_INIT;

public class BasicUserAccountManagerProcessor {
Expand All @@ -30,15 +29,21 @@ public class BasicUserAccountManagerProcessor {
REACTIVE_MSSQL_CLIENT, REACTIVE_DB2_CLIENT, REACTIVE_ORACLE_CLIENT };

@BuildStep
@Record(STATIC_INIT)
SyntheticBeanBuildItem createBasicUserInitializerConfig(BasicUserAccountRecorder recorder, Capabilities capabilities) {
ReactiveClientBuildItem findReactiveClient(Capabilities capabilities) {
final String reactiveClient = capabilities.getCapabilities().stream()
.filter(c -> Arrays.asList(SUPPORTED_REACTIVE_CLIENTS).contains(c))
.findFirst()
.orElseThrow(() -> new RuntimeException("No supported reactive SQL client found"));

return new ReactiveClientBuildItem(reactiveClient);
}

@BuildStep
@Record(STATIC_INIT)
SyntheticBeanBuildItem createBasicUserInitializerConfig(BasicUserAccountRecorder recorder,
ReactiveClientBuildItem reactiveClient) {
final String createTableDdl;
final boolean supportsIfTableNotExists = switch (reactiveClient) {
final boolean supportsIfTableNotExists = switch (reactiveClient.getReactiveClient()) {
case REACTIVE_PG_CLIENT -> {
createTableDdl = "CREATE TABLE IF NOT EXISTS zenei_user_account ("
+ "id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, "
Expand Down Expand Up @@ -96,40 +101,48 @@ SyntheticBeanBuildItem createBasicUserInitializerConfig(BasicUserAccountRecorder
.done();
}

@BuildStep
AdditionalBeanBuildItem createBasicUserAccountInitializerBean() {
return new AdditionalBeanBuildItem(BasicUserAccountInitializer.class);
}

@BuildStep
@Record(STATIC_INIT)
SyntheticBeanBuildItem createBasicUserAccountRepository(BasicUserAccountRecorder recorder, Capabilities capabilities) {
final String reactiveClient = capabilities.getCapabilities().stream()
.filter(c -> Arrays.asList(SUPPORTED_REACTIVE_CLIENTS).contains(c))
.findFirst()
.orElseThrow(() -> new RuntimeException("No supported reactive SQL client found"));

SyntheticBeanBuildItem createBasicUserAccountRepository(BasicUserAccountRecorder recorder,
ReactiveClientBuildItem reactiveClient) {
final Map<String, String> config = Arrays.stream(DefaultQuery.values())
.collect(Collectors.toMap(defaultQuery -> defaultQuery.getMetadata().queryKey(),
defaultQuery -> defaultQuery.getMetadata().getQuery(reactiveClient)));
defaultQuery -> defaultQuery.getMetadata().getQuery(reactiveClient.getReactiveClient())));

return SyntheticBeanBuildItem
.configure(BasicUserAccountRepository.class)
.supplier(recorder.createBasicUserAccountRepository(config))
.scope(Singleton.class)
.configure(BasicUserAccountRepository.QueryProvider.class)
.supplier(recorder.createBasicUserAccountRepositoryQueryProvider(config))
.scope(Dependent.class)
.unremovable()
.done();
}

@BuildStep
AdditionalBeanBuildItem createBasicUserAccountManagerBean() {
return new AdditionalBeanBuildItem(BasicUserAccountManager.class);
AdditionalBeanBuildItem setupBeans() {
AdditionalBeanBuildItem.Builder builder = AdditionalBeanBuildItem.builder();
builder.setDefaultScope(DotName.createSimple(Singleton.class.getName()));

builder.addBeanClass(BasicUserAccountInitializer.class);
builder.addBeanClass(BasicUserAccountRepository.class);

return builder.build();
}

@BuildStep
@Record(RUNTIME_INIT)
@Consume(SyntheticBeansRuntimeInitBuildItem.class)
void setSqlClientPool(BasicUserAccountRecorder recorder, BeanContainerBuildItem beanContainer) {
recorder.setSqlClientPool(beanContainer.getValue());
AdditionalBeanBuildItem setupManager() {
return new AdditionalBeanBuildItem(BasicUserAccountManager.class);
}

private static final class ReactiveClientBuildItem extends SimpleBuildItem {

private final String reactiveClient;

private ReactiveClientBuildItem(String reactiveClient) {
this.reactiveClient = reactiveClient;
}

public String getReactiveClient() {
return reactiveClient;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,37 @@

public enum DefaultQuery {
USER_ACCOUNT_FIND_BY_IDENTIFIER(QueryMetadata.of(
"SELECT id, username, image, created_at, updated_at FROM user_account WHERE id = ?",
"SELECT id, username, image, created_at, updated_at FROM zenei_user_account WHERE id = %s",
1,
QueryRegistry.USER_ACCOUNT_FIND_BY_IDENTIFIER
)),
USER_ACCOUNT_FIND_BY_USERNAME(QueryMetadata.of(
"SELECT id, username, image, created_at, updated_at FROM user_account WHERE username = ?",
"SELECT id, username, image, created_at, updated_at FROM zenei_user_account WHERE username = %s",
1,
QueryRegistry.USER_ACCOUNT_FIND_BY_USERNAME
)),
USER_ACCOUNT_LIST(QueryMetadata.of(
"SELECT id, username, image, created_at, updated_at FROM user_account",
"SELECT id, username, image, created_at, updated_at FROM zenei_user_account",
0,
QueryRegistry.USER_ACCOUNT_LIST
)),
USER_ACCOUNT_LIST_PAGINATED(QueryMetadata.of(
"SELECT id, username, image, created_at, updated_at FROM user_account LIMIT ? OFFSET ?",
"SELECT id, username, image, created_at, updated_at FROM zenei_user_account LIMIT %s OFFSET %s",
2,
QueryRegistry.USER_ACCOUNT_LIST_PAGINATED
)),
USER_ACCOUNT_CREATE(QueryMetadata.of(
"INSERT INTO user_account (username, image, created_at, updated_at) VALUES (?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) RETURNING id, username, image, created_at, updated_at",
"INSERT INTO zenei_user_account (username, image, created_at, updated_at) VALUES (%s, %s, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) RETURNING id, username, image, created_at, updated_at",
2,
QueryRegistry.USER_ACCOUNT_CREATE
)),
USER_ACCOUNT_UPDATE(QueryMetadata.of(
"UPDATE user_account SET username = ?, image = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ? RETURNING id, username, image, created_at, updated_at",
"UPDATE zenei_user_account SET username = %s, image = %s, updated_at = CURRENT_TIMESTAMP WHERE id = %s RETURNING id, username, image, created_at, updated_at",
3,
QueryRegistry.USER_ACCOUNT_UPDATE
)),
USER_ACCOUNT_DELETE(QueryMetadata.of(
"DELETE FROM user_account WHERE id = ? RETURNING id, username, image, created_at, updated_at",
"DELETE FROM zenei_user_account WHERE id = %s RETURNING id, username, image, created_at, updated_at",
1,
QueryRegistry.USER_ACCOUNT_DELETE
));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
package dev.cloudeko.zenei.user.runtime;

import io.quarkus.arc.runtime.BeanContainer;
import io.quarkus.runtime.annotations.Recorder;
import io.vertx.sqlclient.Pool;

import java.util.Map;
import java.util.function.Supplier;

@Recorder
public class BasicUserAccountRecorder {

public void setSqlClientPool(BeanContainer container) {
container.beanInstance(BasicUserAccountRepository.class).setSqlClientPool(container.beanInstance(Pool.class));
}

public Supplier<BasicUserAccountRepository> createBasicUserAccountRepository(Map<String, String> config) {
return () -> new BasicUserAccountRepository(config);
public Supplier<BasicUserAccountRepository.QueryProvider> createBasicUserAccountRepositoryQueryProvider(Map<String, String> config) {
return () -> new BasicUserAccountRepository.QueryProvider(config);
}

public Supplier<BasicUserAccountInitializer.UserAccountInitializerProperties> createUserAccountInitializerProps(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@ public class BasicUserAccountRepository implements UserAccountRepositoryBase<Bas
private static final String FAILED_TO_FIND_USER_BY_IDENTIFIER = "Failed to find user by identifier";

private final Map<String, String> queries;
private Pool pool;
private final Pool pool;

public BasicUserAccountRepository(Map<String, String> queries) {
this.queries = queries;
}

public void setSqlClientPool(Pool pool) {
public BasicUserAccountRepository(QueryProvider queryProvider, Pool pool) {
this.queries = queryProvider.queries();
this.pool = pool;
}

Expand Down Expand Up @@ -117,6 +114,9 @@ public Uni<Boolean> deleteUser(BasicUserAccount basicUserAccount) {
.onFailure().transform(throwable -> new RuntimeException("Failed to delete user", throwable));
}

public record QueryProvider(Map<String, String> queries) {
}

private Uni<Row> processNullableRow(RowSet<Row> rows) {
if (rows.size() == 0) {
return Uni.createFrom().nullItem();
Expand Down

0 comments on commit 8fd4da3

Please sign in to comment.