public async Task <IActionResult> AddCourse([FromBody] CourseForAddDto courseForAdd, int teacherId)
        {
            string auth = Request.Headers["Authorization"]; // get bearer string

            if (AuthHelper.BasicAuth(auth, teacherId, "Teacher") == false)
            {
                return(Unauthorized());
            }

            var course         = _mapper.Map <Course>(courseForAdd);
            var courseToReturn = _mapper.Map <CourseForDetailedDto>(await _repo.AddCourse(course, teacherId));

            return(Ok(courseToReturn));
        }
Beispiel #2
0
        public async Task <IActionResult> AddCourse([FromForm] CourseForAddDto courseForAddDto)
        {
            // courseCategoryForAddDto.Name = courseCategoryForAddDto.Name.ToLower();
            courseForAddDto.CreatedDate = DateTime.Now;

            courseForAddDto.CreatedBy   = User.Identity.Name.ToString();
            courseForAddDto.IdCreatedBy = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);
            courseForAddDto.UpdatedDate = DateTime.Now;
            courseForAddDto.UpdatedBy   = User.Identity.Name.ToString();


            // if (await _repo.UserExists(courseCategoryForAddDto.Name))
            //     return BadRequest("Tài khoản đã tồn tại");

            var file = courseForAddDto.File;

            // _repo.Add(courseToCreate);
            // await _repo.SaveAll();

            var courseToCreate = _mapper.Map <Course>(courseForAddDto);

            _repo.Add(courseToCreate);
            await _repo.SaveAll();

            int idOfCoursAdded = _repo.GetCourseMaxID();

            if (file != null)
            {
                string newFileName = idOfCoursAdded + "_" + file.FileName;
                string path        = Path.Combine(_hostingEnv.ContentRootPath, "Upload", newFileName);
                using (var stream = new FileStream(path, FileMode.Create))
                {
                    file.CopyTo(stream);
                    courseForAddDto.Image = newFileName;
                    courseToCreate.Image  = courseForAddDto.Image;
                    //_data.Entry(courseForAddDto).Property(x => x.Image).IsModified = true;
                    var courseFromRepo1 = await _repo.GetCourse(idOfCoursAdded);

                    _mapper.Map(courseToCreate, courseFromRepo1);
                    await _repo.SaveAll();
                }
            }



            return(Ok(courseToCreate));
        }
        public async Task <IActionResult> Add(CourseForAddDto courseForAddDto)
        {
            try
            {
                var user = await _userManager.GetUserAsync(User);

                var course = _mapper.Map <Course>(courseForAddDto);

                course.UserId = user.Id;

                _courseRepo.Add(course);

                if (await _courseRepo.SaveAll())
                {
                    return(Ok("Successfully created the course"));
                }

                return(BadRequest("Creating the course failed on save"));
            }
            catch (Exception e)
            {
                return(BadRequest("Creating the course failed"));
            }
        }