예제 #1
0
        public IActionResult GetCustomProduct(string id)
        {
            ViewModels.CustomProduct result = null;

            if (!string.IsNullOrEmpty(id) && Guid.TryParse(id, out Guid customProductGuid))
            {
                // query the Dynamics system to get the contact record.
                MicrosoftDynamicsCRMbcgovCustomproduct contact = _dynamicsClient.GetCustomProductById(customProductGuid);

                if (contact != null)
                {
                    result = contact.ToViewModel();
                }
                else
                {
                    return(new NotFoundResult());
                }
            }
            else
            {
                return(BadRequest());
            }

            return(Json(result));
        }
예제 #2
0
        /// <summary>
        /// Create a new equipment notification, using the passed parameters to create the equipment notification view model.
        /// </summary>
        /// <param name="incidentId"></param>
        /// <returns>The http response of the creation request.</returns>
        private async Task <HttpResponseMessage> CreateNewEquipmentNotification(String incidentId)
        {
            ViewModels.CustomProduct viewmodel_equipmentnotification = new ViewModels.CustomProduct()
            {
                incidentId = incidentId
            };
            string jsonString = JsonConvert.SerializeObject(viewmodel_equipmentnotification);

            return(await CreateNewTypeWithContent(jsonString));
        }
        /// <summary>
        /// Create a new custom product, using the passed parameters to create the custom product view model.
        /// </summary>
        /// <param name="productDescriptionAndIntendedUse"></param>
        /// <param name="incidentId"></param>
        /// <returns>The http response of the creation request.</returns>
        private async Task <HttpResponseMessage> CreateNewCustomProduct(String productDescriptionAndIntendedUse, String incidentId)
        {
            ViewModels.CustomProduct viewmodel_customproduct = new ViewModels.CustomProduct()
            {
                productdescriptionandintendeduse = productDescriptionAndIntendedUse,
                incidentId = incidentId
            };
            string jsonString = JsonConvert.SerializeObject(viewmodel_customproduct);

            return(await CreateNewTypeWithContent(jsonString));
        }
        public async System.Threading.Tasks.Task TestUnicodeSupport()
        {
            var incidentGuid = await CreateNewApplicationGuid(await GetAccountForCurrentUser());

            // Create a custom product with French Unicode characters in the product description and intended use string.
            var response = await CreateNewCustomProduct(FRENCH_CHARACTERS, incidentGuid.ToString());

            response.EnsureSuccessStatusCode();

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

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

            // productdescriptionandintendeduse should match.
            Assert.Equal(FRENCH_CHARACTERS, responseViewModel.productdescriptionandintendeduse);
        }
        public async System.Threading.Tasks.Task TestMaxCharacterLength()
        {
            var incidentGuid = await CreateNewApplicationGuid(await GetAccountForCurrentUser());

            // Create a custom product max number of characters in the product description and intended use string.
            string maxString = TestUtilities.RandomANString(MAX_CHAR_LENGTH_PRODUCT_DESCRIPTION_AND_INTENDED_USE);
            var    response  = await CreateNewCustomProduct(maxString, incidentGuid.ToString());

            response.EnsureSuccessStatusCode();

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

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

            // productdescriptionandintendeduse should match.
            Assert.Equal(maxString, responseViewModel.productdescriptionandintendeduse);
        }
 /// <summary>
 /// Convert a given voteQuestion to a ViewModel
 /// </summary>
 public static ViewModels.CustomProduct ToViewModel(this MicrosoftDynamicsCRMbcgovCustomproduct customProduct)
 {
     ViewModels.CustomProduct result = null;
     if (customProduct != null)
     {
         result = new ViewModels.CustomProduct();
         if (customProduct.BcgovCustomproductid != null)
         {
             result.id = customProduct.BcgovCustomproductid;
         }
         result.productdescriptionandintendeduse = customProduct.BcgovProductdescriptionandintendeduse;
         if (customProduct.BcgovPurpose != null && customProduct.BcgovPurpose != 0)
         {
             result.Purpose = (ProductPurpose)customProduct.BcgovPurpose;
         }
     }
     return(result);
 }
예제 #7
0
        public async Task <IActionResult> CreateCustomProduct([FromBody] ViewModels.CustomProduct item)
        {
            if (string.IsNullOrEmpty(item.incidentId))
            {
                return(BadRequest("IncidentId missing"));
            }

            int?maxLength = ControllerUtility.GetAttributeMaxLength((DynamicsClient)_dynamicsClient, _cache, _logger, ENTITY_NAME, "bcgov_productdescriptionandintendeduse");

            if (maxLength != null && item.productdescriptionandintendeduse.Length > maxLength)
            {
                return(BadRequest("productdescriptionandintendeduse exceeds maximum field length"));
            }

            // get UserSettings from the session
            string       temp         = _httpContextAccessor.HttpContext.Session.GetString("UserSettings");
            UserSettings userSettings = JsonConvert.DeserializeObject <UserSettings>(temp);


            // create a new custom product.
            MicrosoftDynamicsCRMbcgovCustomproduct customProduct = new MicrosoftDynamicsCRMbcgovCustomproduct();

            customProduct.CopyValues(item);
            customProduct.RelatedApplicationODataBind = _dynamicsClient.GetEntityURI("incidents", item.incidentId);

            try
            {
                customProduct = await _dynamicsClient.Customproducts.CreateAsync(customProduct);
            }
            catch (OdataerrorException odee)
            {
                _logger.LogError("Error updating custom product");
                _logger.LogError("Request:");
                _logger.LogError(odee.Request.Content);
                _logger.LogError("Response:");
                _logger.LogError(odee.Response.Content);
            }


            return(Json(customProduct.ToViewModel()));
        }
예제 #8
0
        public IActionResult UpdateCustomProduct([FromBody] ViewModels.CustomProduct item, string id)
        {
            if (!string.IsNullOrEmpty(id) && Guid.TryParse(id, out Guid customProductGuid))
            {
                int?maxLength = ControllerUtility.GetAttributeMaxLength((DynamicsClient)_dynamicsClient, _cache, _logger, ENTITY_NAME, "bcgov_productdescriptionandintendeduse");
                if (maxLength != null && item.productdescriptionandintendeduse.Length > maxLength)
                {
                    return(BadRequest("productdescriptionandintendeduse exceeds maximum field length"));
                }

                // get the customProduct
                MicrosoftDynamicsCRMbcgovCustomproduct customProduct = _dynamicsClient.GetCustomProductById(customProductGuid);
                if (customProduct == null)
                {
                    return(new NotFoundResult());
                }
                MicrosoftDynamicsCRMbcgovCustomproduct patchCustomProduct = new MicrosoftDynamicsCRMbcgovCustomproduct();
                patchCustomProduct.CopyValues(item);
                try
                {
                    _dynamicsClient.Customproducts.Update(customProductGuid.ToString(), patchCustomProduct);
                }
                catch (OdataerrorException odee)
                {
                    _logger.LogError("Error updating Custom Product");
                    _logger.LogError("Request:");
                    _logger.LogError(odee.Request.Content);
                    _logger.LogError("Response:");
                    _logger.LogError(odee.Response.Content);
                }

                customProduct = _dynamicsClient.GetCustomProductById(customProductGuid);
                return(Json(customProduct.ToViewModel()));
            }
            else
            {
                return(BadRequest());
            }
        }
        public async System.Threading.Tasks.Task TestCRUD()
        {
            string initialName = "InitialName";
            string changedName = "ChangedName";

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

            ViewModels.CustomProduct viewmodel_customproduct = new ViewModels.CustomProduct()
            {
                productdescriptionandintendeduse = initialName,
                incidentId = incidentId.ToString()
            };

            string jsonString = JsonConvert.SerializeObject(viewmodel_customproduct);

            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.CustomProduct responseViewModel = JsonConvert.DeserializeObject <ViewModels.CustomProduct>(jsonString);

            // name should match.
            Assert.Equal(initialName, responseViewModel.productdescriptionandintendeduse);
            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.CustomProduct>(jsonString);
            Assert.Equal(initialName, responseViewModel.productdescriptionandintendeduse);

            // U - Update
            ViewModels.CustomProduct patchModel = new ViewModels.CustomProduct()
            {
                productdescriptionandintendeduse = changedName
            };

            request = new HttpRequestMessage(HttpMethod.Put, "/api/" + service + "/" + id)
            {
                Content = new StringContent(JsonConvert.SerializeObject(patchModel), 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.CustomProduct>(jsonString);
            Assert.Equal(changedName, responseViewModel.productdescriptionandintendeduse);

            // 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);
        }
예제 #10
0
 public static void CopyValues(this MicrosoftDynamicsCRMbcgovCustomproduct to, ViewModels.CustomProduct from)
 {
     to.BcgovProductdescriptionandintendeduse = from.productdescriptionandintendeduse;
     if (from.Purpose != 0)
     {
         to.BcgovPurpose = (int)from.Purpose;
     }
 }