Update Form HTML output and disable validation auto-configuration
The form-to-HTML output process has been switched from a `<div>` approach to using a `StringBuilder` with a table structure in `ContactFormData.java`. Also, the spring validation auto-configuration has been disabled by adding `exclude = ValidationAutoConfiguration.class` in the `@SpringBootApplication` annotation of `Main.java`. Some changes in `.idea/workspace.xml` and `VerifyController.java` were made as well.
This commit is contained in:
@@ -3,10 +3,11 @@ package com.alttd.forms;
|
||||
import com.alttd.forms.database.DatabaseConnection;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
@SpringBootApplication
|
||||
@SpringBootApplication(exclude = ValidationAutoConfiguration.class)
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) throws SQLException {
|
||||
|
||||
@@ -7,7 +7,7 @@ import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
public class WebConfig implements WebMvcConfigurer { //TODO this can be done with annotations
|
||||
|
||||
@Bean
|
||||
public WebMvcConfigurer corsConfigurer() {
|
||||
|
||||
@@ -38,19 +38,26 @@ public class ContactFormData extends Form {
|
||||
|
||||
@Override
|
||||
public String toHtml() {
|
||||
return "<div style='margin: 10px; padding: 10px; border: 1px solid #000; width: 300px;'>" +
|
||||
"<p><strong>Username:</strong><br>" +
|
||||
"<span style='color: #007BFF;'>" +
|
||||
username +
|
||||
"</span></p>" +
|
||||
"<p><strong>Email:</strong><br>" +
|
||||
"<span style='color: #007BFF;'>" +
|
||||
email +
|
||||
"</span></p>" +
|
||||
"<p><strong>Question:</strong><br>" +
|
||||
"<span style='color: #007BFF;'>" +
|
||||
question +
|
||||
"</span></p>" +
|
||||
"</div>";
|
||||
StringBuilder htmlOutput = new StringBuilder();
|
||||
|
||||
htmlOutput.append("<table style='border-collapse: collapse; width: 100%;'>");
|
||||
|
||||
String[] fields = {"Username", "Email", "Question"};
|
||||
String[] values = {username, email, question};
|
||||
|
||||
for (int i = 0; i < fields.length; i++) {
|
||||
htmlOutput.append("<tr style='border: 1px solid #ddd;'>");
|
||||
htmlOutput.append("<td style='border: 1px solid #ddd; padding: 10px; font-weight: bold;'>");
|
||||
htmlOutput.append(fields[i]);
|
||||
htmlOutput.append("</td>");
|
||||
htmlOutput.append("<td style='border: 1px solid #ddd; padding: 10px;'>");
|
||||
htmlOutput.append(values[i]);
|
||||
htmlOutput.append("</td>");
|
||||
htmlOutput.append("</tr>");
|
||||
}
|
||||
|
||||
htmlOutput.append("</table>");
|
||||
|
||||
return htmlOutput.toString();
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import java.sql.SQLException;
|
||||
|
||||
public class Database {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(Database.class);
|
||||
private static final Logger logger = LoggerFactory.getLogger(Database.class); //TODO can be replaced with library and that one db file that creates the db
|
||||
|
||||
public static void createTables() {
|
||||
String[] createTables = {
|
||||
|
||||
@@ -37,7 +37,7 @@ public class FormQuery {
|
||||
}
|
||||
|
||||
private Optional<Form> getFormForId(Connection connection, int formId) throws SQLException {
|
||||
String sql = "SELECT form_json FROM form WHERE formId = ?";
|
||||
String sql = "SELECT form_json, form_class FROM form WHERE formId = ?";
|
||||
|
||||
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
|
||||
stmt.setInt(1, formId);
|
||||
|
||||
@@ -10,6 +10,13 @@ import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@RestController
|
||||
@@ -23,6 +30,27 @@ public class VerifyController {
|
||||
logger.debug(verificationData.toString());
|
||||
return new FormQuery().getFormForCode(verificationData.code, verificationData.eMail).thenApply(result -> result.form()
|
||||
.map(form -> {
|
||||
try {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("http://51.222.255.92:8001/api/contact/submitContactForm"))//TODO get uri from form
|
||||
.header("Content-Type", "application/json;charset=UTF-8")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(form.toJsonString(), StandardCharsets.UTF_8))
|
||||
.build();
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
if (response.statusCode() < 200 || response.statusCode() > 200) {
|
||||
logger.error(String.format("Failed to send form to Discord. Got status code [%d], with body\n%s", response.statusCode(), response.body()));
|
||||
//TODO handle failure
|
||||
//TODO strings to config
|
||||
return ResponseEntity.ok("Failed to send form to Discord, please contact us at [email protected]");
|
||||
}
|
||||
} catch (URISyntaxException e) {
|
||||
logger.error("Unable to create URI for posting form", e); //TODO more clear
|
||||
return ResponseEntity.ok("Unable to create URI for posting form, please contact us at [email protected]");
|
||||
} catch (IOException | InterruptedException e) {
|
||||
logger.error("Unable to send form to Discord", e); //TODO more clear
|
||||
return ResponseEntity.ok("Unable to send form to Discord, please contact us at [email protected]");
|
||||
}
|
||||
MailForm.sendForm("[email protected]", form);
|
||||
return ResponseEntity.ok(form.toJsonString());
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user