public async Task <IActionResult> UpdateContact([FromBody] ViewModels.Contact item, string id)
        {
            /*
             * if (id != item.id)
             * {
             *  return BadRequest();
             * }
             */

            // get the legal entity.
            Guid contactId = new Guid(id);

            DataServiceCollection <Interfaces.Microsoft.Dynamics.CRM.Contact> ContactCollection = new DataServiceCollection <Interfaces.Microsoft.Dynamics.CRM.Contact>(_system);


            Interfaces.Microsoft.Dynamics.CRM.Contact contact = await _system.Contacts.ByKey(contactId).GetValueAsync();

            _system.UpdateObject(contact);
            // copy values over from the data provided
            contact.CopyValues(item);

            DataServiceResponse dsr = await _system.SaveChangesAsync(SaveChangesOptions.PostOnlySetProperties | SaveChangesOptions.BatchWithIndependentOperations);

            foreach (OperationResponse result in dsr)
            {
                if (result.StatusCode == 500) // error
                {
                    return(StatusCode(500, result.Error.Message));
                }
            }
            return(Json(contact.ToViewModel()));
        }
        public async Task <IActionResult> CreateContact([FromBody] ViewModels.Contact viewModel)
        {
            Interfaces.Microsoft.Dynamics.CRM.Contact item = viewModel.ToModel();

            // create a new contact.
            Interfaces.Microsoft.Dynamics.CRM.Contact contact = new Interfaces.Microsoft.Dynamics.CRM.Contact();

            // create a DataServiceCollection to add the record
            DataServiceCollection <Interfaces.Microsoft.Dynamics.CRM.Contact> ContactCollection = new DataServiceCollection <Interfaces.Microsoft.Dynamics.CRM.Contact>(_system);

            // add a new contact.
            ContactCollection.Add(contact);

            // changes need to made after the add in order for them to be saved.
            contact.CopyValues(item);

            // PostOnlySetProperties is used so that settings such as owner will get set properly by the dynamics server.

            DataServiceResponse dsr = await _system.SaveChangesAsync(SaveChangesOptions.PostOnlySetProperties | SaveChangesOptions.BatchWithSingleChangeset);

            foreach (OperationResponse result in dsr)
            {
                if (result.StatusCode == 500) // error
                {
                    return(StatusCode(500, result.Error.Message));
                }
            }
            contact.Contactid = dsr.GetAssignedId();
            // if we have not yet authenticated, then this is the new record for the user.
            string       temp         = _httpContextAccessor.HttpContext.Session.GetString("UserSettings");
            UserSettings userSettings = JsonConvert.DeserializeObject <UserSettings>(temp);

            if (userSettings.IsNewUserRegistration)
            {
                if (string.IsNullOrEmpty(userSettings.ContactId))
                {
                    userSettings.ContactId = contact.Contactid.ToString();
                    string userSettingsString = JsonConvert.SerializeObject(userSettings);
                    // add the user to the session.
                    _httpContextAccessor.HttpContext.Session.SetString("UserSettings", userSettingsString);
                }
            }

            return(Json(contact.ToViewModel()));
        }
        public async Task <IActionResult> GetContact(string id)
        {
            ViewModels.Contact result = null;
            // query the Dynamics system to get the contact record.

            Guid?contactId = new Guid(id);

            Interfaces.Microsoft.Dynamics.CRM.Contact contact = null;
            if (contactId != null)
            {
                try
                {
                    contact = await _system.Contacts.ByKey(contactId).GetValueAsync();

                    result = contact.ToViewModel();
                }
                catch (Microsoft.OData.Client.DataServiceQueryException dsqe)
                {
                    return(new NotFoundResult());
                }
            }

            return(Json(result));
        }