public bool AddNoteToQuestion(string inspectionUUID, string noteUUID, int questionId, int noteType, string content, string name, int orderNumber, int userId, DateTime modifiedTime) { //find inspection id by uuid int inspectionId = context.Inspections.Where(i => i.UUID == inspectionUUID).Select(i => i.Id).FirstOrDefault(); //finde answerId by InspectionId and questionId int answerId = context.Answers.Where(a => a.InspectionId == inspectionId && a.QuestionId == questionId).Select(a => a.Id).FirstOrDefault(); //create Answer with inspectionId and questionId is not exist if (answerId == 0) { //create answer CSInspectionDatabaseModel.Answer answer = new CSInspectionDatabaseModel.Answer(); answer.QuestionId = questionId; answer.AnsweredBy = userId; answer.InspectionId = inspectionId; answer.CreatedTime = modifiedTime; answer.ModifiedTime = modifiedTime; answer.UpdatedTime = DateTime.Now; context.Answers.Add(answer); context.SaveChanges(); answerId = context.Answers.Where(a => a.InspectionId == inspectionId && a.QuestionId == questionId).Select(a => a.Id).FirstOrDefault(); } //create Note CSInspectionDatabaseModel.Note note = new CSInspectionDatabaseModel.Note(); note.UUID = noteUUID; note.Type = noteType; note.Content = content; note.OrderNumber = orderNumber; note.CreatedBy = userId; note.ModifiedBy = userId; note.CreatedTime = modifiedTime; note.ModifiedTime = modifiedTime; note.UpdatedTime = DateTime.Now; note.Name = name; context.Notes.Add(note); context.SaveChanges(); int noteId = context.Notes.Where(n => n.UUID == noteUUID).Select(n => n.Id).FirstOrDefault(); //connect AnswerNote CSInspectionDatabaseModel.AnswerNote answerNote = new AnswerNote(); answerNote.AnswerId = answerId; answerNote.NoteId = noteId; context.AnswerNotes.Add(answerNote); context.SaveChanges(); bool isSuccess = context.AnswerNotes.Any(an => an.AnswerId == answerId && an.NoteId == noteId); return isSuccess; }
public bool SetQuestionPositiveNegativePercent(string inspectionUUID, int questionId, int positivePercent, int negativePercent, int userId, DateTime modifiedTime) { bool isSuccess = false; //find inspection id by uuid int inspectionId = context.Inspections.Where(i => i.UUID == inspectionUUID).Select(i => i.Id).FirstOrDefault(); //get answer by inspection id and questionId CSInspectionDatabaseModel.Answer answer = context.Answers.Where(a => a.QuestionId == questionId && a.InspectionId == inspectionId).Select(a => a).FirstOrDefault(); //if not exist - create answer if(answer == null) { //create answer answer = new CSInspectionDatabaseModel.Answer(); answer.QuestionId = questionId; answer.AnsweredBy = userId; answer.PositivePercent = positivePercent; answer.NegativePercent = negativePercent; answer.InspectionId = inspectionId; answer.CreatedTime = modifiedTime; context.Answers.Add(answer); context.SaveChanges(); } else { answer.AnsweredBy = userId; answer.PositivePercent = positivePercent; answer.NegativePercent = negativePercent; context.SaveChanges(); } //check if success isSuccess = context.Answers.Where(a => a.Id == answer.Id).Select(a => a.PositivePercent).FirstOrDefault() == positivePercent ? true : false; isSuccess = context.Answers.Where(a => a.Id == answer.Id).Select(a => a.NegativePercent).FirstOrDefault() == negativePercent ? true : false; return isSuccess; }