Exemplo n.º 1
0
        // POST een FormContent
        public HttpResponseMessage Post(FormContentBindingModel formContentBindingModel)
        {
            try
            {
                IsJSON isJson = IsValidJson(formContentBindingModel.Content); // Check of JSON klopt en maak resultaat object
                if (!isJson.Status)                                           // als resultaat object status fals is return error
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "JSON in 'content' is not correct JSON: " + isJson.Error));
                }

                ContentEqeulsHeadersCheck headersCheck = ContentEqeulsHeaders(formContentBindingModel);
                if (!headersCheck.Status)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, headersCheck.Error));
                }

                FormContent formContent = new FormContent
                {
                    Content       = formContentBindingModel.Content,
                    FormulierenId = formContentBindingModel.FormId
                };


                db.FormContent.Add(formContent);
                db.SaveChanges();

                return(Request.CreateResponse(HttpStatusCode.OK, formContent));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
        public void Put_Succes()
        {
            FormContentBindingModel formContentBindingModel = new FormContentBindingModel();

            formContentBindingModel.FormId  = GetFormulierenTemplateId();
            formContentBindingModel.Content = "[{'Naam':'testnaam'},{'Leeftijd':'22'},{'Afwijking':'ADHD'}]";

            HttpResponseMessage result = controller.Put(FormContentId, formContentBindingModel);
            var resultContent          = result.Content.ReadAsAsync <dynamic>().Result;

            // Assert
            Assert.AreEqual(result.StatusCode, HttpStatusCode.OK);
            Assert.IsNotNull(resultContent);
        }
Exemplo n.º 3
0
        public void Post_Fail_BracketMissing()
        {
            FormContentBindingModel formContentBindingModel = new FormContentBindingModel
            {
                Content = "'Naam':'testnaam'},{'Leeftijd':'22'},{'Afwijking':'ADHD'}]",
                FormId  = GetFormulierenTemplateId()
            };

            // Act
            HttpResponseMessage result = controller.Post(formContentBindingModel);
            var resultContent          = result.Content.ReadAsAsync <System.Web.Http.HttpError>().Result;

            // Assert
            Assert.AreEqual(result.StatusCode, HttpStatusCode.BadRequest);
            Assert.AreEqual(resultContent.Message, "JSON in 'content' is not correct JSON: JSON doesn't start or and with with '{/}' or '[/]' ");
        }
Exemplo n.º 4
0
        public void Post_Fail_JSON()
        {
            FormContentBindingModel formContentBindingModel = new FormContentBindingModel
            {
                Content = "[{Naam':'testnaam'},{'Leeftijd':'22'},{'Afwijking':'ADHD'}]",
                FormId  = GetFormulierenTemplateId()
            };

            // Act
            HttpResponseMessage result = controller.Post(formContentBindingModel);
            var resultContent          = result.Content.ReadAsAsync <System.Web.Http.HttpError>().Result;

            // Assert
            Assert.AreEqual(result.StatusCode, HttpStatusCode.BadRequest);
            Assert.AreEqual(resultContent.Message, "JSON in 'content' is not correct JSON: Invalid JavaScript property identifier character: '. Path '[0]', line 1, position 6.");
        }
Exemplo n.º 5
0
        // PUT FormContent by Id
        public HttpResponseMessage Put(int id, [FromBody] FormContentBindingModel UpdateObject)
        {
            var formContent = db.FormContent.Where(f => f.Id == id).FirstOrDefault();

            if (formContent == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "No FormContent found with id: " + id.ToString()));
            }
            else
            {
                formContent.FormulierenId = UpdateObject.FormId;

                IsJSON isJson = IsValidJson(UpdateObject.Content); // Check of JSON klopt en maak resultaat object
                if (!isJson.Status)                                // als resultaat object status fals is return error
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "JSON in 'content' is not correct JSON: " + isJson.Error));
                }


                ContentEqeulsHeadersCheck headersCheck = ContentEqeulsHeaders(UpdateObject);
                if (!headersCheck.Status)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, headersCheck.Error));
                }

                formContent.Content = UpdateObject.Content;
                try
                {
                    db.SaveChanges();
                    return(Request.CreateResponse(HttpStatusCode.OK, formContent));
                }
                catch (Exception ex)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
                }
            }
        }
Exemplo n.º 6
0
        private ContentEqeulsHeadersCheck ContentEqeulsHeaders(FormContentBindingModel formContentBindingModel)
        {
            ContentEqeulsHeadersCheck result = new ContentEqeulsHeadersCheck();
            var Formulier = db.Formulieren.Where(f => f.Id == formContentBindingModel.FormId).FirstOrDefault(); //Haalt bijbehorende formulier op

            var obj = JToken.Parse(formContentBindingModel.Content);                                            //Maak object van mee gegeven content

            foreach (var item in obj)                                                                           //loop door mee gegeven content
            {
                //Pak de propery naam
                string jsonName     = item.First.Path.ToString();
                var    splitPath    = jsonName.Split('.');
                string propertyName = splitPath[1];

                if (!Formulier.FormTemplate.Contains(propertyName))
                {
                    result.Status = false;
                    result.Error  = "'" + propertyName + "'" + " is not found in the headers of the matching Formulier";
                    return(result);
                }
            }
            result.Status = true;
            return(result);
        }