Task <RigOapChecklist> IRequestHandler <UpdateAuditProtocolRequest, RigOapChecklist> .Handle(UpdateAuditProtocolRequest request, CancellationToken cancellationToken) { var existingCheckList = RigOapChecklistService.GetCompleteChecklist(request.Protocol.Id); if (existingCheckList == null) { throw new ApplicationException($"UpdateRigOapChecklistHandler: RigChecklist with Id {request.Protocol.Id} does not exist."); } //AutoMapper to Map the fields Mapper.Map(request.Protocol, existingCheckList); //To handle the case when update is done in the DashBoard if (request.Protocol.Questions != null) { Mapper.AddOrUpdate <RigOapChecklistQuestion, Guid>(request.Protocol.Questions, existingCheckList.Questions, (dl, sq) => dl.SingleOrDefault(i => i.OapChecklistQuestionId == sq.OapChecklistQuestionId)); foreach (var sq in request.Protocol.Questions) { var dq = existingCheckList.Questions.SingleOrDefault(q => q.OapChecklistQuestionId == sq.OapChecklistQuestionId); if (dq != null) { Mapper.AddOrUpdate <RigOapChecklistQuestionAnswer, Guid>(sq.Answers, dq.Answers, (dl, sqa) => dl.SingleOrDefault(i => i.RigOapChecklistQuestionId == sqa.RigOapChecklistQuestionId && i.Ordinal == sqa.Ordinal)); } } } //To handle the case when update is done in the DashBoard if (request.Protocol.Comments != null) { Mapper.AddOrUpdate <RigOapChecklistComment, Guid>(request.Protocol.Comments, existingCheckList.Comments, (dl, sq) => dl.SingleOrDefault(i => i.OapChecklistCommentId == sq.OapChecklistCommentId)); } var rigOapChecklist = request.Protocol; using (var ts = new TransactionScope()) { RigOapChecklistService.Update(existingCheckList); ts.Complete(); } return(Task.FromResult(RigOapChecklistService.GetCompleteChecklist(existingCheckList.Id))); }
Task <RigOapChecklist> IRequestHandler <UpdateRigOapChecklistRequest, RigOapChecklist> .Handle(UpdateRigOapChecklistRequest request, CancellationToken cancellationToken) { var existingCheckList = RigOapChecklistService.GetCompleteChecklist(request.Checklist.Id); if (existingCheckList == null) { throw new ApplicationException($"UpdateRigOapChecklistHandler: RigChecklist with Id {request.Checklist.Id} does not exist."); } //AutoMapper to Map the fields Mapper.Map(request.Checklist, existingCheckList); //To handle the case when update is done in the DashBoard if (request.Checklist.Questions != null) { Mapper.AddOrUpdate <RigOapChecklistQuestion, Guid>(request.Checklist.Questions, existingCheckList.Questions, (dl, sq) => dl.SingleOrDefault(i => i.OapChecklistQuestionId == sq.OapChecklistQuestionId)); foreach (var sq in request.Checklist.Questions) { var dq = existingCheckList.Questions.SingleOrDefault(q => q.OapChecklistQuestionId == sq.OapChecklistQuestionId); if (dq != null) { Mapper.AddOrUpdate <RigOapChecklistQuestionAnswer, Guid>(sq.Answers, dq.Answers, (dl, sqa) => dl.SingleOrDefault(i => i.RigOapChecklistQuestionId == sqa.RigOapChecklistQuestionId && i.Ordinal == sqa.Ordinal)); //if (sq.NoAnswerComment != null && dq.NoAnswerComment == null) //Add //{ // sq.NoAnswerComment.Id = sq.Id; // var id = ChecklistQuestionNoAnswerCommentService.Add(sq.NoAnswerComment); //} //else if (sq.NoAnswerComment == null && dq.NoAnswerComment != null) //{ // ChecklistQuestionNoAnswerCommentService.Delete(dq.NoAnswerComment); //} //else if (sq.NoAnswerComment != null && dq.NoAnswerComment != null) //{ // //var saveFile = !string.IsNullOrEmpty(sq.NoAnswerComment.FileName) && string.IsNullOrEmpty(dq.NoAnswerComment.FileName); // //if (saveFile) // //{ // // var imageLocation = $"{ConfigurationManager.AppSettings["uploadfilepath"]}\\{sq.NoAnswerComment.FileName}"; // // sq.NoAnswerComment.FileStream?.Save(imageLocation); // //} // Mapper.Map(sq.NoAnswerComment, dq.NoAnswerComment); // ChecklistQuestionNoAnswerCommentService.Update(sq.NoAnswerComment); //} } } } //To handle the case when update is done in the DashBoard if (request.Checklist.Comments != null) { Mapper.AddOrUpdate <RigOapChecklistComment, Guid>(request.Checklist.Comments, existingCheckList.Comments, (dl, sq) => dl.SingleOrDefault(i => i.OapChecklistCommentId == sq.OapChecklistCommentId)); } var rigOapChecklist = request.Checklist; using (var ts = new TransactionScope()) { RigOapChecklistService.Update(existingCheckList); ts.Complete(); } return(Task.FromResult(RigOapChecklistService.GetCompleteChecklist(existingCheckList.Id))); }