/// <inheritdoc /> public async Task UpdateAsync(long companyId, AgileCrmCompanyRequest agileCrmCompanyModel) { const string MethodName = nameof(this.UpdateAsync); this.logger.LogMethodStart(ClassName, MethodName); try { // Validate argument object agileCrmCompanyModel.ValidateModel(); // Serialize object to JSON var companyEntityBase = agileCrmCompanyModel.ToCompanyEntityBase(); companyEntityBase.Id = companyId; var stringContent = companyEntityBase.ToStringContent(); // Send JSON to server const string Uri = "contacts/edit-properties"; var httpResponseMessage = await this.httpClient.PutAsync(Uri, stringContent).ConfigureAwait(false); // Analyze server response for errors httpResponseMessage.EnsureSuccessStatusCode(); } catch (Exception exception) { this.logger.LogException(ClassName, MethodName, exception); throw; } this.logger.LogUpdated(ServiceType.Company, companyId); this.logger.LogMethodEnd(ClassName, MethodName); }
/// <inheritdoc /> public async Task CreateAsync(AgileCrmCompanyRequest agileCrmCompanyModel) { const string MethodName = nameof(this.CreateAsync); this.logger.LogMethodStart(ClassName, MethodName); var companyId = default(long); try { // Validate argument object agileCrmCompanyModel.ValidateModel(); // Serialize object to JSON var companyEntityBase = agileCrmCompanyModel.ToCompanyEntityBase(); var stringContent = companyEntityBase.ToStringContent(); // Send JSON to server const string Uri = "contacts"; var httpResponseMessage = await this.httpClient.PostAsync(Uri, stringContent).ConfigureAwait(false); // Analyze server response for errors httpResponseMessage.EnsureSuccessStatusCode(); // Retrieve identifier for logging var httpContentAsString = await httpResponseMessage.Content.ReadAsStringAsync().ConfigureAwait(false); companyId = httpContentAsString.DeserializeJson(new { id = default(long) }).id; } catch (Exception exception) { this.logger.LogException(ClassName, MethodName, exception); throw; } this.logger.LogCreated(ServiceType.Company, companyId); this.logger.LogMethodEnd(ClassName, MethodName); }
/// <summary> /// Maps a AgileCrmCompanyEntity onto a CompanyEntityBase. /// </summary> /// <param name="agileCrmCompanyModel">The AgileCRM company model.</param> /// <returns> /// <see cref="AgileCrmCompanyEntity" />. /// </returns> public static AgileCrmCompanyEntity ToCompanyEntityBase(this AgileCrmCompanyRequest agileCrmCompanyModel) { var agileCrmPropertyEntities = new List <AgileCrmPropertyEntityBase> { new AgileCrmPropertyEntity { Type = PropertyType.System, Name = CompanyPropertyName.Name, Value = agileCrmCompanyModel.Name }, new AgileCrmPropertyEntity { Type = PropertyType.System, Name = CompanyPropertyName.URL, Value = agileCrmCompanyModel.URL } }; foreach (var keyValuePair in agileCrmCompanyModel.Phone) { agileCrmPropertyEntities.Add( new AgileCrmPropertySubTypeEntity { Type = PropertyType.System, Name = PropertyName.Phone, Value = keyValuePair.Value, SubType = keyValuePair.Key.ToPhoneTypeValue() }); } foreach (var keyValuePair in agileCrmCompanyModel.Email) { agileCrmPropertyEntities.Add( new AgileCrmPropertySubTypeEntity { Type = PropertyType.System, Name = PropertyName.Email, Value = keyValuePair.Value, SubType = keyValuePair.Key.ToEmailTypeValue() }); } foreach (var keyValuePair in agileCrmCompanyModel.Website) { agileCrmPropertyEntities.Add( new AgileCrmPropertySubTypeEntity { Type = PropertyType.System, Name = PropertyName.Website, Value = keyValuePair.Value, SubType = keyValuePair.Key.ToWebsiteTypeValue() }); } agileCrmPropertyEntities.Add( new AgileCrmPropertyAddressEntity { Type = PropertyType.System, Name = PropertyName.Address, Value = new AgileCrmAddressEntity { Address = agileCrmCompanyModel.Address, City = agileCrmCompanyModel.City, State = agileCrmCompanyModel.State, Country = agileCrmCompanyModel.Country, ZipCode = agileCrmCompanyModel.ZipCode }, SubType = agileCrmCompanyModel.AddressType.ToAddressTypeValue() }); foreach (var keyValuePair in agileCrmCompanyModel.CustomFields) { agileCrmPropertyEntities.Add( new AgileCrmPropertyEntity { Type = PropertyType.Custom, Name = keyValuePair.Key, Value = keyValuePair.Value }); } var tagsCollection = new List <string>(); foreach (var stringItem in agileCrmCompanyModel.Tags) { tagsCollection.Add(stringItem); } var agileCrmServerCompanyEntity = new AgileCrmCompanyEntity { // Id = (set by calling method if required). LeadScore = agileCrmCompanyModel.LeadScore, StarValue = agileCrmCompanyModel.StarValue, Properties = agileCrmPropertyEntities, Tags = tagsCollection }; return(agileCrmServerCompanyEntity); }