Beispiel #1
0
        public async Task <IActionResult> AddNewChallenge(IndexChallengeViewModelFromPost inputModel)
        {
            var challenge = new ACMT.ChallengeDetails
            {
                Description          = inputModel.Description,
                Id                   = Guid.NewGuid().ToString(),
                Name                 = inputModel.Name,
                Questions            = new List <ACMQ.QuestionLite>(),
                AzureServiceCategory = inputModel.AzureServiceCategory,
                Duration             = inputModel.Duration,
                WelcomeMessage       = inputModel.WelcomeMessage,
                PrereqLinks          = inputModel.PrereqLinks
            };

            var response = await challengeProvider.AddItemAsync(challenge);

            if (response.Success)
            {
                // Add a new global parameter object for the Challenge
                await challengeParameterProvider.AddItemAsync(new ACMP.GlobalChallengeParameters()
                {
                    ChallengeId = challenge.Id
                });

                return(Ok());
            }

            return(StatusCode(500));
        }
Beispiel #2
0
        public async Task <IActionResult> CopyChallenge(IndexChallengeViewModelFromPost inputModel)
        {
            var newChallengeId = Guid.NewGuid().ToString();

            // Get the copy-from challenge, assigned questions and global parameters
            var challegeOriginalResponse = await challengeProvider.GetItemAsync(inputModel.Id);

            var assignedQuestionOriginalResponse = await assignedQuestionProvider.GetItemsOfChallengeAsync(inputModel.Id);

            var globalParametersOriginalReponse = await challengeParameterProvider.GetItemAsync(inputModel.Id);

            // Change the challenge name, description and id
            var challenge = challegeOriginalResponse.Item2;

            challenge.Name                 = inputModel.Name;
            challenge.Description          = inputModel.Description;
            challenge.Id                   = newChallengeId;
            challenge.IsLocked             = false;
            challenge.IsPublic             = false;
            challenge.AzureServiceCategory = inputModel.AzureServiceCategory;

            // For each assigned question, change the id and associated id in the
            foreach (var q in assignedQuestionOriginalResponse.Item2)
            {
                var newQuestionId = Guid.NewGuid().ToString();

                var challengeQuestion = challenge.Questions.Where(p => p.Id == q.QuestionId).FirstOrDefault();
                challengeQuestion.Id = newQuestionId;
                q.QuestionId         = newQuestionId;
                q.ChallengeId        = newChallengeId;
            }

            // We need to set the correct new nextQuestionIds
            for (var i = 0; i < challenge.Questions.Count(); i++)
            {
                if (i + 1 != challenge.Questions.Count())
                {
                    challenge.Questions[i].NextQuestionId = challenge.Questions[i + 1].Id;
                }
            }

            globalParametersOriginalReponse.Item2.ChallengeId = newChallengeId;

            // Add the challenge
            await challengeProvider.AddItemAsync(challenge);

            await challengeParameterProvider.AddItemAsync(globalParametersOriginalReponse.Item2);

            // For each question, add it
            foreach (var q in assignedQuestionOriginalResponse.Item2)
            {
                await assignedQuestionProvider.AddItemAsync(q);
            }

            // Add some delay so that the database can commit the copy. Otherwise, doesn't appear on the refresh...
            Task.Delay(1000).Wait();

            return(Ok());
        }