public async Task <IHttpActionResult> GetSafetyInstruction(int id, string property) { WebApiResponseList <dynamic> response = new WebApiResponseList <dynamic>(); try { SafetyInstruction safetyInstruction = await db.SafetyInstructions.Include(property).FirstOrDefaultAsync(d => d.ID == id); if (safetyInstruction == null) { return(NotFound()); } response.RequestUrl = Request.RequestUri.ToString(); response.Version = WebApi.Version; response.Exception = null; response.StatusCode = "200"; response.List = safetyInstruction.GetType().GetProperty(property).GetValue(safetyInstruction); if (response.List == null) { return(NotFound()); } } catch (Exception e) { response.Exception = e; response.StatusCode = "500"; } return(Ok(response)); }
public async Task <IHttpActionResult> PostSafetyInstruction(SafetyInstructionViewModel safetyInstructionView) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } SafetyInstruction safetyInstruction = new SafetyInstruction(); safetyInstruction.Description = safetyInstructionView.Description; //ContentType contentType = await db.ContentTypes.FirstOrDefaultAsync(x => x.SafetyInstruction.Id == safetyInstruction.Id); ContentType contentType = new ContentType(); contentType.Type = safetyInstructionView.Type; contentType.Extension = safetyInstructionView.Attachment.Substring(safetyInstructionView.Attachment.LastIndexOf(".")); Attachment attachment = new Attachment(); attachment.Path = safetyInstructionView.Attachment; attachment.Content = contentType; attachment.FileName = safetyInstructionView.Attachment.Substring(safetyInstructionView.Attachment.LastIndexOf("\\") + 1); safetyInstruction.Attachment = attachment; db.SafetyInstructions.Add(safetyInstruction); await db.SaveChangesAsync(); return(CreatedAtRoute("DefaultApi", new { id = safetyInstruction.ID }, safetyInstruction)); }
public async Task <IHttpActionResult> PutSafetyInstruction(int id, SafetyInstruction safetyInstruction) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != safetyInstruction.ID) { return(BadRequest()); } db.Entry(safetyInstruction).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SafetyInstructionExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IHttpActionResult> DeleteSafetyInstruction(int id) { SafetyInstruction safetyInstruction = await db.SafetyInstructions.FindAsync(id); if (safetyInstruction == null) { return(NotFound()); } db.SafetyInstructions.Remove(safetyInstruction); await db.SaveChangesAsync(); return(Ok(safetyInstruction)); }