Skip to content

Commit

Permalink
adding insertApplication
Browse files Browse the repository at this point in the history
  • Loading branch information
jmilkiewicz committed Nov 20, 2023
1 parent 22db837 commit 0d2a583
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.exacaster.lighter.application.ApplicationType;
import com.exacaster.lighter.application.SubmitParams;
import com.exacaster.lighter.application.sessions.exceptions.InvalidSessionStateException;
import com.exacaster.lighter.application.sessions.exceptions.SessionAlreadyExistsException;
import com.exacaster.lighter.application.sessions.processors.StatementHandler;
import com.exacaster.lighter.backend.Backend;
import com.exacaster.lighter.rest.SessionParams;
Expand Down Expand Up @@ -54,10 +53,6 @@ public Application createSession(SessionParams sessionParams) {
}

public Application createSession(String sessionId, SessionParams sessionParams) {
//TODO do we wanna do it for all session or perm only
if (applicationStorage.findApplication(sessionId).isPresent()) {
throw new SessionAlreadyExistsException(sessionId);
}
if (Boolean.TRUE.equals(sessionParams.getPermanent())) {
return createPermanentSession(sessionId, sessionParams);
}
Expand Down Expand Up @@ -86,7 +81,7 @@ private Application createSession(SubmitParams params, String sessionId, Applica
.setCreatedAt(now)
.setContactedAt(now)
.build();
return applicationStorage.saveApplication(entity);
return applicationStorage.insertApplication(entity);
}

protected List<Application> fetchRunningSession() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public interface ApplicationStorage {

Application saveApplication(Application application);

Application insertApplication(Application application);

List<Application> findApplicationsByStates(ApplicationType type, List<ApplicationState> states, SortOrder order,
Integer from, Integer size);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import io.micronaut.context.annotation.Requires;
import jakarta.inject.Singleton;
import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;
Expand Down Expand Up @@ -78,45 +79,63 @@ public void deleteApplication(String internalApplicationId) {
@Transactional
public Application saveApplication(Application application) {
return jdbi.withHandle(handle -> {
var updated = handle.createUpdate("UPDATE application SET "
+ "app_id=:app_id, "
+ "app_info=:app_info, "
+ "state=:state, "
+ "contacted_at=:contacted_at WHERE id=:id")
.bind("state", application.getState().name())
.bind("app_id", application.getAppId())
.bind("app_info", application.getAppInfo())
.bind("contacted_at", application.getContactedAt())
.bind("id", application.getId())
.execute();
final var updated = update(handle, application);
// Not all SQL databases support ON CONFLICT syntax, so doing fallback if nothing updated
if (updated == 0) {
String conf = null;
try {
conf = objectMapper.writeValueAsString(application.getSubmitParams());
} catch (JsonProcessingException e) {
LOG.warn("Failed serializing submit params", e);
}
handle
.createCall(
"INSERT INTO application (id, type, state, app_id, app_info, submit_params, created_at, contacted_at, deleted) "
+ "VALUES (:id, :type, :state, :app_id, :app_info, :submit_params, :created_at, :contacted_at, :deleted)")
.bind("id", application.getId())
.bind("type", application.getType().name())
.bind("state", application.getState().name())
.bind("app_id", application.getAppId())
.bind("app_info", application.getAppInfo())
.bind("submit_params", conf)
.bind("created_at", application.getCreatedAt())
.bind("contacted_at", application.getContactedAt())
.bind("deleted", false)
.invoke();
insert(handle, application);
}
return application;
}
);
}

@Override
@Transactional
public Application insertApplication(Application application) {
return jdbi.withHandle(handle -> {
insert(handle, application);
return application;
});
}

private static int update(Handle handle, Application application) {
var updated = handle.createUpdate("UPDATE application SET "
+ "app_id=:app_id, "
+ "app_info=:app_info, "
+ "state=:state, "
+ "contacted_at=:contacted_at WHERE id=:id")
.bind("state", application.getState().name())
.bind("app_id", application.getAppId())
.bind("app_info", application.getAppInfo())
.bind("contacted_at", application.getContactedAt())
.bind("id", application.getId())
.execute();
return updated;
}

private void insert(Handle handle, Application application) {
String conf = null;
try {
conf = objectMapper.writeValueAsString(application.getSubmitParams());
} catch (JsonProcessingException e) {
LOG.warn("Failed serializing submit params", e);
}
handle
.createCall(
"INSERT INTO application (id, type, state, app_id, app_info, submit_params, created_at, contacted_at, deleted) "
+ "VALUES (:id, :type, :state, :app_id, :app_info, :submit_params, :created_at, :contacted_at, :deleted)")
.bind("id", application.getId())
.bind("type", application.getType().name())
.bind("state", application.getState().name())
.bind("app_id", application.getAppId())
.bind("app_info", application.getAppInfo())
.bind("submit_params", conf)
.bind("created_at", application.getCreatedAt())
.bind("contacted_at", application.getContactedAt())
.bind("deleted", false)
.invoke();
}

@Override
@Transactional
public List<Application> findApplicationsByStates(ApplicationType type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class InMemoryStorage implements ApplicationStorage, LogStorage {
return storeEntity(application)
}

@Override
Application insertApplication(Application application) {
return storeEntity(application)
}

@Override
List<Application> findApplicationsByStates(ApplicationType type, List<ApplicationState> states, SortOrder order, Integer offset, Integer limit) {
return findMany({ type == it.getType() && states.contains(it.getState()) }, Application.class)
Expand Down

0 comments on commit 0d2a583

Please sign in to comment.