/// <summary> /// Gets the module model info object. /// </summary> /// <param name="type">The module model type.</param> /// <returns>The model info object.</returns> public static ModelInfo ReadByType(Type type) { var modelInfo = new ModelInfo(); if (type != null) { object[] classAttrs = type.GetTypeInfo().GetCustomAttributes(typeof(ModulePropertyAttribute), false).ToArray(); if (classAttrs.Length == 1) { string modelName = ((ModulePropertyAttribute)classAttrs[0]).ModuleName; string jsonModelName = ((ModulePropertyAttribute)classAttrs[0]).TableName; modelInfo.ModelName = modelName; modelInfo.JsonModelName = jsonModelName; modelInfo.Type = type; modelInfo.ModelProperties = new List <ModelProperty>(); var props = type.GetTypeInfo().DeclaredProperties; foreach (PropertyInfo prop in props) { object[] propAttrs = prop.CustomAttributes.ToArray(); foreach (object attr in propAttrs) { var modelProperty = new ModelProperty(); var customAttr = attr as CustomAttributeData; var jsonProperty = attr as JsonPropertyAttribute; if (customAttr.AttributeType == typeof(JsonPropertyAttribute)) { modelProperty.Name = prop.Name; modelProperty.Type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType; modelProperty.JsonName = customAttr.NamedArguments.FirstOrDefault(a => a.MemberName == "PropertyName").TypedValue.Value.ToString(); modelInfo.ModelProperties.Add(modelProperty); } } } } } return(modelInfo); }
/// <summary> /// Gets the module model info object. /// </summary> /// <param name="type">The module model type.</param> /// <returns>The model info object.</returns> public static ModelInfo ReadByType(Type type) { var modelInfo = new ModelInfo(); if (type != null) { object[] classAttrs = type.GetCustomAttributes(typeof(ModulePropertyAttribute), false); if (classAttrs.Length == 1) { string modelName = ((ModulePropertyAttribute)classAttrs[0]).ModuleName; string jsonModelName = ((ModulePropertyAttribute)classAttrs[0]).TableName; modelInfo.ModelName = modelName; modelInfo.JsonModelName = jsonModelName; modelInfo.Type = type; modelInfo.ModelProperties = new List <ModelProperty>(); var props = type.GetProperties(); foreach (PropertyInfo prop in props) { object[] propAttrs = prop.GetCustomAttributes(true); foreach (object attr in propAttrs) { var modelProperty = new ModelProperty(); var jsonProperty = attr as JsonPropertyAttribute; if (jsonProperty != null) { modelProperty.Name = prop.Name; modelProperty.Type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType; modelProperty.JsonName = jsonProperty.PropertyName; modelInfo.ModelProperties.Add(modelProperty); } } } } } return(modelInfo); }
/// <summary> /// Gets entity by id - returned data with linked modules data. /// </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 ExecuteLinkedGetById(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); var readEntryResponse = GetLinkedEntry.Run(loginResponse.SessionId, request.Url, request.ModuleName, request.Parameter.ToString(), selectFields, linkedSelectFields); 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 = null; } 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> /// 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> /// 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> /// 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 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 = Authentication.Login(loginRequest); var selectFields = modelInfo.GetJsonPropertyNames(request.Options.SelectFields); string query = modelInfo.GetQuery(request.Options.QueryPredicates, request.Options.Query); var readEntryListResponse = 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> /// Execute request. /// </summary> /// <param name="request">The request object.</param> /// <param name="modelInfo">The model info for the referenced SugarCrm module.</param> /// <returns>SugarRestResponse object.</returns> private async Task <SugarRestResponse> InternalExceute(SugarRestRequest request, ModelInfo modelInfo) { switch (request.RequestType) { case RequestType.ReadById: { return(await this.ExecuteGetById(request, modelInfo)); } case RequestType.BulkRead: { return(await this.ExecuteGetAll(request, modelInfo)); } case RequestType.PagedRead: { return(await this.ExecuteGetPaged(request, modelInfo)); } case RequestType.Create: { return(await this.ExecuteInsert(request, modelInfo)); } case RequestType.BulkCreate: { return(await this.ExecuteInserts(request, modelInfo)); } case RequestType.Update: { return(await this.ExecuteUpdate(request, modelInfo)); } case RequestType.BulkUpdate: { return(await this.ExecuteUpdates(request, modelInfo)); } case RequestType.Delete: { return(await this.ExecuteDelete(request, modelInfo)); } case RequestType.LinkedReadById: { return(await this.ExecuteLinkedGetById(request, modelInfo)); } case RequestType.LinkedBulkRead: { return(await this.ExecuteLinkedGetAll(request, modelInfo)); } } throw new Exception("Request type is invalid!"); }