Refactor code to use Form objects instead of JSON strings
Several parts of the code have been altered to use Form objects instead of JSON strings. Changes include updating the FormQueryResult record type to hold an Optional<Form> instead of an Optional<String>, altering methods in the StoreFormQuery class to insert Form data into the database and replacing JSON handling methods in the FormQuery class with Form object oriented methods. A 'form_class' field has also been added to the 'form' table in the database to aid form identification and reconstruction from stored data.
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package com.alttd.forms.verify_mail;
|
||||
|
||||
import com.alttd.forms.contact.ContactFormData;
|
||||
import com.alttd.forms.contact.StoreFormQuery;
|
||||
import com.alttd.forms.database.DatabaseConnection;
|
||||
import com.alttd.forms.form.Form;
|
||||
import com.google.gson.Gson;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -15,7 +19,7 @@ public class FormQuery {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(FormQuery.class);
|
||||
|
||||
public static Optional<Integer> getFormId(Connection connection, int verificationCode, String eMail) throws SQLException {
|
||||
public Optional<Integer> getFormId(Connection connection, int verificationCode, String eMail) throws SQLException {
|
||||
String sql = "SELECT formId FROM verify_form WHERE verification_code = ? AND e_mail = ?";
|
||||
|
||||
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
|
||||
@@ -33,7 +37,7 @@ public class FormQuery {
|
||||
}
|
||||
}
|
||||
|
||||
private static Optional<String> getFormForId(Connection connection, int formId) throws SQLException {
|
||||
private Optional<Form> getFormForId(Connection connection, int formId) throws SQLException {
|
||||
String sql = "SELECT form_json FROM form WHERE formId = ?";
|
||||
|
||||
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
|
||||
@@ -43,14 +47,30 @@ public class FormQuery {
|
||||
logger.warn("Could not find form with id: " + formId);
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(resultSet.getString("form_json"));
|
||||
String json = resultSet.getString("form_json");
|
||||
String formClass = resultSet.getString("form_class");
|
||||
try {
|
||||
return Optional.of(getForm(formClass, json));
|
||||
} catch (IllegalArgumentException e) {
|
||||
logger.error("Invalid form class in database", e);
|
||||
return Optional.empty();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
logger.error("Failed select form query for form with id: " + formId, e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public static CompletableFuture<FormQueryResult> getFormForCode(String verificationCode, String eMail) {
|
||||
private Form getForm(String className, String json) throws IllegalArgumentException {
|
||||
switch (className) {
|
||||
case "ContactFormData" -> {
|
||||
return new Gson().fromJson(json, ContactFormData.class);
|
||||
}
|
||||
default -> throw new IllegalArgumentException("Invalid form class name: " + className);
|
||||
}
|
||||
}
|
||||
|
||||
public CompletableFuture<FormQueryResult> getFormForCode(String verificationCode, String eMail) {
|
||||
Connection connection = DatabaseConnection.getConnection();
|
||||
int code;
|
||||
try {
|
||||
@@ -73,7 +93,7 @@ public class FormQuery {
|
||||
|
||||
try {
|
||||
return getFormForId(connection, formId.get())
|
||||
.map(formJson -> new FormQueryResult(Optional.of(formJson), "Success"))
|
||||
.map(form -> new FormQueryResult(Optional.of(form), "Success"))
|
||||
.orElse(new FormQueryResult(Optional.empty(), "Unable to find your form"));
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.alttd.forms.verify_mail;
|
||||
|
||||
import com.alttd.forms.form.Form;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public record FormQueryResult(Optional<String> formJson, String failReason) {
|
||||
public record FormQueryResult(Optional<Form> form, String failReason) {
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.alttd.forms.verify_mail;
|
||||
|
||||
import com.alttd.forms.form.Form;
|
||||
import com.alttd.forms.mail.mail_forms.MailForm;
|
||||
import jakarta.validation.Valid;
|
||||
import org.slf4j.Logger;
|
||||
@@ -21,12 +22,12 @@ public class VerifyController {
|
||||
@PostMapping("/form")
|
||||
public CompletableFuture<ResponseEntity<String>> validateEmailFromForm(@Valid @RequestBody VerificationData verificationData) {
|
||||
logger.debug(verificationData.toString());
|
||||
return FormQuery.getFormForCode(verificationData.code, verificationData.eMail).thenApply(form -> form.formJson()
|
||||
.map(body -> {
|
||||
MailForm.sendForm("[email protected]", body);
|
||||
return ResponseEntity.ok(body);
|
||||
return new FormQuery().getFormForCode(verificationData.code, verificationData.eMail).thenApply(result -> result.form()
|
||||
.map(form -> {
|
||||
MailForm.sendForm("[email protected]", form);
|
||||
return ResponseEntity.ok(form.toJsonString());
|
||||
})
|
||||
.orElse(ResponseEntity.ok(form.failReason()))
|
||||
.orElse(ResponseEntity.ok(result.failReason()))
|
||||
).exceptionally(throwable -> ResponseEntity.internalServerError()
|
||||
.body("The server was unable to process your request, if this issue persists please contact [email protected]"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user