Bind sign up data instead of building by hand (#6895)

#### What type of PR is this?

/kind improvement
/area core
/milestone 2.20.x

#### What this PR does / why we need it:

This PR refactors sign up data binding using internal `bind` method in `ServerRequest` instead of binding my hand. It's more convenient and simpler.

#### Does this PR introduce a user-facing change?

```release-note
None
```
This commit is contained in:
John Niang 2024-10-18 15:55:39 +08:00 committed by GitHub
parent 697a5e5a4c
commit 3570353ce2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 1 additions and 34 deletions

View File

@ -13,10 +13,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Objects;
import java.util.Optional;
import lombok.Data;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import run.halo.app.infra.ValidationUtils;
/**
@ -52,35 +49,6 @@ public class SignUpData {
@NotBlank
private String confirmPassword;
public static SignUpData of(MultiValueMap<String, String> formData) {
var form = new SignUpData();
Optional.ofNullable(formData.getFirst("username"))
.filter(StringUtils::hasText)
.ifPresent(form::setUsername);
Optional.ofNullable(formData.getFirst("displayName"))
.filter(StringUtils::hasText)
.ifPresent(form::setDisplayName);
Optional.ofNullable(formData.getFirst("email"))
.filter(StringUtils::hasText)
.ifPresent(form::setEmail);
Optional.ofNullable(formData.getFirst("password"))
.filter(StringUtils::hasText)
.ifPresent(form::setPassword);
Optional.ofNullable(formData.getFirst("emailCode"))
.filter(StringUtils::hasText)
.ifPresent(form::setEmailCode);
Optional.ofNullable(formData.getFirst("confirmPassword"))
.filter(StringUtils::hasText)
.ifPresent(form::setConfirmPassword);
return form;
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {SignUpDataConstraintValidator.class})

View File

@ -79,8 +79,7 @@ class PreAuthSignUpEndpoint {
.POST(
"",
contentType(APPLICATION_FORM_URLENCODED),
request -> request.formData()
.map(SignUpData::of)
request -> request.bind(SignUpData.class)
.flatMap(signUpData -> {
// sign up
var bindingResult = validate(signUpData, validator, request.exchange());