コード例 #1
0
        /// <summary>
        /// Creates a challenge for each collection based on template
        /// </summary>
        /// <param name="learningPaths">The list of collections to be used in the template.</param>
        /// <param name="request">The template to be used to created the contest.</param>
        /// <returns>The ContestResponse.</returns>
        private async Task <List <ContestResponse> > CreateCollectionChallengesAsync(IList <LearningPath> learningPaths, ContestRequest request)
        {
            if (learningPaths == null)
            {
                throw new ArgumentNullException($"{nameof(learningPaths)} is null");
            }

            var results = new List <ContestResponse>();

            request.Name = $"{request.Name} | {DateTime.Now.ToString(ContestDateFormat)} {ContestNameTag}";

            foreach (LearningPath lp in learningPaths)
            {
                request.CollectionUrl  = lp.CollectionUrl;
                request.CollectionName = lp.CollectionName;
                request.CollectionID   = lp.GetCollectionId();

                if (!string.IsNullOrWhiteSpace(request.CollectionName) &&
                    request.CollectionUrl.StartsWith(MsLearnUriPrefix) &&
                    !string.IsNullOrWhiteSpace(request.CollectionName))
                {
                    ContestResponse result = await CreateChallengeAsync(request);

                    results.Add(result);
                }
            }

            return(results);
        }
コード例 #2
0
        /// <summary>
        /// Return the progress precentage of the user
        /// </summary>
        /// <param name="contestId">The contestId</param>
        /// <param name="userName">The mslearnid</param>
        /// <returns>T
        ///     The progress percentage of the learning path the user completed.
        ///     -1 if the user was not found in the learning path.
        /// </returns>
        public async Task <int> GetUserProgressAsync(string contestId, string userName)
        {
            ContestResponse currentContest = await GetContestAsync(contestId);

            foreach (Learner learner in currentContest.Learners)
            {
                if (learner.UserName.ToLower() == userName.ToLower())
                {
                    return(int.Parse(learner.ProgressPercentage));
                }
            }

            return(-1);
        }
コード例 #3
0
        /// <summary>
        /// Return the progress precentage of the user
        /// </summary>
        /// <param name="contestId">The contestId</param>
        /// <param name="userName">The mslearnid</param>
        /// <returns>T
        ///     The progress percentage of the learning path the user completed.
        ///     -1 if the user was not found in the learning path.
        /// </returns>
        public async Task <Learner> GetLearner(string contestId, string userName)
        {
            ContestResponse currentContest = await GetContestAsync(contestId);

            foreach (Learner learner in currentContest.Learners)
            {
                if (learner.UserName.ToLower() == userName.ToLower())
                {
                    learner.NextUpdateOn = $"{currentContest.NextProgressUpdateOn}({currentContest.TimeZone})";
                    return(learner);
                }
            }

            return(null);
        }
コード例 #4
0
        /// <summary>
        /// Creates a Cloud Skills Challenge based on the parameter.
        /// </summary>
        /// <param name="contest">The argurments to pass to the server.</param>
        /// <returns>The ContestResponse from the server.</returns>
        private async Task <ContestResponse> CreateChallengeAsync(ContestRequest contest)
        {
            Uri    uri  = new Uri($"{apiRoot}{ApiPathContests}");
            string json = JsonConvert.SerializeObject(contest);

            try
            {
                HttpResponseMessage response = await httpClient.PostAsync(uri,
                                                                          new StringContent(json, Encoding.UTF8, MediaTypeJson));

                string jsonResponse = await response.Content.ReadAsStringAsync();

                ContestResponse result = JsonConvert.DeserializeObject <ContestResponse>(jsonResponse);
                return(result);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(string.Format("Error: {0}; Request details: {1}", ex.Message, json));
                throw;
            }
        }
コード例 #5
0
        /// <summary>
        /// Makes get call to fetch contest.
        /// </summary>
        /// <param name="contestId">The id of the contest to fetch</param>
        /// <returns>The contest response if value. Throw exception otherwise</returns>
        private async Task <ContestResponse> GetContestAsync(string contestId)
        {
            if (string.IsNullOrWhiteSpace(contestId))
            {
                throw new ArgumentNullException($"{nameof(contestId)} is blank");
            }

            string uri = string.Concat(apiRoot, ApiPathContests, contestId);
            HttpResponseMessage response = await httpClient.GetAsync(uri);

            if (!response.IsSuccessStatusCode)
            {
                throw new HttpRequestException("Failed http request");
            }

            string jsonResponse = await response.Content.ReadAsStringAsync();

            ContestResponse result = JsonConvert.DeserializeObject <ContestResponse>(jsonResponse);

            return(result);
        }
コード例 #6
0
        public async Task <String> GetCollectionNameAsync(string contestId)
        {
            ContestResponse currentContest = await GetContestAsync(contestId);

            return(currentContest.CollectionName);
        }