//Enter Feedback public bool createFeedbackAnswer(FeedbackAnswer feedModel) { bool result = false; float fValue = 0; using (myProjectEntities context = new myProjectEntities()) { Mapper.CreateMap <FeedbackAnswer, tblFeedbackResult>(); tblFeedbackResult newFeed = Mapper.Map <FeedbackAnswer, tblFeedbackResult>(feedModel); context.tblFeedbackResults.Add(newFeed); context.SaveChanges(); var valueFeed = context.tblFeedbackResults.Where(q => q.FK_Feedback_id == feedModel.FK_Feedback_id).Select(q => q.feedback_answer).AsEnumerable(); float d = float.Parse(valueFeed.Aggregate((a, b) => a + b).ToString()); int count = valueFeed.Count(); fValue = d / count; tblFeedback f = context.tblFeedbacks.Where(q => q.PK_Feedback_id == feedModel.FK_Feedback_id).FirstOrDefault(); f.feedback_value = fValue; context.Entry(f).State = EntityState.Modified; context.SaveChanges(); } return(result); }
public bool createFeedAnswer(int studentId, int lectureId, float answer) { bool result = false; float fValue = 0; using (myProjectEntities context = new myProjectEntities()) { int q3 = 0; var feedId = context.tblFeedbacks.Where(q => q.FK_Lecture_id == lectureId).FirstOrDefault(); FeedbackAnswer feedModel = new FeedbackAnswer(); feedModel.feedback_answer = answer; feedModel.feedback_date = System.DateTime.Now; feedModel.FK_Feedback_id = Convert.ToInt32(feedId.PK_Feedback_id); feedModel.FK_Student_id = studentId; Mapper.CreateMap <FeedbackAnswer, tblFeedbackResult>(); tblFeedbackResult newFeed = Mapper.Map <FeedbackAnswer, tblFeedbackResult>(feedModel); context.tblFeedbackResults.Add(newFeed); q3 = context.SaveChanges(); //Now update feedback value in tblFeedback //This is because after every submission there is change in the overall feedback value if (q3 != 0) { var valueFeed = context.tblFeedbackResults.Where(q => q.FK_Feedback_id == feedModel.FK_Feedback_id).Select(q => q.feedback_answer).AsEnumerable(); float d = float.Parse(valueFeed.Aggregate((a, b) => a + b).ToString()); int count = valueFeed.Count(); fValue = d / count; tblFeedback f = context.tblFeedbacks.Where(q => q.PK_Feedback_id == feedModel.FK_Feedback_id).FirstOrDefault(); f.feedback_value = fValue; context.Entry(f).State = EntityState.Modified; q3 = context.SaveChanges(); if (q3 != 0) { result = true; } } } return(result); }