public async Task <IActionResult> CreateEstablishment([FromBody] ViewModels.AdoxioEstablishment item)
        {
            // create a new establishment.
            var adoxioEstablishment = new Adoxio_establishment();

            // create a DataServiceCollection to add the record
            var EstablishmentCollection = new DataServiceCollection <Adoxio_establishment>(_system);

            EstablishmentCollection.Add(adoxioEstablishment);

            adoxioEstablishment.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.BatchWithIndependentOperations);

            foreach (OperationResponse operationResult in dsr)
            {
                if (operationResult.StatusCode == 500) // error
                {
                    return(StatusCode(500, operationResult.Error.Message));
                }
            }
            ViewModels.AdoxioEstablishment result = adoxioEstablishment.ToViewModel();
            result.id = ((Guid)dsr.GetAssignedId()).ToString();

            return(Json(result));
        }
        public async Task <IActionResult> UpdateEstablishment([FromBody] ViewModels.AdoxioEstablishment item, string id)
        {
            if (id != item.id)
            {
                return(BadRequest());
            }
            // get the establishment.
            Guid adoxio_establishmetid = new Guid(id);
            DataServiceCollection <Interfaces.Microsoft.Dynamics.CRM.Adoxio_establishment> AccountCollection = new DataServiceCollection <Interfaces.Microsoft.Dynamics.CRM.Adoxio_establishment>(_system);

            Adoxio_establishment adoxioEstablishment = await _system.Adoxio_establishments.ByKey(adoxio_establishmetid).GetValueAsync();

            _system.UpdateObject(adoxioEstablishment);
            // copy values over from the data provided
            adoxioEstablishment.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.BatchWithIndependentOperations);

            foreach (OperationResponse result in dsr)
            {
                if (result.StatusCode == 500) // error
                {
                    return(StatusCode(500, result.Error.Message));
                }
            }
            return(Json(adoxioEstablishment.ToViewModel()));
        }
        public async Task <IActionResult> GetEstablishment(string id)
        {
            ViewModels.AdoxioEstablishment result = null;
            // query the Dynamics system to get the establishment record.

            Guid?adoxio_establishment_id       = new Guid(id);
            Adoxio_establishment establishment = null;

            if (adoxio_establishment_id != null)
            {
                try
                {
                    establishment = await _system.Adoxio_establishments.ByKey(adoxio_establishment_id).GetValueAsync();

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

            return(Json(result));
        }
Exemple #4
0
        public async System.Threading.Tasks.Task TestCRUD()
        {
            string initialName = "InitialName";
            string changedName = "ChangedName";

            var loginUser = randomNewUserName("NewLoginUser", 6);
            var strId     = await LoginAndRegisterAsNewUser(loginUser);

            // C - Create
            var request = new HttpRequestMessage(HttpMethod.Post, "/api/" + service);

            Adoxio_establishment adoxio_establishment = new Adoxio_establishment()
            {
                Adoxio_establishmentid = Guid.NewGuid(),
                Adoxio_name            = initialName
            };


            ViewModels.AdoxioEstablishment viewmodel_adoxio_establishment = adoxio_establishment.ToViewModel();

            string jsonString = JsonConvert.SerializeObject(viewmodel_adoxio_establishment);

            request.Content = new StringContent(jsonString, Encoding.UTF8, "application/json");

            var response = await _client.SendAsync(request);

            response.EnsureSuccessStatusCode();

            // parse as JSON.
            jsonString = await response.Content.ReadAsStringAsync();

            ViewModels.AdoxioEstablishment responseViewModel = JsonConvert.DeserializeObject <ViewModels.AdoxioEstablishment>(jsonString);

            // name should match.
            Assert.Equal(initialName, responseViewModel.Name);
            Guid id = new Guid(responseViewModel.id);

            // R - Read

            request  = new HttpRequestMessage(HttpMethod.Get, "/api/" + service + "/" + id);
            response = await _client.SendAsync(request);

            response.EnsureSuccessStatusCode();

            jsonString = await response.Content.ReadAsStringAsync();

            responseViewModel = JsonConvert.DeserializeObject <ViewModels.AdoxioEstablishment>(jsonString);
            Assert.Equal(initialName, responseViewModel.Name);

            // U - Update
            adoxio_establishment.Adoxio_name            = changedName;
            adoxio_establishment.Adoxio_establishmentid = id;

            request = new HttpRequestMessage(HttpMethod.Put, "/api/" + service + "/" + id)
            {
                Content = new StringContent(JsonConvert.SerializeObject(adoxio_establishment.ToViewModel()), Encoding.UTF8, "application/json")
            };
            response = await _client.SendAsync(request);

            jsonString = await response.Content.ReadAsStringAsync();

            response.EnsureSuccessStatusCode();

            // verify that the update persisted.

            request  = new HttpRequestMessage(HttpMethod.Get, "/api/" + service + "/" + id);
            response = await _client.SendAsync(request);

            response.EnsureSuccessStatusCode();

            jsonString = await response.Content.ReadAsStringAsync();

            responseViewModel = JsonConvert.DeserializeObject <ViewModels.AdoxioEstablishment>(jsonString);
            Assert.Equal(changedName, responseViewModel.Name);

            // D - Delete

            request  = new HttpRequestMessage(HttpMethod.Post, "/api/" + service + "/" + id + "/delete");
            response = await _client.SendAsync(request);

            response.EnsureSuccessStatusCode();

            // second delete should return a 404.
            request  = new HttpRequestMessage(HttpMethod.Post, "/api/" + service + "/" + id + "/delete");
            response = await _client.SendAsync(request);

            Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);

            // should get a 404 if we try a get now.
            request  = new HttpRequestMessage(HttpMethod.Get, "/api/" + service + "/" + id);
            response = await _client.SendAsync(request);

            Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);

            await LogoutAndCleanupTestUser(strId);
        }