public void Put_Succes() { FormulierenBindingModel formBindingModel = new FormulierenBindingModel(); FormulierenController controller = GetController(); Random rnd = new Random();//rnd formBindingModel.FormTemplate = "[{'Naam':'string'},{'Leeftijd':'22'},{'" + rnd.Next(1, 100).ToString() + "':'ADHD'}]"; formBindingModel.Region = "test"; formBindingModel.Name = "name"; HttpResponseMessage result = controller.Put(47, formBindingModel); var resultContent = result.Content.ReadAsAsync <dynamic>().Result; Assert.AreEqual(result.StatusCode, HttpStatusCode.OK); formBindingModel.FormTemplate = "[{'Naam':'string'},{'Leeftijd':'22'}]"; formBindingModel.Region = "test"; formBindingModel.Name = "name"; result = controller.Put(47, formBindingModel); resultContent = result.Content.ReadAsAsync <dynamic>().Result; Assert.AreEqual(result.StatusCode, HttpStatusCode.OK); formBindingModel.FormTemplate = "[{'Naam':'string'},{'Leeftijd':'22'},{'Afwijking':'string'}]"; formBindingModel.Region = "test"; formBindingModel.Name = "name"; result = controller.Put(47, formBindingModel); resultContent = result.Content.ReadAsAsync <dynamic>().Result; Assert.AreEqual(result.StatusCode, HttpStatusCode.OK); Assert.IsNotNull(resultContent); }
// POST een Formulier public HttpResponseMessage Post(FormulierenBindingModel formulierenBindingModel) { try { var isJson = IsValidJson(formulierenBindingModel.FormTemplate); // 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)); } var formulier = new Formulieren { Name = formulierenBindingModel.Name, Region = formulierenBindingModel.Region, FormTemplate = formulierenBindingModel.FormTemplate }; db.Formulieren.Add(formulier); db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public void FormPost_Ok() { // Arramge FormulierenController controller = GetController(); FormulierenBindingModel formulierenBindingModel = new FormulierenBindingModel(); formulierenBindingModel.Name = "Testformulier9999"; formulierenBindingModel.Region = "Zuid-Holland"; formulierenBindingModel.FormTemplate = "[{'Naam':'string'},{'Leeftijd':'string'},{'Afwijking':'string'}]"; // Act HttpResponseMessage actionResult = controller.Post(formulierenBindingModel); // Assert Assert.AreEqual(actionResult.StatusCode, HttpStatusCode.OK); }
public void FormPost_BadRequest() { // Arramge // Arramge FormulierenController controller = GetController(); FormulierenBindingModel formulierenBindingModel = new FormulierenBindingModel(); formulierenBindingModel.Name = "Testformulier9999"; formulierenBindingModel.Region = "Zuid-Holland"; formulierenBindingModel.FormTemplate = "{iets:data"; // Act HttpResponseMessage actionResult = controller.Post(formulierenBindingModel); // Assert Assert.AreEqual(actionResult.StatusCode, HttpStatusCode.BadRequest); }
// PUT api/values/5 public HttpResponseMessage Put(int id, [FromBody] FormulierenBindingModel UpdateObject) { try { var form = db.Formulieren.Where(f => f.Id == id).FirstOrDefault(); if (form == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "No form found with id: " + id.ToString())); } if (form.FormTemplate == UpdateObject.FormTemplate) //check if template is changed { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The template hasn't been changed. Please submit a changed template.")); } IsJSON isJson = IsValidJson(UpdateObject.FormTemplate); if (!isJson.Status) // Check if new formTemplate is correct JSON { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "JSON in 'template' is not correct JSON: " + isJson.Error)); } form.Name = UpdateObject.Name; form.Region = UpdateObject.Region; form.FormTemplate = UpdateObject.FormTemplate; var formContentList = db.FormContent.Where(s => s.FormulierenId == id).ToList(); //get all the formContents related to the form if (formContentList.Count == 0) { db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, form)); } List <JArray> formContentArray = new List <JArray>(); foreach (var formContent in formContentList) { formContentArray.Add(JArray.Parse(formContent.Content)); //parse db data to JSON list } var formTemplate = JArray.Parse(form.FormTemplate); //parse new template to JSON if (formTemplate.Count - formContentArray.FirstOrDefault().Count > 1 || formTemplate.Count - formContentArray.FirstOrDefault().Count < -1) //check if only 1 key is edited { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Only 1 key can be added/removed/edited at a time")); } UpdateFormContent(formContentArray, formTemplate); foreach (var content in formContentList) { foreach (var newContent in formContentArray) { content.Content = newContent.ToString(); db.Entry(content).State = EntityState.Modified; db.SaveChanges(); } } return(Request.CreateResponse(HttpStatusCode.OK, form)); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));; } }
// POST api/values public void Post(FormulierenBindingModel formContentBindingModel) { }