using TEAMModelOS.SDK.Extension.DataResult.PageToken; using System; using System.Collections.Generic; using System.Text; namespace TEAMModelOS.SDK.Extension.DataResult.JsonRpcResponse { public class JsonRPCResponseBuilder { private string message="Success"; private object data; private long total; private int currPage; private int pageSize; private int totalPage; private Dictionary extend; private Pagination page; private AzureTableToken token; private ErrorModel error =null; public JsonRPCResponseBuilder() { } public JsonRPCResponseBuilder Success() { error = null; return this; } public JsonRPCResponseBuilder Success(String message) { this.message = message; return this; } public static JsonRPCResponseBuilder custom() { return new JsonRPCResponseBuilder(); } public JsonRPCResponseBuilder Data(object data) { this.data = data; return this; } public JsonRPCResponseBuilder Error( int code, string message) { this.error = new ErrorModel { code=code, message=message, data = null }; return this; } public JsonRPCResponseBuilder Error(int code, Dictionary errorData) { this.error = new ErrorModel { code = code, message = message ,data= errorData }; return this; } public JsonRPCResponseBuilder Error( int code) { this.error = new ErrorModel { code = code, message = "Error", data = null }; return this; } public JsonRPCResponseBuilder Error(int code, string message, Dictionary errorData) { this.error = new ErrorModel { code = code, message = message, data = errorData }; return this; } public JsonRPCResponseBuilder Extend(Dictionary extend) { this.extend = extend; return this; } public JsonRPCResponseBuilder Token(AzureTableToken token) { this.token = token; return this; } public JsonRPCResponseBuilder Page(Pagination page) { this.pageSize = page.pageSize; this.currPage = page.currPage; this.total = page.total; this.page = page; this.totalPage = (int)Math.Ceiling((double)this.total / (double)this.pageSize); return this; } public JsonRPCResponseBuilder totalCount(int totalCount) { this.total = totalCount; return this; } public JsonRPCResponseBuilder CurrPage(int currPage) { this.currPage = currPage; return this; } public JsonRPCResponseBuilder PageSize(int pageSize) { this.pageSize = pageSize; return this; } public JsonRPCResponseBuilder TotalPage(int totalPage) { this.totalPage = totalPage; return this; } public BaseJosnRPCResponse build() { object baseResponse = null; if (error != null) { ErrorJosnRPCResponse errorJosnRPCResponse = new ErrorJosnRPCResponse(); errorJosnRPCResponse.error = error.data; error.code = error.code; error.message = error.message; return errorJosnRPCResponse; } if (this.total > 0 && this.pageSize > 0) { this.totalPage = (int)Math.Ceiling((double)this.total / (double)this.pageSize); } if (null != this.data && this.token != null) { TokenJosnRPCResponse response = new TokenJosnRPCResponse(); response.result.data = this.data; response.result.extend = this.extend; response.result.azureToken = this.token; response.code = 0; response.message = message; baseResponse = response; } else if (null != this.data && this.total > 0 && this.currPage > 0 && this.pageSize > 0 && this.totalPage > 0) { PageJosnRPCResponse response = new PageJosnRPCResponse(); response.result.data = this.data; response.result.page = new Pagination(this.total, this.currPage, this.pageSize, this.totalPage); response.result.extend = this.extend; response.message = message; response.code = 0; baseResponse = response; } else if (this.data != null) { DataJosnRPCResponse response = new DataJosnRPCResponse(); response.result.data = this.data; response.result.extend = this.extend; response.message = message; response.code = 0; baseResponse = response; } else if (this.data == null) { DataJosnRPCResponse response = new DataJosnRPCResponse(); response.result.data = this.data; response.result.extend = this.extend; response.message = message; response.code = 0; baseResponse = response; } else { return new EmptyJosnRPCResponse() ; } return (BaseJosnRPCResponse)baseResponse; } } }