Skip to content

Commit

Permalink
✨ feat: add base model response and builder #4
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed May 31, 2024
1 parent 665d209 commit de51dff
Show file tree
Hide file tree
Showing 8 changed files with 380 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.unify4j.common.Collection4j;
import org.unify4j.model.response.HttpResponse;
import org.unify4j.model.response.MetaResponse;
import org.unify4j.model.response.PaginationResponse;
import org.unify4j.model.response.WrapResponse;
import org.unify4j.model.response.*;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

Expand Down Expand Up @@ -123,6 +121,78 @@ public HttpWrapBuilder<T> pagination(PaginationBuilder builder) {
return this;
}

/**
* Sets the path for the wrap builder.
*
* @param path the path value to be set
* @return the HttpWrapBuilder instance for method chaining
*/
public HttpWrapBuilder<T> path(String path) {
this.builder().setPath(path);
return this;
}

/**
* Sets the debug list for the wrap builder.
*
* @param debug the list of debug values to be set
* @return the HttpWrapBuilder instance for method chaining
*/
public HttpWrapBuilder<T> debug(List<BaseOutlineResponse> debug) {
this.builder().setDebug(debug);
return this;
}

/**
* Sets the values list for the wrap builder using varargs.
*
* @param values the varargs array of values to be set
* @return the HttpWrapBuilder instance for method chaining
*/
public HttpWrapBuilder<T> debug(BaseOutlineResponse... values) {
this.builder().appendDebug(values);
return this;
}

/**
* Adds debug information constructed from object, field, rejectedValue, and message.
*
* @param object the object associated with the debug information
* @param field the field associated with the debug information
* @param rejectedValue the rejected value associated with the debug information
* @param message the message associated with the debug information
* @return the HttpWrapBuilder instance for method chaining
*/
public HttpWrapBuilder<T> debug(String object, String field, Object rejectedValue, String message) {
return this.debug(
new VerificationOutlineBuilder()
.setObject(object)
.setField(field)
.setRejectedValue(rejectedValue)
.setMessage(message)
.build());
}

/**
* Adds debug information constructed from object and message.
*
* @param object the object associated with the debug information
* @param message the message associated with the debug information
* @return the HttpWrapBuilder instance for method chaining
*/
public HttpWrapBuilder<T> debug(String object, String message) {
return this.debug(
new VerificationOutlineBuilder()
.setObject(object)
.setMessage(message)
.build());
}

public HttpWrapBuilder<T> debug(VerificationOutlineBuilder builder) {
this.builder().appendDebug(builder);
return this;
}

public HttpWrapBuilder<T> ok(T data) {
builder.setStatusCode(HttpStatusBuilder.OK);
builder.setData(data);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.unify4j.model.builder;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.unify4j.model.response.VerificationResponse;

import java.io.Serializable;

@JsonIgnoreProperties(ignoreUnknown = true)
public class VerificationBuilder implements Serializable {
public VerificationBuilder() {
super();
}

private String object;
private String field;
private Object rejectedValue;
private String message;

public VerificationBuilder setObject(String object) {
this.object = object;
return this;
}

public VerificationBuilder setField(String field) {
this.field = field;
return this;
}

public VerificationBuilder setRejectedValue(Object rejectedValue) {
this.rejectedValue = rejectedValue;
return this;
}

public VerificationBuilder setMessage(String message) {
this.message = message;
return this;
}

public VerificationResponse build() {
VerificationResponse e = new VerificationResponse();
e.setObject(this.object);
e.setField(this.field);
e.setRejectedValue(this.rejectedValue);
e.setMessage(this.message);
return e;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.unify4j.model.builder;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.unify4j.model.response.VerificationOutlineResponse;

import java.io.Serializable;

@JsonIgnoreProperties(ignoreUnknown = true)
public class VerificationOutlineBuilder implements Serializable {
public VerificationOutlineBuilder() {
super();
}

private String object;
private String field;
private Object rejectedValue;
private String message;

public VerificationOutlineBuilder setObject(String object) {
this.object = object;
return this;
}

public VerificationOutlineBuilder setField(String field) {
this.field = field;
return this;
}

public VerificationOutlineBuilder setRejectedValue(Object rejectedValue) {
this.rejectedValue = rejectedValue;
return this;
}

public VerificationOutlineBuilder setMessage(String message) {
this.message = message;
return this;
}

public VerificationOutlineResponse build() {
VerificationOutlineResponse e = new VerificationOutlineResponse();
e.setObject(this.object);
e.setField(this.field);
e.setRejectedValue(this.rejectedValue);
e.setMessage(this.message);
return e;
}
}
37 changes: 33 additions & 4 deletions plugin/src/main/groovy/org/unify4j/model/builder/WrapBuilder.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.unify4j.model.builder;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.unify4j.model.response.HttpResponse;
import org.unify4j.model.response.MetaResponse;
import org.unify4j.model.response.PaginationResponse;
import org.unify4j.model.response.WrapResponse;
import org.unify4j.common.Object4j;
import org.unify4j.model.response.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@SuppressWarnings({"UnusedReturnValue"})
Expand All @@ -24,6 +24,8 @@ public WrapBuilder() {
private HttpResponse headers;
private MetaResponse meta;
private PaginationResponse pagination;
private String path;
private List<BaseOutlineResponse> debug;

public WrapBuilder<T> setStatusCode(int statusCode) {
this.statusCode = statusCode;
Expand Down Expand Up @@ -89,6 +91,31 @@ public WrapBuilder<T> setPagination(PaginationBuilder builder) {
return this;
}

public WrapBuilder<T> setPath(String path) {
this.path = path;
return this;
}

public WrapBuilder<T> setDebug(List<BaseOutlineResponse> debug) {
this.debug = debug;
return this;
}

public WrapBuilder<T> appendDebug(BaseOutlineResponse... values) {
if (Object4j.isEmpty(values)) {
return this;
}
if (this.debug == null) {
this.debug = new ArrayList<>();
}
this.debug.addAll(Arrays.asList(values));
return this;
}

public WrapBuilder<T> appendDebug(VerificationOutlineBuilder builder) {
return this.appendDebug(builder.build());
}

public WrapResponse<T> build() {
WrapResponse<T> e = new WrapResponse<>();
e.setStatusCode(statusCode);
Expand All @@ -99,6 +126,8 @@ public WrapResponse<T> build() {
e.setHeaders(headers);
e.setMeta(meta);
e.setPagination(pagination);
e.setPath(path);
e.setDebug(debug);
return e;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.unify4j.model.response;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public abstract class BaseOutlineResponse {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.unify4j.model.response;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.unify4j.common.Json4j;

@JsonIgnoreProperties(ignoreUnknown = true)
public class VerificationOutlineResponse extends BaseOutlineResponse {
public VerificationOutlineResponse() {
super();
}

public VerificationOutlineResponse(String object, String message) {
super();
this.object = object;
this.message = message;
}

public VerificationOutlineResponse(String object, String field, Object rejectedValue, String message) {
super();
this.object = object;
this.field = field;
this.rejectedValue = rejectedValue;
this.message = message;
}

private String object;
private String field;
private Object rejectedValue;
private String message;

public String getObject() {
return object;
}

public void setObject(String object) {
this.object = object;
}

public String getField() {
return field;
}

public void setField(String field) {
this.field = field;
}

public Object getRejectedValue() {
return rejectedValue;
}

public void setRejectedValue(Object rejectedValue) {
this.rejectedValue = rejectedValue;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public static String getForms(VerificationOutlineResponse verification) {
return String.format("%s: %s", verification.getField(), verification.getMessage());
}

@Override
public String toString() {
return String.format("Verification outline response { object: %s, field: %s, rejected_value: %s, message: %s }",
object, field, Json4j.toJson(rejectedValue), message);
}
}
Loading

0 comments on commit de51dff

Please sign in to comment.