public bool AddOrUpdateNotices(DbEntities.Notices.Notice notice) { try { var noti = Context.Notice.Find(notice.Id); if (noti == null) { Context.Notice.Add(notice); Context.SaveChanges(); return(true); } noti.Content = notice.Content; noti.PublishNoticeToNoticeBoard = notice.PublishNoticeToNoticeBoard; noti.UpdatedDate = DateTime.Now; noti.Title = notice.Title; noti.NoticePublishTo = notice.NoticePublishTo; noti.PublishedDate = notice.PublishedDate; noti.UpdatedBy = notice.UpdatedBy; noti.UpdatedDate = notice.UpdatedDate; noti.Void = notice.Void; Context.SaveChanges(); return(true); } catch (Exception) { return(false); } }
public DbEntities.Exams.Exam AddOrUpdateExam(DbEntities.Exams.Exam exam , int userId, List <ExamOfClass> eocList, string titleOfNotice) { try { using (var scope = new TransactionScope()) { var model = Context.Exam.Find(exam.Id); if (model == null) { model = Context.Exam.Add(exam); Context.SaveChanges(); } else { model.Name = exam.Name; model.NoticeContent = exam.NoticeContent; model.ResultDate = exam.ResultDate; model.StartDate = exam.StartDate; model.Weight = exam.Weight; model.ExamTypeId = exam.ExamTypeId; model.UpdatedDate = DateTime.Now; model.ExamCoordinatorId = exam.ExamCoordinatorId; model.PublishNoticeToNoticeBoard = exam.PublishNoticeToNoticeBoard; Context.SaveChanges(); } //publishing notice to noticeBoard var notice = Context.Notice.Find(model.NoticeId ?? 0); if (notice == null) { var n = new DbEntities.Notices.Notice() { PublishNoticeToNoticeBoard = model.PublishNoticeToNoticeBoard ?? false , Content = model.NoticeContent , CreatedBy = userId , CreatedDate = DateTime.Now , NoticePublishTo = true , SchoolId = model.SchoolId }; if (model.PublishNoticeToNoticeBoard ?? false) { n.PublishedDate = DateTime.Now; n.Title = titleOfNotice; } else { n.Title = model.ExamTypeId == null ? model.Name : model.ExamType.Name; } var savedNotice = Context.Notice.Add(n); Context.SaveChanges(); model.NoticeId = savedNotice.Id; Context.SaveChanges(); } else { if (model.PublishNoticeToNoticeBoard ?? false) { notice.Title = titleOfNotice; if (!notice.PublishNoticeToNoticeBoard) { notice.PublishedDate = DateTime.Now; } } else { notice.Title = model.ExamTypeId == null ? model.Name : model.ExamType.Name; } notice.PublishNoticeToNoticeBoard = model.PublishNoticeToNoticeBoard ?? false; notice.Content = model.NoticeContent; notice.UpdatedBy = userId; notice.UpdatedDate = DateTime.Now; notice.NoticePublishTo = true; Context.SaveChanges(); } if (eocList != null)//&& userId != null { eocList.ForEach(x => { x.ExamId = model.Id; var eoc = Context.ExamOfClass.Find(x.Id); if (eoc == null) { eoc = x; // // eoc.SettingsUsedFromExam = true; eoc.CreatedBy = userId; eoc.CreatedDate = DateTime.Now; var addedClass = Context.ExamOfClass.Add(eoc); Context.SaveChanges(); //now add all the subjects //needed only here //while updating the subjects need not be changed var subjects = Context.SubjectClass.Where(sc => sc.RunningClassId == eoc.RunningClassId && sc.IsRegular && !(sc.Void ?? false)); foreach (var sc in subjects) { Context.ExamSubject.Add(new ExamSubject() { ExamOfClassId = addedClass.Id , SubjectClassId = sc.Id , SettingsUsedFromExam = true , }); Context.SaveChanges(); } } else { eoc.Void = x.Void; eoc.UpdatedBy = x.UpdatedBy; eoc.UpdatedDate = x.UpdatedDate; //eoc.Weight = x.Weight; //eoc.SettingsUsedFromExam = x.SettingsUsedFromExam; //eoc.PassMarks = x.PassMarks; //eoc.IsPercent = x.IsPercent; //eoc.FullMarks = x.FullMarks; Context.SaveChanges(); } }); } scope.Complete(); return(model); } } catch (Exception ex) { return(null); } }