Exemple #1
0
        public bool DeleteQuestionOption(int QuestionId, int OptionId)
        {
            tblQuestionOption Option = new tblQuestionOption();

            Option.QuestionId           = QuestionId;
            Option.OptionId             = OptionId;
            context.Entry(Option).State = EntityState.Deleted;
            int result = context.SaveChanges();

            if (context.tblCorrectOptions.Any(x => x.QuestionId == QuestionId && x.CorrectOptionId == OptionId))
            {
                tblCorrectOption CorrectOption = new tblCorrectOption();
                CorrectOption.CorrectOptionId      = OptionId;
                CorrectOption.QuestionId           = QuestionId;
                context.Entry(CorrectOption).State = EntityState.Deleted;
                context.SaveChanges();
            }
            return(result > 0 ? true : false);
        }
Exemple #2
0
        public List <QuestionOptions> SaveQuestionOptions(List <QuestionOptions> Options)
        {
            try
            {
                List <tblQuestionOption> QuesOptions = new List <tblQuestionOption>();
                for (int i = 0; i < Options.Count; i++)
                {
                    tblQuestionOption option = new tblQuestionOption();
                    if (Options[i].Id == 0)
                    {
                        option.QuestionId  = Options[i].QuestionID;
                        option.OptionText  = Options[i].OptionText;
                        option.OptionOrder = Options[i].OptionOrder;
                        option.IsActive    = Options[i].IsActive;
                        option.CreatedBy   = Options[i].CreatedBy;
                        option.CreatedAt   = DateTime.Now;
                        option.UpdatedAt   = DateTime.Now;
                        option.UpdatedBy   = Options[i].CreatedBy;
                        QuesOptions.Add(option);
                        context.tblQuestionOptions.Add(option);
                        context.SaveChanges();
                    }
                    else
                    {
                        int OptionID = Options[i].Id;
                        option                      = context.tblQuestionOptions.Where(x => x.OptionId == OptionID).FirstOrDefault();
                        option.QuestionId           = Options[i].QuestionID;
                        option.OptionText           = Options[i].OptionText;
                        option.OptionOrder          = Options[i].OptionOrder;
                        option.IsActive             = Options[i].IsActive;
                        option.OptionId             = Options[i].Id;
                        context.Entry(option).State = EntityState.Modified;
                        context.SaveChanges();
                    }
                    if (Options[i].CorrectOption)
                    {
                        int OptionID = Options[i].Id;
                        tblCorrectOption correctOption = context.tblCorrectOptions.Where(q => q.CorrectOptionId == OptionID).FirstOrDefault();
                        if (correctOption == null)
                        {
                            correctOption = new tblCorrectOption();
                            correctOption.CorrectOptionId = option.OptionId;
                            correctOption.QuestionId      = option.QuestionId;

                            context.tblCorrectOptions.Add(correctOption);
                            context.SaveChanges();
                        }
                    }
                    else
                    {
                        int OptionID = Options[i].Id;
                        tblCorrectOption CorrectOption = context.tblCorrectOptions.Where(q => q.CorrectOptionId == OptionID).FirstOrDefault();
                        if (CorrectOption != null)
                        {
                            context.Entry(CorrectOption).State = EntityState.Deleted;
                            context.SaveChanges();
                        }
                    }
                }

                int questionID = Options[0].QuestionID;
                var list       = context.tblQuestionOptions.Where(q => q.QuestionId == questionID).Select(x => new QuestionOptions()
                {
                    Id          = x.OptionId,
                    OptionText  = x.OptionText,
                    OptionOrder = x.OptionOrder.Value,
                    QuestionID  = x.QuestionId,
                    IsActive    = x.IsActive.Value,
                    UpdatedAt   = x.UpdatedAt.Value,
                    CreatedAt   = x.CreatedAt.Value
                }).ToList();

                return(list);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }