示例#1
0
        public async Task <IActionResult> Create([FromBody] InquirerDLL.Entities.Survey model)
        {
            var survey = new Survey
            {
                CreatorId = (int)model.CreatorId,
                Title     = "Шаблон опитування",
                StartDate = DateTime.Now,
                EndDate   = DateTime.Now.AddDays(7)
            };

            Database.Surveys.Add(survey);

            await Database.SaveChangesAsync();

            return(Result(survey.Id));
        }
示例#2
0
        public async Task <IActionResult> Update([FromBody] InquirerDLL.Entities.Survey model, int id)
        {
            var survey = Database.Surveys
                         .Where(s => s.Id == id)
                         .FirstOrDefault();

            survey.Title       = model.Title;
            survey.Description = model.Description;
            survey.StartDate   = model.StartDate;
            survey.EndDate     = model.EndDate;
            survey.IsOpen      = (bool)model.IsOpen;
            survey.IsAuthenticationRequired = (bool)model.IsAuthenticationRequired;

            await Database.SaveChangesAsync();

            var newQuestions = new List <Question>();
            var newOptions   = new List <Option>();

            if (model.Questions.Count() > 0)
            {
                int questionIndex = 0;
                foreach (var i in model.Questions)
                {
                    Question question = null;
                    if (i.Id == null)
                    {
                        question = new Question
                        {
                            Title      = i.Title,
                            TypeId     = (int)i.TypeId,
                            SurveyId   = survey.Id,
                            Image      = i.Image,
                            Index      = questionIndex,
                            IsRequired = (bool)i.IsRequired
                        };
                        Database.Questions.Add(question);
                    }
                    else
                    {
                        question = Database.Questions
                                   .Where(q => q.Id == i.Id)
                                   .FirstOrDefault();

                        question.Title      = i.Title;
                        question.TypeId     = (int)i.TypeId;
                        question.Image      = i.Image;
                        question.Index      = questionIndex;
                        question.IsRequired = (bool)i.IsRequired;
                    }
                    questionIndex++;

                    newQuestions.Add(question);
                }

                await Database.SaveChangesAsync();

                int index = 0;
                foreach (var i in model.Questions)
                {
                    if (i.Options.Count() > 0)
                    {
                        int optionIndex = 0;
                        foreach (var i2 in i.Options)
                        {
                            Option option = null;
                            if (i2.Id == null)
                            {
                                option = new Option
                                {
                                    Label      = i2.Label,
                                    Value      = i2.Value,
                                    Image      = i2.Image,
                                    IsCustom   = (bool)i2.IsCustom,
                                    QuestionId = newQuestions[index].Id,
                                    Index      = optionIndex
                                };
                                Database.Options.Add(option);
                            }
                            else
                            {
                                option = Database.Options
                                         .Where(o => o.Id == i2.Id)
                                         .FirstOrDefault();

                                option.Label    = i2.Label;
                                option.Value    = i2.Value;
                                option.Image    = i2.Image;
                                option.IsCustom = (bool)i2.IsCustom;
                                option.Index    = optionIndex;
                            }
                            optionIndex++;

                            newOptions.Add(option);
                        }
                    }
                    index++;
                }

                await Database.SaveChangesAsync();
            }

            var oldQuestions = Database.Questions
                               .Where(o => o.SurveyId == survey.Id);

            oldQuestions = oldQuestions.Except(newQuestions);
            Database.Questions.RemoveRange(oldQuestions);

            var oldOptions = Database.Options
                             .Include(o => o.Question)
                             .Where(o => o.Question.SurveyId == survey.Id);

            oldOptions = oldOptions.Except(newOptions);
            Database.Options.RemoveRange(oldOptions);

            await Database.SaveChangesAsync();

            return(Status(true));
        }