80 lines
2.3 KiB
Java
Executable File
80 lines
2.3 KiB
Java
Executable File
package zw.qantra.tm.rest;
|
|
|
|
import lombok.Getter;
|
|
import lombok.Setter;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
@Setter
|
|
@Getter
|
|
public class ApiResponse {
|
|
|
|
private String message;
|
|
private HashMap<Object, Object> response;
|
|
private Boolean success;
|
|
private HashMap<Object, Object> body;
|
|
|
|
private Map<String, ?> errors = new HashMap<>();
|
|
|
|
// private Object body;
|
|
|
|
private HttpStatus status = HttpStatus.OK;
|
|
|
|
public ApiResponse(String message) {
|
|
super();
|
|
this.message = message;
|
|
}
|
|
|
|
|
|
public ResponseEntity ApiResponse(Object obj, HttpStatus status) {
|
|
return new ResponseEntity<Object>(obj, status);
|
|
}
|
|
|
|
public static ResponseEntity ok(Object obj){
|
|
return ApiResponse.build(HttpStatus.OK, "SUCCESSFUL", obj, null, true);
|
|
}
|
|
|
|
public static ResponseEntity created(Object obj){
|
|
return ApiResponse.build(HttpStatus.CREATED, "CREATED", obj, null, true);
|
|
}
|
|
|
|
public static ResponseEntity badRequest(Object obj){
|
|
return ApiResponse.build(HttpStatus.BAD_REQUEST, "BAD REQUEST", null, obj, false);
|
|
}
|
|
|
|
public static ResponseEntity notFound(){
|
|
return ApiResponse.build(HttpStatus.NOT_FOUND, "NOT FOUND", null, null, false);
|
|
}
|
|
|
|
public static ResponseEntity notContent(){
|
|
return ApiResponse.build(HttpStatus.NO_CONTENT, "DELETED", null, null, false);
|
|
}
|
|
|
|
public static ResponseEntity badRequest(Map<String, Object> errors){
|
|
return ApiResponse.build(HttpStatus.BAD_REQUEST, "Problem with request", null, errors, false);
|
|
}
|
|
|
|
public static ResponseEntity build(HttpStatus status, String message, Object body, Object errors, Boolean success){
|
|
Map<String, Object> map = new HashMap<>();
|
|
map.put("message", message);
|
|
map.put("body", body);
|
|
map.put("errors", errors);
|
|
map.put("success", success);
|
|
map.put("status", status.value());
|
|
|
|
return new ResponseEntity<Object>(map, status);
|
|
}
|
|
|
|
public ResponseEntity build(){
|
|
Map<String, Object> map = new HashMap<>();
|
|
map.put("message", this.message);
|
|
map.put("body", this.body);
|
|
map.put("errors", this.errors);
|
|
// map.put("status", this.errors);
|
|
|
|
return new ResponseEntity<Object>(map, this.status);
|
|
}
|
|
} |