コード例 #1
0
 public IActionResult CopyQuestions([FromBody] CopyQuestions data)
 {
     try
     {
         var userData = jwtService.ParseData(this.User);
         questionService.CopyQuestions(data, userData.UserId);
         return(Ok());
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
コード例 #2
0
        public void CopyQuestions(CopyQuestions data, int userId)
        {
            var user = this.context.Users.SingleOrDefault(x => x.Id == userId);

            if (user == null)
            {
                throw new ServiceException(Constants.UserNotFoundMessage);
            }

            var targetDirectory = this.context.QuestionSheets
                                  .SingleOrDefault(x => x.Id == data.SelectedDir && x.IsGlobal == false);

            if (targetDirectory == null)
            {
                throw new ServiceException("Target Directory Does Not Exist!");
            }

            if (targetDirectory.UserId != user.Id)
            {
                throw new ServiceException("Target Directory Does Not Belong To You!");
            }

            var globalQuestions = this.context.GlobalQuestionPackages
                                  .Where(x => data.SelectedQuestions.Contains(x.Id))
                                  .OrderBy(x => x.Column)
                                  .ThenBy(x => x.Order)
                                  .ToArray();

            var personalQuestions = new List <PersonalQuestionPackage>();

            for (int i = 0; i < globalQuestions.Length; i++)
            {
                var globalQuestion = globalQuestions[i];
                personalQuestions.Add(new PersonalQuestionPackage
                {
                    Name               = globalQuestion.Name,
                    Question           = globalQuestion.Question,
                    Answer             = globalQuestion.Answer,
                    Comment            = globalQuestion.Comment,
                    Difficulty         = globalQuestion.Difficulty,
                    AnswerRate         = 0,
                    TimesBeingAnswered = 0,
                    YourBestAnswer     = "This is the first time you are answering this question",
                    QuestionSheetId    = targetDirectory.Id,
                    DerivedFromId      = globalQuestion.Id,
                });
            }

            this.context.PersonalQuestionPackages.AddRange(personalQuestions);
            context.SaveChanges();
        }