Пример #1
0
        public ActionResult EnrollToCourse(UsersInCourseView userInCourseView)
        {
            this.logger.Info("Entering: " + System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName + ": " + System.Reflection.MethodBase.GetCurrentMethod().Name + " --> " + User.Identity.Name);
            try
            {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(this.apiMethodsUrl);
                    client.DefaultRequestHeaders.Accept.Add(
                        new MediaTypeWithQualityHeaderValue("application/json")
                        );
                    HttpResponseMessage response = client.GetAsync("api/course/GetCourseById/?id=" + userInCourseView.CourseId).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        Courses course = response.Content.ReadAsAsync <Courses>().Result;
                        //verify if enrollement key is correct
                        if (course.enrollementKey != null && !course.enrollementKey.Equals(userInCourseView.EnrollementKey))
                        {
                            throw new CustomException("Enrollment key is incorrect");
                        }
                    }
                    else
                    {
                        throw new CustomException("Could not complete the operation!");
                    }
                }
                UsersInCourseDTO dto = new UsersInCourseDTO();
                dto.CourseId         = userInCourseView.CourseId;
                dto.UserId           = userInCourseView.UserId;
                dto.EnrollementKey   = userInCourseView.EnrollementKey;
                dto.CourseUserstatus = userInCourseView.EnrollementKey;
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(this.apiMethodsUrl);
                    client.DefaultRequestHeaders.Accept.Add(
                        new MediaTypeWithQualityHeaderValue("application/json")
                        );

                    HttpResponseMessage response = client.PostAsJsonAsync("api/course/EnrollStudentInCourse/?usersInCourse=", dto).Result;
                    if (!response.IsSuccessStatusCode)
                    {
                        throw new CustomException("Could not complete the operation!");
                    }
                }
                return(RedirectToAction("DisplayAllAvailableCourses"));
            }
            catch (CustomException ce)
            {
                this.logger.Trace(ce, "Username: "******"Error"));
            }
            catch (Exception ex)
            {
                this.logger.Trace(ex, "Username: "******"Operation could not be completed! Try again.";
                return(View("Error"));
            }
        }
Пример #2
0
        //
        // GET: /Student/EnrollToCourse

        public ActionResult EnrollToCourse(int id = 0)
        {
            this.logger.Info("Entering: " + System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName + ": " + System.Reflection.MethodBase.GetCurrentMethod().Name + " --> " + User.Identity.Name);
            var _userId      = Session["UserId"];
            var _sessionUser = Convert.ToInt32(_userId);
            UsersInCourseView userInCourseView = new UsersInCourseView();

            userInCourseView.CourseId = id;
            userInCourseView.UserId   = (int)_userId;
            return(View(userInCourseView));
        }