public IHttpActionResult Post([FromBody] CreateTOTReportModel model) { if (ModelState.IsValid) { var report = new TOTReport { Module = model.Module, StartDate = model.StartDate.Value, EndDate = model.EndDate.Value, Venue = model.Venue, Organization = model.Organization, AgeR1NoOfMale = model.AgeR1NoOfMale, AgeR1NoOfFemale = model.AgeR1NoOfFemale, AgeR2NoOfMale = model.AgeR2NoOfMale, AgeR2NoOfFemale = model.AgeR2NoOfFemale, AgeR3NoOfMale = model.AgeR3NoOfMale, AgeR3NoOfFemale = model.AgeR3NoOfFemale, AgeR4NoOfMale = model.AgeR4NoOfMale, AgeR4NoOfFemale = model.AgeR4NoOfFemale, AgeR5NoOfMale = model.AgeR5NoOfMale, AgeR5NoOfFemale = model.AgeR5NoOfFemale, SalaryR1NoOfMale = model.SalaryR1NoOfMale, SalaryR1NoOfFemale = model.SalaryR1NoOfFemale, SalaryR2NoOfMale = model.SalaryR2NoOfMale, SalaryR2NoOfFemale = model.SalaryR2NoOfFemale, SalaryR3NoOfMale = model.SalaryR3NoOfMale, SalaryR3NoOfFemale = model.SalaryR3NoOfFemale, SalaryR4NoOfMale = model.SalaryR4NoOfMale, SalaryR4NoOfFemale = model.SalaryR4NoOfFemale, SalaryR5NoOfMale = model.SalaryR5NoOfMale, SalaryR5NoOfFemale = model.SalaryR5NoOfFemale, SalaryR6NoOfMale = model.SalaryR6NoOfMale, SalaryR6NoOfFemale = model.SalaryR6NoOfFemale, CreatedBy = model.CreatedBy, CreatedDate = DateTime.Now }; db.TOTReport.Add(report); foreach (var fileid in model.FilesId) { var reportfile = new TOTReportFile { FileId = fileid, Report = report }; db.TOTReportFile.Add(reportfile); } db.SaveChanges(); return(Ok(report)); } return(BadRequest(ModelState)); }
public IHttpActionResult Put(int id, [FromBody] EditTOTReportModel model) { if (ModelState.IsValid) { var report = db.TOTReport.Where(u => u.Id == id).FirstOrDefault(); if (report == null) { return(NotFound()); } report.Module = model.Module; report.StartDate = model.StartDate; report.EndDate = model.EndDate; report.Venue = model.Venue; report.Organization = model.Organization; report.AgeR1NoOfMale = model.AgeR1NoOfMale; report.AgeR1NoOfFemale = model.AgeR1NoOfFemale; report.AgeR2NoOfMale = model.AgeR2NoOfMale; report.AgeR2NoOfFemale = model.AgeR2NoOfFemale; report.AgeR3NoOfMale = model.AgeR3NoOfMale; report.AgeR3NoOfFemale = model.AgeR3NoOfFemale; report.AgeR4NoOfMale = model.AgeR4NoOfMale; report.AgeR4NoOfFemale = model.AgeR4NoOfFemale; report.AgeR5NoOfMale = model.AgeR5NoOfMale; report.AgeR5NoOfFemale = model.AgeR5NoOfFemale; report.SalaryR1NoOfMale = model.SalaryR1NoOfMale; report.SalaryR1NoOfFemale = model.SalaryR1NoOfFemale; report.SalaryR2NoOfMale = model.SalaryR2NoOfMale; report.SalaryR2NoOfFemale = model.SalaryR2NoOfFemale; report.SalaryR3NoOfMale = model.SalaryR3NoOfMale; report.SalaryR3NoOfFemale = model.SalaryR3NoOfFemale; report.SalaryR4NoOfMale = model.SalaryR4NoOfMale; report.SalaryR4NoOfFemale = model.SalaryR4NoOfFemale; report.SalaryR5NoOfMale = model.SalaryR5NoOfMale; report.SalaryR5NoOfFemale = model.SalaryR5NoOfFemale; report.SalaryR6NoOfMale = model.SalaryR6NoOfMale; report.SalaryR6NoOfFemale = model.SalaryR6NoOfFemale; db.Entry(report).State = EntityState.Modified; //remove file var attachments = db.TOTReportFile.Where(s => s.TOTReportId == model.Id).ToList(); if (attachments != null) { //delete all if (model.Attachments == null) { foreach (var attachment in attachments) { attachment.FileDocument.Display = false; db.FileDocument.Attach(attachment.FileDocument); db.Entry(attachment.FileDocument).Property(m => m.Display).IsModified = true; db.TOTReportFile.Remove(attachment); } } else { foreach (var attachment in attachments) { if (!model.Attachments.Any(u => u.Id == attachment.FileDocument.Id))//delete if not exist anymore { attachment.FileDocument.Display = false; db.FileDocument.Attach(attachment.FileDocument); db.Entry(attachment.FileDocument).Property(m => m.Display).IsModified = true; db.TOTReportFile.Remove(attachment); } } } } //add files foreach (var fileid in model.FilesId) { var reportfile = new TOTReportFile { FileId = fileid, TOTReportId = id }; db.TOTReportFile.Add(reportfile); } db.Configuration.ValidateOnSaveEnabled = true; db.SaveChanges(); return(Ok(true)); } return(BadRequest(ModelState)); }