Skip to content
Snippets Groups Projects
Commit 21528cd0 authored by Zekun Fan's avatar Zekun Fan
Browse files

add some common utils

parent f6503856
No related branches found
No related tags found
1 merge request!1Master
package com.example.freshonline.constants;
public class Constants {
public static final String CODE = "code";
public static final int SUCCESS_CODE = 0;
public static final int FAIL_CODE = -1;
public static final String MSG = "msg";
public static final String DATA = "data";
public static final String ADD_GOODS_SUCCESS = "add goods success";
public static final String UPDATE_GOODS_SUCCESS = "update goods success";
public static final String DELETE_GOODS_SUCCESS = "delete goods success";
public static final String ADD_GOODS_FAIL = "add goods fail";
public static final String UPDATE_GOODS_FAIL = "update goods fail";
public static final String DELETE_GOODS_FAIL = "delete goods fail";
}
package com.example.freshonline.enums.respVerifyRule;
/**
* @author zekun
*/
public interface VerifyRespData {
boolean verify(Object data);
}
package com.example.freshonline.enums.respVerifyRule;
import lombok.Data;
import java.util.Collection;
/**
* rules that verify response data
* @author zekun
*/
public enum VerifyRule implements VerifyRespData {
NOT_NULL{
@Override
public boolean verify(Object data) {
return data != null;
}
},
COLLECTION_NOT_EMPTY{
@Override
public boolean verify(Object data) {
Collection<?> collection = (Collection<?>) data;
return collection != null && !collection.isEmpty();
}
},
GREATER_THAN_ZERO{
@Override
public boolean verify(Object data) {
return (Integer)data > 0;
}
},
TRUE{
@Override
public boolean verify(Object data) {
return (Boolean)data;
}
}
;
}
package com.example.freshonline.utils;
import com.alibaba.fastjson.JSONObject;
import com.example.freshonline.constants.Constants;
import com.example.freshonline.enums.respVerifyRule.VerifyRule;
/**
* response builder
* @author zekun
*/
public class RespBuilder {
public static JSONObject create(Object data, VerifyRule rule, String success_msg, String fail_msg) {
JSONObject resp = new JSONObject();
if (rule.verify(data)){
resp.put(Constants.CODE, Constants.SUCCESS_CODE);
resp.put(Constants.MSG, success_msg);
resp.put(Constants.DATA, data);
}else{
resp.put(Constants.CODE, Constants.FAIL_CODE);
resp.put(Constants.MSG, fail_msg);
}
return resp;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment