コード例 #1
0
        public async Task <IActionResult> AddCourse(CourseCreateRequestModel model, [FromForm] IFormFile imageFile)
        {
            ViewData["UserName"] = HttpContext.Session.GetString("UserName");
            var jwtToken = HttpContext.Session.GetString("Token");
            var userId   = HttpContext.Session.GetString("UserId");

            //CourseType
            var apiResponse = await httpClientConfig.GetRestResponse("CourseType/getAllCourseType", jwtToken);

            var courseTypes = JsonConvert.DeserializeObject <CourseType>(apiResponse);

            //CourseCategory
            var apiResponse2 = await httpClientConfig.GetRestResponse("CourseCategory/getAllCourseCategory", jwtToken);

            var courseCategory = JsonConvert.DeserializeObject <CourseCategory>(apiResponse2);

            //CourseLevel
            var apiResponse3 = await httpClientConfig.GetRestResponse("CourseLevel/getAllCourseLevel", jwtToken);

            var courseLevel = JsonConvert.DeserializeObject <CourseLevelTypes>(apiResponse3);

            //Add Select option to the select List
            courseTypes.Data.Insert(0, new CourseTypeData {
                id = 0, courseTypeName = "Select Course Type"
            });
            courseCategory.Data.Insert(0, new CourseCategoryData {
                id = 0, courseCategoryName = "Select Category"
            });
            courseLevel.Data.Insert(0, new CourseLevelTypesData {
                id = 0, levelTypeName = "Select Course Level "
            });

            //Send the data to the View Bag
            ViewBag.CourseTypeList     = courseTypes.Data;
            ViewBag.CourseCategoryList = courseCategory.Data;
            ViewBag.CourseLevelList    = courseLevel.Data;


            model.FacilitatorId = new Guid(userId);



            string courseImageUrl = string.Empty;

            if (imageFile != null) //checks if the course Image is not null
            {
                //Image Upload to Cloudinary Instance
                var imageUploadResult = await _cloudinary.ImagesUpload(imageFile);

                courseImageUrl = imageUploadResult.Url.ToString();

                model.CourseImageUrl = courseImageUrl;
            }
            else
            {
                model.CourseImageUrl = "";
            }

            var restResponse = await httpClientConfig.PostRestResponseJson("Course/createCourse", model, jwtToken);

            //Response deserialized to Object Type specified
            var resp = JsonConvert.DeserializeObject <CreateResponseModel>(restResponse);

            if (resp.StatusCode == 200)
            {
                ViewData["message"] = resp.StatusMessage;
            }


            else
            {
                TempData["alert"] = "alert-danger";
                ViewBag.Message   = resp.StatusMessage;
                return(View(model));
            }
            return(View(model));
        }