// PUT api/Question/5 public IHttpActionResult PutQuestion(int id, Question question) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != question.Id) { return BadRequest(); } db.Entry(question).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!QuestionExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public IHttpActionResult PostQuestion(Question question) { if (!ModelState.IsValid) { return BadRequest(ModelState); } //checking if the owner of the questions is provided if (question.Owner == "" || question.Owner == null) question.Owner = User.Identity.Name; //setting the paper field to the owners name if empty if (question.Paper == null || question.Paper == "") question.Paper = question.Owner; db.Question.Add(question); try { db.SaveChanges(); } catch (DbUpdateException) { if (QuestionExists(question.Id)) { return Conflict(); } else if(db.Question.Where(x => x.Owner == question.Owner && x.Paper == question.Paper && x.QuestionName == question.QuestionName) != null) { return BadRequest("Conflict: Paper and Question Id Must be unique"); } } var baseUri = "api/q"; if (question.Paper == question.Owner) return Created(baseUri + "/" + question.Owner + "/" + question.QuestionName, question); //CreatedAtRoute("DefaultApi", new { controller = "GetQuestion", owner = question.Owner, questionName = question.QuestionName }, question); else return Created(baseUri + "/" + question.Owner + "/" + question.Paper + "/" + question.QuestionName, question); //CreatedAtRoute("DefaultApi", new { controller = "GetQuestion", owner = question.Owner, paper = question.Paper, questionName = question.QuestionName }, question); }
public Task<HttpResponseMessage> PostQuestion(Question question) { string uri = baseUri + "Question"; HttpClient httpClient = new HttpClient(); string json = Newtonsoft.Json.JsonConvert.SerializeObject(question); HttpContent content = new StringContent(json); content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var response = httpClient.PostAsync(uri,content); return response; }
public async Task<ActionResult> PostQuestion(Question question) { HttpResponseMessage response = await service.PostQuestion(question); return RedirectToAction("GetQuestion", new { id = question.Id }); }