private void MapRequestToProperties(EditCourseItemCommand request, CourseItem item)
 {
     item.State = request.State;
     item.Order = request.Order;
     item.Title = request.Title;
     item.AdminMessageForTeacher = request.AdminMessageForTeacher;
     item.IsPreview   = request.IsPreview;
     item.ContentType = request.File != null?GetContentType(request.File.ContentType) : ContentType.File;
 }
        private async Task HandleCoursePendingToApproveItemCount(EditCourseItemCommand request, CourseItem item)
        {
            if (item.State == CourseItemApprovalState.PendingToApproveByAdmin &&
                request.State != CourseItemApprovalState.PendingToApproveByAdmin)
            {
                var course = await _courseRepo.GetById(item.CourseId);

                course.HasPendingItemToApprove = true;
                await _courseRepo.EditAsync(course);
            }
        }
        public async Task <CourseItemViewModel> Handle(EditCourseItemCommand request,
                                                       CancellationToken cancellationToken)
        {
            var item = await _courseItemRepo.GetById(request.Id);

            await HandleCoursePendingToApproveItemCount(request, item);

            MapRequestToProperties(request, item);
            await HandleUploadedFileAndItemFilePath(request, item);

            await _courseItemRepo.EditAsync(item);

            return(_mapper.Map <CourseItemViewModel>(item));
        }
        private async Task HandleUploadedFileAndItemFilePath(EditCourseItemCommand request, CourseItem item)
        {
            if (request.File != null)
            {
                if (File.Exists(item.FilePath))
                {
                    _coursesFileManager.DeleteFile(item.FilePath);
                }
                var course = _courseRepo.GetQueryable().Include(x => x.Teacher)
                             .First(x => x.Contents.Any(c => c.Id == request.Id));
                await _coursesFileManager.SaveFile(request.File, course.Teacher.UserName, item.CourseId);

                item.FilePath = _coursesFileManager.GetFilePathForDownload(request.File, course.Teacher.UserName, item.CourseId);
            }
        }