public async Task <IActionResult> DeleteVideo(int teacherId, int videoId) { string auth = Request.Headers["Authorization"]; // get bearer string if (AuthHelper.BasicAuth(auth, teacherId, "Teacher") == false) { return(Unauthorized()); } var videoToDelete = await _repo.GetVideo(videoId); if (videoToDelete != null) { if (videoToDelete.PublicId != null) { var delParam = new DeletionParams(videoToDelete.PublicId) { ResourceType = ResourceType.Video }; var delResResult = _cloudinary.Destroy(delParam); } await _repo.DeleteVideo(videoId); return(Ok()); } else { return(BadRequest("This course doesn't exist !!")); } }
public async Task <IActionResult> AddCoursePhoto(int teacherId, int courseId) { string auth = Request.Headers["Authorization"]; // get bearer string if (AuthHelper.BasicAuth(auth, teacherId, "Teacher") == false) { return(Unauthorized()); } var files = Request.Form.Files; if (files == null) { return(BadRequest("Request must contain at least a file")); } // init ava and cover file object dynamic ava = null; int avaCount = 0; FormFileChecker fileTypeChecker = new FormFileChecker(); foreach (var file in files) { if (fileTypeChecker.CheckFileType(file, "image")) { if (avaCount < 1) { ava = file; } else { return(BadRequest("Too much Ava to upload")); } } } // create FileUploadHelper reference FileUploadHelper fileUpload = new FileUploadHelper(_config); // Modify image link if posted data != null var courseToUpdate = await _repo.GetCourse(courseId, teacherId); try { courseToUpdate.AvaUrl = await fileUpload.UploadImage(ava); } catch (ImgurException imgurEx) { return(BadRequest("Unable to upload image to Imgur: " + imgurEx.Message)); } await _repo.SaveAll(); var courseToReturn = _mapper.Map <CourseForDetailedDto>(courseToUpdate); return(Ok(courseToReturn)); }
public async Task <IActionResult> GetCourse(int courseId, int studentId) { string auth = Request.Headers["Authorization"]; // get bearer string if (AuthHelper.BasicAuth(auth, studentId, "Student") == false) { return(Unauthorized()); } var enrolled = _repo.IsEnrolled(courseId, studentId); return(Ok(new { enrolled })); }
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)); }
public async Task <IActionResult> AddVideo(int courseId, int teacherId) { string auth = Request.Headers["Authorization"]; // get bearer string if (AuthHelper.BasicAuth(auth, teacherId, "Teacher") == false) { return(Unauthorized()); } var files = Request.Form.Files; if (files == null) { return(BadRequest("Request must contain video file")); } FormFileChecker fileChecker = new FormFileChecker(); foreach (var file in files) { if (!fileChecker.CheckFileType(file, "video")) { return(BadRequest("Course Video must be type of video")); } } // return Ok(files); var videoToReturn = new List <VideoForDetailedDto>(); FileUploadHelper fileUploadHelper = new FileUploadHelper(_cloudinary); foreach (var file in files) { var uploadResult = fileUploadHelper.UploadVideo(file); var videoToCreate = new VideoForCreateDto { CourseId = courseId, VideoUrl = uploadResult.Uri.ToString(), PublicId = uploadResult.PublicId }; var videoToAdd = _mapper.Map <Models.Video>(videoToCreate); var videoAdded = await _repo.AddVideo(videoToAdd, courseId); await _repo.SaveAll(); videoToReturn.Add(_mapper.Map <VideoForDetailedDto>(videoAdded)); } return(Ok(videoToReturn)); }
public async Task <IActionResult> GetCourses([FromQuery] CourseParam courseParams, int studentId) { string auth = Request.Headers["Authorization"]; // get bearer string if (AuthHelper.BasicAuth(auth, studentId, "Student") == false) { return(Unauthorized()); } var courses = await _repo.GetCourses(courseParams, studentId); var coursesToReturn = _mapper.Map <IEnumerable <CourseForListDto> >(courses); Response.AddPagination(courses.CurrentPage, courses.PageSize, courses.TotalCount, courses.TotalPages); return(Ok(coursesToReturn)); }
public async Task <IActionResult> EnrollCourse(int studentId, int courseId) { string auth = Request.Headers["Authorization"]; // get bearer string if (AuthHelper.BasicAuth(auth, studentId, "Student") == false) { return(Unauthorized()); } if (await _repo.Enroll(courseId, studentId)) { return(Ok()); } else { return(BadRequest("Unable to enroll to this course")); } }
public async Task <IActionResult> DeleteCourse(int teacherId, int courseId) { string auth = Request.Headers["Authorization"]; // get bearer string if (AuthHelper.BasicAuth(auth, teacherId, "Teacher") == false) { return(Unauthorized()); } if (await _repo.DeleteCourse(courseId)) { return(Ok()); } else { return(BadRequest("This course doesn't exist !!")); } }
public async Task <IActionResult> UpdateCourse(int teacherId, int courseId, [FromBody] CourseForUpdateDto courseForUpdate) { string auth = Request.Headers["Authorization"]; // get bearer string if (AuthHelper.BasicAuth(auth, teacherId, "Teacher") == false) { return(Unauthorized()); } var courseFromRepo = await _repo.GetCourse(courseId, teacherId); _mapper.Map(courseForUpdate, courseFromRepo); await _repo.SaveAll(); var courseToReturn = _mapper.Map <CourseForDetailedDto>(courseFromRepo); return(Ok(courseToReturn)); }
public async Task <IActionResult> UpdateTeacher(int id, [FromBody] TeacherForUpdateDto teacherForUpdateDto) { string auth = Request.Headers["Authorization"]; // get bearer string if (AuthHelper.BasicAuth(auth, id, "Teacher") == false) { return(Unauthorized()); } var teacherFromRepo = await _repo.GetTeacher(id); _mapper.Map(teacherForUpdateDto, teacherFromRepo); await _repo.SaveAll(); var teacherToReturn = _mapper.Map <TeacherDetailedDto>(teacherFromRepo); return(Ok(teacherToReturn)); }
public async Task <IActionResult> RateCourse(int studentId, int courseId, [FromBody] JObject content) { string auth = Request.Headers["Authorization"]; // get bearer string if (AuthHelper.BasicAuth(auth, studentId, "Student") == false) { return(Unauthorized()); } try { var ratingPoint = int.Parse(content.GetValue("ratingPoint").ToString()); if (await _repo.RateCourse(ratingPoint, courseId, studentId)) { return(Ok()); } else { return(StatusCode(304)); } } catch (Exception e) { return(BadRequest(e.Message)); } }