The 'code', 'eMail' fields in the VerificationData class and 'username', 'email', 'question' fields in the ContactFormData class are now marked as 'final'. At the same time, some unused imports from ContactController, ContactFormData, FormQuery, and VerifyController have been removed for code cleanliness.
39 lines
1.8 KiB
Java
39 lines
1.8 KiB
Java
package com.alttd.forms.contact;
|
|
import com.alttd.forms.mail.verification.VerificationResult;
|
|
import com.alttd.forms.mail.verification.Verify;
|
|
import jakarta.validation.Valid;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/contact")
|
|
public class ContactController {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ContactController.class);
|
|
|
|
@PostMapping("/submitContactForm")
|
|
public CompletableFuture<ResponseEntity<String>> submitForm(@Valid @RequestBody ContactFormData formData) {
|
|
logger.debug(formData.toString());
|
|
|
|
CompletableFuture<Integer> storeFormForVerificationCode = new StoreFormQuery().storeFormForVerificationCode(formData.email, formData);
|
|
return storeFormForVerificationCode.thenCompose(code -> Verify.verifyEmail(formData.email, code).thenApply(verificationResult -> {
|
|
if (verificationResult == VerificationResult.VERIFICATION_SENT) {
|
|
//TODO if this is ok tell the user they have x min to verify if they fail to do so they have to remake the form
|
|
return ResponseEntity.ok("User Data received and email verification sent.");
|
|
} else {
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
.body("Failed to send verification email. Reason: " + verificationResult.name());
|
|
}
|
|
})).exceptionally(throwable -> {
|
|
logger.error("Failed to store form", throwable);
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to store your form");
|
|
});
|
|
}
|
|
}
|
|
|