internal bool SaveSubjectsForTutor(SubjectsListModel objReq) { using (var db = new WizzDataContext()) { int tutorId = Convert.ToInt32(objReq.userId); // List<tblTutorSubject> objSubject = new List<tblTutorSubject>(); var objSubjectDbList = db.usp_GetSubjectsForTutor(tutorId).ToList(); tblTutorSubject listObj = new tblTutorSubject(); if (objReq.subjectsIdList!=null&&objReq.subjectsIdList.Count != 0) { foreach (var n in objSubjectDbList) { var data = objReq.subjectsIdList.Where(x => x == Convert.ToString(n.subjectId)).FirstOrDefault(); //data is null and n.isadded false h means not in all list wont be added wont be deleted //data is null and n.isadded true h deleted from subject table // if not null then check is added if yes den leave //if not null and is added false insert //if not null and is added true then do nothing if (data == null && n.isApplied == "True") { var tutorSub = db.tblTutorSubjects.Where(x => x.fkSubjectId == n.subjectId&&x.fkTutorId==Convert.ToInt64(objReq.userId)).FirstOrDefault(); db.tblTutorSubjects.DeleteOnSubmit(tutorSub); db.SubmitChanges(); } else if (data != null) { if (n.isApplied == "False") { var tutorSub = new tblTutorSubject(); tutorSub.fkTutorId = Convert.ToInt64(objReq.userId); tutorSub.fkSubjectId = Convert.ToInt64(data); tutorSub.isApproved = false; db.tblTutorSubjects.InsertOnSubmit(tutorSub); db.SubmitChanges(); //insert with is approved false in tutorsubject } } } } return true; } }
public Response<ResponseTutorSubjects> SaveSubjectsForTutor(SubjectsListModel objReq) { Response<ResponseTutorSubjects> response = new Response<ResponseTutorSubjects>(); List<ResponseTutorSubjects> objResp = new List<ResponseTutorSubjects>(); try { Tutor obj = new Tutor(); if (CheckRequestIsvalidornot(this.Request)) { if (ModelState.IsValid) { if (objReq.subjectsIdList.Count == 0) response.Create(false, "Atleast one subject id should be there", Messages.AppVersion, objResp); else if (obj.SaveSubjectsForTutor(objReq)) response.Create(true, Messages.FormatMessage(Messages.Success, "Subjects saved"), Messages.AppVersion, objResp); else response.Create(false, Messages.ErrorOccure, Messages.AppVersion, objResp); } else response.Create(false, ModelState.Values.FirstOrDefault().Errors.FirstOrDefault().ErrorMessage, Messages.AppVersion, objResp); } else { response.Create(false, Messages.FormatMessage(Messages.InvalidReq), Messages.AppVersion, objResp); } } catch (Exception ex) { object session = new JavaScriptSerializer().Serialize(objReq); LogManager.Error("Error occured while Processing Webservice request :{0}", ex, session, ex.Message); response.Create(false, Messages.FormatMessage(Messages.ErrorOccure), Messages.AppVersion, objResp); } return response; }