public async Task <IHttpActionResult> PostAsync(ProductScore productScore)
        {
            //            if (ModelState.IsValid)

            //TODO: Should I be using a JsonObject here instead of a typed object?
            //if so why? It works with a typed object
            //There will be an overhead of deserialising the JSON into an object, but how much of a performance hit does that cause?

            if (!ValidProductScore(productScore /*, false*/))
            {
                return(BadRequest("Supplied productScore is Invalid."));
            }

            try
            {
                productScore.LastUpdated = DateTime.UtcNow;
                var insertOccured = await ProductStoreRepository.InsertAsync(productScore).ConfigureAwait(false);

                if (!insertOccured || string.IsNullOrEmpty(productScore.Id.ToString()))
                {
                    // but why was it not saved, and no exception was thrown?!?!
                    return(InternalServerError());
                }

                var location = new Uri(Url.Link("GetProductScoreById", new { productId = productScore.Id }));

                return(Created(location, productScore));
            }
            catch (Exception e)
            {
                //log the exception detail
                return(InternalServerError());
            }
        }
        public async Task <IHttpActionResult> PutAsync(ProductScore productScore)
        {
            try
            {
                if (!ValidProductScore(productScore))
                {
                    return(BadRequest("Supplied productScore is Invalid."));
                }

                productScore.LastUpdated = DateTime.UtcNow;
                var updateOccured = await ProductStoreRepository.UpdateAsync(productScore).ConfigureAwait(false);

                if (updateOccured)
                {
                    return(ResponseMessage(new HttpResponseMessage(HttpStatusCode.NoContent)));
                }
                else
                {
                    return(NotFound());
                }
            }
            catch (Exception e)
            {
                //log the exception
                return(InternalServerError());
            }
        }
        private static ByteArrayContent ConvertProductToByteArrayContent(ProductScore productScore)
        {
            var json = JsonConvert.SerializeObject(productScore);

            var buffer      = System.Text.Encoding.UTF8.GetBytes(json);
            var byteContent = new ByteArrayContent(buffer);

            byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            return(byteContent);
        }
        private bool ValidProductScore(ProductScore productScore)
        {
            //            return ValidProductScore(productScore, true);
            //        }
            //
            //        private bool ValidProductScore(ProductScore productScore, bool validateProductId)
            //        {
            //            var validProductId = (validateProductId ? ValidateProductId(productScore.Id) : true);
            //TODO: output a list of validation errors

            return(/*validProductId && */ !string.IsNullOrEmpty(productScore.Product) && productScore.ImmuneSystem != null);
        }
Esempio n. 5
0
        private static ProductScore MapProductScoreViewModelToProductScore(ProductScoreViewModel newProductScore, bool isNewProduct)
        {
            var scoreToSave = new ProductScore();

            if (!isNewProduct)
            {
                scoreToSave.Id = newProductScore.Id;
            }

            scoreToSave.Product             = newProductScore.Product;
            scoreToSave.Sdet                = newProductScore.SDET;
            scoreToSave.Team                = newProductScore.SelectedTeam;
            scoreToSave.ImmuneSystem        = new ImmuneSystem();
            scoreToSave.ImmuneSystem.Scores = new List <Score>
            {
                new Score()
                {
                    Category = Constants.AUTOMATION, Grade = newProductScore.SelectedAutomationScore
                },
                new Score()
                {
                    Category = Constants.CROSSBROWSER, Grade = newProductScore.SelectedCrossBrowserScore
                },
                new Score()
                {
                    Category = Constants.MONITORING_AND_ALERTING, Grade = newProductScore.SelectedMonitoringScore
                },
                new Score()
                {
                    Category = Constants.SECURITY, Grade = newProductScore.SelectedSecurityScore
                },
                new Score()
                {
                    Category = Constants.PERFORMANCE, Grade = newProductScore.SelectedPerformanceScore
                },
                new Score()
                {
                    Category = Constants.TECH_DEBT, Grade = newProductScore.SelectedTechDebtScore
                },
                new Score()
                {
                    Category = Constants.RELEASE, Grade = newProductScore.SelectedReleaseScore
                }
            };

            return(scoreToSave);
        }
        public async Task <HttpResponseMessage> UpdateProduct(ProductScore productScore)
        {
            var byteContent = ConvertProductToByteArrayContent(productScore);

            return(await _httpClient.PutAsync(_productscoresApiUri.AbsoluteUri, byteContent).ConfigureAwait(false));
        }