Beispiel #1
0
        public IActionResult Post([FromBody] ProductInfoDtoV1 model)
        {
            if (!this.ModelState.IsValid)
            {
                var error = new Dictionary <string, string>();
                error.Add("code", "422");
                //  Microsoft.AspNetCore.Http.StatusCodes

                return(BadRequest("Invalid format"));
            }

            try
            {
                // var mappedModel = mapper.Map<ProductInfo>(model);
                // var result = repo.Add(mappedModel);
                // repo.SaveChanges();
                // var mappedResult = mapper.Map<ProductInfoDtoV1>(result);
                // return Created("", mappedResult);
                return(Created("", null));
            }
            catch (Exception ex)
            {
                logger.LogError("Failed to execute POST " + ex.Message);
                return(BadRequest("Save Failed"));
            }
        }
Beispiel #2
0
        public void ProductV1_EndpointsReturnSuccessAndCorrectContentType(string url, string Method, string payload)
        {
            // Arrange
            var client = factory.CreateClient();

            client.DefaultRequestHeaders.Add("api-version", "1.0");
            client.DefaultRequestHeaders.Add("Authorization", this.authTokenFixture.Token);
            // client.DefaultRequestHeaders.Add("Accept", "text/plain, application/json, text/json");
            //client.DefaultRequestHeaders.Add("Accept", "application/json, text/json");

            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
            // Act
            System.Threading.Tasks.Task <System.Net.Http.HttpResponseMessage> responseTask = null;
            if (Method == "GET")
            {
                output.WriteLine("GET is Selected");
                responseTask = client.GetAsync(url);
            }
            else if (Method == "POST")
            {
                output.WriteLine("POST is Selected");

                Random rnd = new Random();

                ProductInfoDtoV1 newproduct = new ProductInfoDtoV1();
                newproduct.Id = rnd.Next(1000, 100000);
                var stringPayload = JsonConvert.SerializeObject(newproduct);
                var httpContent   = new StringContent(stringPayload, Encoding.UTF8, "application/json");
                responseTask = client.PostAsync(url, httpContent);
            }
            else if (Method == "PUT")
            {
                output.WriteLine("PUT is Selected");
                var httpContent = new StringContent(payload, Encoding.UTF8, "application/json");
                responseTask = client.PutAsync(url, httpContent);
            }
            else if (Method == "DELETE")
            {
                output.WriteLine("DELETE is Selected");
                //var httpContent = new StringContent(payload, Encoding.UTF8, "application/json");
                url          = url + "?id=" + payload;
                responseTask = client.DeleteAsync(url);
            }
            responseTask.Wait();
            var response = responseTask.Result;

            // Assert
            response.EnsureSuccessStatusCode(); // Status Code 200-299
            // Assert.Equal("application/json; charset=utf-8",
            //     response.Content.Headers.ContentType.ToString());
        }
Beispiel #3
0
        public IActionResult Put([FromBody] ProductInfoDtoV1 model)
        {
            if (!this.ModelState.IsValid)
            {
                return(BadRequest("Invalid format"));
            }

            try
            {
                var mappedModel = mapper.Map <ProductInfo>(model);
                // repo.Update(mappedModel);
                // repo.SaveChanges();
                return(Ok());
            }
            catch (Exception ex)
            {
                logger.LogError("Failed to execute PUT " + ex.Message);
                return(BadRequest());
            }
        }