Example #1
0
        /// <summary>
        /// Maps a AgileCrmDealNoteModel onto a DealNoteEntityBase.
        /// </summary>
        /// <param name="agileCrmNoteModel">The AgileCRM note model.</param>
        /// <returns>
        ///   <see cref="AgileCrmDealNoteEntity" />.
        /// </returns>
        public static AgileCrmDealNoteEntity ToDealNoteEntityBase(this AgileCrmNoteRequest agileCrmNoteModel)
        {
            var agileCrmServerNoteEntity = new AgileCrmDealNoteEntity
            {
                // Id = (set by calling method if required).
                // DealId = (set by calling method if required).
                Subject     = agileCrmNoteModel.Subject,
                Description = agileCrmNoteModel.Description
            };

            return(agileCrmServerNoteEntity);
        }
        /// <inheritdoc />
        public async Task CreateAsync(long dealId, AgileCrmNoteRequest agileCrmNoteModel)
        {
            const string MethodName = nameof(this.CreateAsync);

            this.logger.LogMethodStart(ClassName, MethodName);

            var noteId = default(long);

            try
            {
                // Validate argument object
                agileCrmNoteModel.ValidateModel();

                // Serialize object to JSON
                var dealNoteEntityBase = agileCrmNoteModel.ToDealNoteEntityBase();

                dealNoteEntityBase.DealId = new List <string> {
                    dealId.ToString()
                };

                var stringContent = dealNoteEntityBase.ToStringContent();

                // Send JSON to server
                const string Uri = "opportunity/deals/notes";

                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);

                noteId = JsonConvert.DeserializeAnonymousType(httpContentAsString, new { id = default(long) }).id;
            }
            catch (Exception exception)
            {
                this.logger.LogException(ClassName, MethodName, exception);
                throw;
            }

            this.logger.LogCreated(ServiceType.Note, noteId);
            this.logger.LogMethodEnd(ClassName, MethodName);
        }