Skip to content

Commit

Permalink
Bind sign up data instead of building by hand (#6895)
Browse files Browse the repository at this point in the history
#### 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
```
  • Loading branch information
JohnNiang authored Oct 18, 2024
1 parent 697a5e5 commit 3570353
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 34 deletions.
32 changes: 0 additions & 32 deletions api/src/main/java/run/halo/app/core/user/service/SignUpData.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
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;

/**
Expand Down Expand Up @@ -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})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ RouterFunction<ServerResponse> preAuthSignUpEndpoints() {
.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());
Expand Down

0 comments on commit 3570353

Please sign in to comment.