/// <summary> /// Gets all entities limited by MaxResultCount sets in request options. /// </summary> /// <param name="client">SugarRestClient object.</param> /// <param name="request">The request object.</param> /// <param name="modelInfo">The entity model info.</param> /// <returns>SugarRestResponse object.</returns> public static SugarRestResponse ExecuteLinkedGetAll(this SugarRestClient client, SugarRestRequest request, ModelInfo modelInfo) { var sugarRestResponse = new SugarRestResponse(); var loginResponse = new LoginResponse(); try { var loginRequest = new LoginRequest { Url = request.Url, Username = request.Username, Password = request.Password }; loginResponse = Authentication.Login(loginRequest); var selectFields = request.Options == null ? new List <string>() : request.Options.SelectFields; var linkedFields = request.Options == null ? null : request.Options.LinkedModules; selectFields = modelInfo.GetJsonPropertyNames(selectFields); Dictionary <string, List <string> > linkedSelectFields = modelInfo.GetJsonLinkedInfo(linkedFields); string query = modelInfo.GetQuery(request.Options.QueryPredicates, request.Options.Query); var readLinkedEntryListResponse = GetLinkedEntryList.Run(loginResponse.SessionId, request.Url, request.ModuleName, selectFields, linkedSelectFields, query, request.Options.MaxResult); if (readLinkedEntryListResponse != null) { sugarRestResponse.JsonRawRequest = readLinkedEntryListResponse.JsonRawRequest; sugarRestResponse.JsonRawResponse = readLinkedEntryListResponse.JsonRawResponse; var jsonEnityList = readLinkedEntryListResponse.EntityList; if (jsonEnityList != null) { sugarRestResponse.JData = readLinkedEntryListResponse.EntityList.ToString(); sugarRestResponse.StatusCode = readLinkedEntryListResponse.StatusCode; sugarRestResponse.Data = null; } else { sugarRestResponse.Error = readLinkedEntryListResponse.Error; sugarRestResponse.StatusCode = readLinkedEntryListResponse.StatusCode; } } return(sugarRestResponse); } catch (Exception exception) { sugarRestResponse.StatusCode = HttpStatusCode.InternalServerError; sugarRestResponse.Error = ErrorResponse.Format(exception, string.Empty); } finally { Authentication.Logout(request.Url, loginResponse.SessionId); } return(sugarRestResponse); }
/// <summary> /// Gets all entities by page. Page options set in request options. /// </summary> /// <param name="client">SugarRestClient object.</param> /// <param name="request">The request object.</param> /// <param name="modelInfo">The entity model info.</param> /// <returns>SugarRestResponse object.</returns> public static async Task <SugarRestResponse> ExecuteGetPaged(this SugarRestClient client, SugarRestRequest request, ModelInfo modelInfo) { var sugarRestResponse = new SugarRestResponse(); var loginResponse = new LoginResponse(); try { var loginRequest = new LoginRequest { Url = request.Url, Username = request.Username, Password = request.Password }; loginResponse = await Authentication.Login(loginRequest); var selectFields = modelInfo.GetJsonPropertyNames(request.Options.SelectFields); string query = modelInfo.GetQuery(request.Options.QueryPredicates, request.Options.Query); var readEntryListResponse = await GetPagedEntryList.Run(loginResponse.SessionId, loginRequest.Url, request.ModuleName, selectFields, query, request.Options.CurrentPage, request.Options.NumberPerPage); if (readEntryListResponse != null) { sugarRestResponse.JsonRawRequest = readEntryListResponse.JsonRawRequest; sugarRestResponse.JsonRawResponse = readEntryListResponse.JsonRawResponse; var jsonEnityList = readEntryListResponse.EntityList; if (jsonEnityList != null) { sugarRestResponse.JData = readEntryListResponse.EntityList.ToString(); sugarRestResponse.StatusCode = readEntryListResponse.StatusCode; sugarRestResponse.Data = sugarRestResponse.JData.ToObjects(modelInfo.Type); } else { sugarRestResponse.Error = readEntryListResponse.Error; sugarRestResponse.StatusCode = readEntryListResponse.StatusCode; } } return(sugarRestResponse); } catch (Exception exception) { sugarRestResponse.StatusCode = HttpStatusCode.InternalServerError; sugarRestResponse.Error = ErrorResponse.Format(exception, string.Empty); } finally { Authentication.Logout(request.Url, loginResponse.SessionId); } return(sugarRestResponse); }
/// <summary> /// Gets entity by id. /// </summary> /// <param name="client">SugarRestClient object.</param> /// <param name="request">The request object.</param> /// <param name="modelInfo">The entity model info.</param> /// <returns>SugarRestResponse object.</returns> public static SugarRestResponse ExecuteGetById(this SugarRestClient client, SugarRestRequest request, ModelInfo modelInfo) { var sugarRestResponse = new SugarRestResponse(); var loginResponse = new LoginResponse(); try { var loginRequest = new LoginRequest { Url = request.Url, Username = request.Username, Password = request.Password }; loginResponse = Authentication.Login(loginRequest); var selectFields = request.Options == null ? new List <string>() : request.Options.SelectFields; selectFields = modelInfo.GetJsonPropertyNames(selectFields); var readEntryResponse = GetEntry.Run(loginResponse.SessionId, request.Url, request.ModuleName, request.Parameter.ToString(), selectFields); if (readEntryResponse != null) { sugarRestResponse.JsonRawRequest = readEntryResponse.JsonRawRequest; sugarRestResponse.JsonRawResponse = readEntryResponse.JsonRawResponse; var jsonEnityList = readEntryResponse.Entity; if (jsonEnityList != null) { sugarRestResponse.JData = readEntryResponse.Entity.ToString(); sugarRestResponse.StatusCode = readEntryResponse.StatusCode; sugarRestResponse.Data = sugarRestResponse.JData.ToObject(modelInfo.Type); } else { sugarRestResponse.Error = readEntryResponse.Error; sugarRestResponse.StatusCode = readEntryResponse.StatusCode; } } return(sugarRestResponse); } catch (Exception exception) { sugarRestResponse.StatusCode = HttpStatusCode.InternalServerError; sugarRestResponse.Error = ErrorResponse.Format(exception, string.Empty); } finally { Authentication.Logout(request.Url, loginResponse.SessionId); } return(sugarRestResponse); }
/// <summary> /// Update entities. /// </summary> /// <param name="client">SugarRestClient object.</param> /// <param name="request">The request object.</param> /// <param name="modelInfo">The entity model info.</param> /// <returns>SugarRestResponse object.</returns> public static SugarRestResponse ExecuteUpdates(this SugarRestClient client, SugarRestRequest request, ModelInfo modelInfo) { var sugarRestResponse = new SugarRestResponse(); var loginResponse = new LoginResponse(); try { var loginRequest = new LoginRequest { Url = request.Url, Username = request.Username, Password = request.Password }; loginResponse = Authentication.Login(loginRequest); JArray objectList = JsonConverterHelper.SerializeList(request.Parameter, modelInfo.Type); var selectFields = modelInfo.GetJsonPropertyNames(request.Options.SelectFields); var updateEntriesResponse = UpdateEntries.Run(loginResponse.SessionId, loginRequest.Url, request.ModuleName, objectList, selectFields); if (updateEntriesResponse != null) { sugarRestResponse.JsonRawRequest = updateEntriesResponse.JsonRawRequest; sugarRestResponse.JsonRawResponse = updateEntriesResponse.JsonRawResponse; if ((updateEntriesResponse.Ids != null) && (updateEntriesResponse.Ids.Count > 0)) { sugarRestResponse.Data = updateEntriesResponse.Ids; sugarRestResponse.JData = (updateEntriesResponse.Ids == null) ? null : JArray.FromObject(updateEntriesResponse.Ids).ToString(); sugarRestResponse.StatusCode = updateEntriesResponse.StatusCode; } else { sugarRestResponse.Error = updateEntriesResponse.Error; sugarRestResponse.StatusCode = updateEntriesResponse.StatusCode; } } return(sugarRestResponse); } catch (Exception exception) { sugarRestResponse.StatusCode = HttpStatusCode.InternalServerError; sugarRestResponse.Error = ErrorResponse.Format(exception, string.Empty); } finally { Authentication.Logout(request.Url, loginResponse.SessionId); } return(sugarRestResponse); }
/// <summary> /// Insert entity. /// </summary> /// <param name="client">SugarRestClient object.</param> /// <param name="request">The request object.</param> /// <param name="modelInfo">The entity model info.</param> /// <returns>SugarRestResponse object.</returns> public static SugarRestResponse ExecuteInsert(this SugarRestClient client, SugarRestRequest request, ModelInfo modelInfo) { var sugarRestResponse = new SugarRestResponse(); var loginResponse = new LoginResponse(); try { var loginRequest = new LoginRequest { Url = request.Url, Username = request.Username, Password = request.Password }; loginResponse = Authentication.Login(loginRequest); JObject jobject = JsonConverterHelper.Serialize(request.Parameter, modelInfo.Type); var selectFields = modelInfo.GetJsonPropertyNames(request.Options.SelectFields); var insertEntryResponse = InsertEntry.Run(loginResponse.SessionId, loginRequest.Url, request.ModuleName, jobject, selectFields); if (insertEntryResponse != null) { sugarRestResponse.JsonRawRequest = insertEntryResponse.JsonRawRequest; sugarRestResponse.JsonRawResponse = insertEntryResponse.JsonRawResponse; if (!string.IsNullOrEmpty(insertEntryResponse.Id)) { sugarRestResponse.Data = insertEntryResponse.Id; sugarRestResponse.JData = (insertEntryResponse.Id == null) ? null : JToken.FromObject(insertEntryResponse.Id).ToString(); sugarRestResponse.StatusCode = insertEntryResponse.StatusCode; } else { sugarRestResponse.Error = insertEntryResponse.Error; sugarRestResponse.StatusCode = insertEntryResponse.StatusCode; } } return(sugarRestResponse); } catch (Exception exception) { sugarRestResponse.StatusCode = HttpStatusCode.InternalServerError; sugarRestResponse.Error = ErrorResponse.Format(exception, string.Empty); } finally { Authentication.Logout(request.Url, loginResponse.SessionId); } return(sugarRestResponse); }
/// <summary> /// Execute request asynchronously using SugarCrm module name. /// </summary> /// <param name="request">The request object.</param> /// <returns>SugarRestResponse object.</returns> public async Task <SugarRestResponse> ExecuteAsync(SugarRestRequest request) { SugarRestResponse response = new SugarRestResponse(); if (!this.IsRequestValidate(ref request, ref response)) { return(response); } ModelInfo modelInfo = ModelInfo.ReadByName(request.ModuleName); return(await Task.Run(() => { return this.InternalExceute(request, modelInfo); })); }
/// <summary> /// Execute client. /// </summary> /// <param name="request">The request object.</param> /// <returns>SugarRestResponse object.</returns> public SugarRestResponse Execute(SugarRestRequest request) { SugarRestResponse response = new SugarRestResponse(); if (!this.IsRequestValidate(ref request, ref response)) { return(response); } ModelInfo modelInfo = ModelInfo.ReadByName(request.ModuleName); return(this.InternalExceute(request, modelInfo)); }
/// <summary> /// Delete entity. /// </summary> /// <param name="client">SugarRestClient object.</param> /// <param name="request">The request object.</param> /// <param name="modelInfo">The entity model info.</param> /// <returns>SugarRestResponse object.</returns> public static SugarRestResponse ExecuteDelete(this SugarRestClient client, SugarRestRequest request, ModelInfo modelInfo) { var sugarRestResponse = new SugarRestResponse(); var loginResponse = new LoginResponse(); try { var loginRequest = new LoginRequest { Url = request.Url, Username = request.Username, Password = request.Password }; loginResponse = Authentication.Login(loginRequest); var deleteEntryResponse = DeleteEntry.Run(loginResponse.SessionId, loginRequest.Url, request.ModuleName, request.Parameter.ToString()); if (deleteEntryResponse != null) { sugarRestResponse.JsonRawRequest = deleteEntryResponse.JsonRawRequest; sugarRestResponse.JsonRawResponse = deleteEntryResponse.JsonRawResponse; if (!string.IsNullOrEmpty(deleteEntryResponse.Id)) { sugarRestResponse.Data = deleteEntryResponse.Id; sugarRestResponse.JData = (deleteEntryResponse.Id == null) ? null : JToken.FromObject(deleteEntryResponse.Id).ToString(); sugarRestResponse.StatusCode = deleteEntryResponse.StatusCode; } else { sugarRestResponse.Error = deleteEntryResponse.Error; sugarRestResponse.StatusCode = deleteEntryResponse.StatusCode; } } return(sugarRestResponse); } catch (Exception exception) { sugarRestResponse.StatusCode = HttpStatusCode.InternalServerError; sugarRestResponse.Error = ErrorResponse.Format(exception, string.Empty); } finally { Authentication.Logout(request.Url, loginResponse.SessionId); } return(sugarRestResponse); }
/// <summary> /// Execute client based on entity type. /// </summary> /// <param name="request">The request object</param> /// <typeparam name="TEntity">Entity type of EntityBase type</typeparam> /// <returns>SugarRestResponse object.</returns> public SugarRestResponse Execute <TEntity>(SugarRestRequest request) where TEntity : EntityBase { ModelInfo modelInfo = ModelInfo.ReadByType(typeof(TEntity)); request.ModuleName = modelInfo.ModelName; SugarRestResponse response = new SugarRestResponse(); if (!this.IsRequestValidate(ref request, ref response)) { return(response); } return(this.InternalExceute(request, modelInfo)); }
/// <summary> /// Execute request asynchronously using the C# SugarCrm model type. /// </summary> /// <typeparam name="TEntity">The template parameter.</typeparam> /// <param name="request">The request object.</param> /// <returns>SugarRestResponse object.</returns> public async Task <SugarRestResponse> ExecuteAsync <TEntity>(SugarRestRequest request) where TEntity : EntityBase { ModelInfo modelInfo = ModelInfo.ReadByType(typeof(TEntity)); request.ModuleName = modelInfo.ModelName; SugarRestResponse response = new SugarRestResponse(); if (!this.IsRequestValidate(ref request, ref response)) { return(response); } return(await Task.Run(() => { return this.InternalExceute(request, modelInfo); })); }
/// <summary> /// Method checks if request is valid. /// </summary> /// <param name="request">The request object.</param> /// <param name="response">The response object.</param> /// <returns>True or false.</returns> private bool IsRequestValidate(ref SugarRestRequest request, ref SugarRestResponse response) { if (request == null) { response.StatusCode = HttpStatusCode.BadRequest; response.Error = ErrorResponse.Format("Request is invalid."); return(false); } request.Url = string.IsNullOrEmpty(request.Url) ? this.url : request.Url; request.Username = string.IsNullOrEmpty(request.Username) ? this.username : request.Username; request.Password = string.IsNullOrEmpty(request.Password) ? this.password : request.Password; if (!request.IsValid) { response.StatusCode = HttpStatusCode.BadRequest; response.Error = ErrorResponse.Format(request.ValidationMessage); return(false); } return(true); }