public async Task <IHttpActionResult> GetSafetyQuestion(int id, string property) { WebApiResponseList <dynamic> response = new WebApiResponseList <dynamic>(); try { SafetyQuestion safetyQuestion = await db.SafetyQuestions.Include(property).FirstOrDefaultAsync(d => d.ID == id); if (safetyQuestion == null) { return(NotFound()); } response.RequestUrl = Request.RequestUri.ToString(); response.Version = WebApi.Version; response.Exception = null; response.StatusCode = "200"; response.List = safetyQuestion.GetType().GetProperty(property).GetValue(safetyQuestion, null);; if (response.List == null) { return(NotFound()); } } catch (Exception e) { response.Exception = e; response.StatusCode = "500"; } return(Ok(response)); }
public async Task <IHttpActionResult> PutSafetyQuestion(int id, SafetyQuestion safetyQuestion) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != safetyQuestion.ID) { return(BadRequest()); } db.Entry(safetyQuestion).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SafetyQuestionExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IHttpActionResult> DeleteSafetyQuestion(int id) { SafetyQuestion safetyQuestion = await db.SafetyQuestions.FindAsync(id); if (safetyQuestion == null) { return(NotFound()); } db.SafetyQuestions.Remove(safetyQuestion); await db.SaveChangesAsync(); return(Ok(safetyQuestion)); }
public async Task <IHttpActionResult> PostSafetyQuestion(SafetyQuestionViewModel safetyQuestionView) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } SafetyQuestion safetyQuestion = new SafetyQuestion(); safetyQuestion.Question = safetyQuestionView.Question; List <Option> Options = new List <Option>(); foreach (String option in safetyQuestionView.Options) { Options.Add(new Option(option)); } safetyQuestion.Options = Options; safetyQuestion.Answer = safetyQuestionView.AnswerId; db.SafetyQuestions.Add(safetyQuestion); await db.SaveChangesAsync(); return(CreatedAtRoute("DefaultApi", new { id = safetyQuestion.ID }, safetyQuestion)); }