public void RepresentationNewRating(string userId, int representationId, int rating)
        {
            //representation rating
            Representation representation = repository.Representations
                                            .FirstOrDefault(r => r.RepresentationId == representationId);

            representation.Rating = rating;
            repository.SaveRepresentation(representation);
            //add new notification
            repository.NewNotification(userId, "RepresentationRating", "Video", representation.VideoId);

            _ = ShowRepresentationNewRating(userId, representationId);
        }
        public async Task <IActionResult> NewRepresentation(NewRepresentation model)
        {
            AppUser user = await userManager.FindByIdAsync(model.UserId);

            if (user != null)
            {
                if (ModelState.IsValid)
                {
                    //create new representation
                    string representationTitle = model.TitlePlaceholder.Replace("_", " ");
                    if (model.RepresentationTitle != null)
                    {
                        representationTitle = (model.RepresentationTitle.Trim() == "") ?
                                              model.TitlePlaceholder.Replace("_", " ") :
                                              model.RepresentationTitle;
                    }

                    Presentation presentation = repository.Presentations
                                                .FirstOrDefault(p => p.PresentationId == model.PresentationId);
                    Representation representation = new Representation
                    {
                        CreatedBy = user,
                        Title     = representationTitle,
                        DateAdded = DateTime.Now,
                        Status    = "Public"
                    };
                    presentation.Representations.Add(representation);
                    repository.SavePresentation(presentation);

                    //save representation video
                    representation = repository.Presentations
                                     .FirstOrDefault(p => p.PresentationId == model.PresentationId)
                                     .Representations.FirstOrDefault(r => r.CreatedBy == user);

                    int videoId = 0;

                    string base64 = model.videoUrl.Substring(model.videoUrl.IndexOf(',') + 1);
                    byte[] data   = Convert.FromBase64String(base64);

                    //Create video directory
                    string videoTitle = model.TitlePlaceholder;
                    if (model.VideoTitle != null)
                    {
                        videoTitle = (model.VideoTitle.Trim() == "") ?
                                     model.TitlePlaceholder :
                                     model.VideoTitle.Replace(" ", "_");
                    }

                    var dirPath = Path.Combine(hostingEnvironment.WebRootPath,
                                               $@"UsersData\{model.UserId}\Representations");
                    System.IO.Directory.CreateDirectory(dirPath);
                    var fileName = $@"{videoTitle}.mp4";
                    var filePath = Path.Combine(dirPath, fileName);

                    //save video
                    Video video = new Video
                    {
                        Title     = $"{videoTitle.Replace("_", " ")}",
                        CreatedBy = model.UserId,
                        Status    = "Public",
                        For       = "Representation",
                        ForId     = representation.RepresentationId,
                        DateAdded = DateTime.Now,
                        VideoPath = $@"/UsersData/{model.UserId}/Representations/{videoTitle}.mp4"
                    };
                    repository.SaveVideo(video);

                    videoId = repository.Videos
                              .FirstOrDefault(v => v.For == "Representation" &&
                                              v.ForId == representation.RepresentationId).Id;

                    using (var videoFile = new FileStream(filePath, FileMode.Create))
                    {
                        videoFile.Write(data, 0, data.Length);
                        videoFile.Flush();
                    }

                    representation.VideoId = videoId;
                    repository.SaveRepresentation(representation);

                    TempData["sccMsgCourse"] = "'" + representation.Title + "' has been saved!";
                }
                else
                {
                    TempData["errMsgCourse"] = "Sorry, something went wrong!";
                }
            }
            return(RedirectToAction("CourseDetails", new
            {
                id = model.UserId,
                courseId = model.CourseId
            }));
        }
        public async Task <IActionResult> SaveVideo(VideoModel model)
        {
            AppUser user = await userManager.FindByIdAsync(model.UserId);


            if (user != null)
            {
                ViewData["Id"] = user.Id;
                int videoId = 0;

                string base64 = model.videoUrl.Substring(model.videoUrl.IndexOf(',') + 1);
                byte[] data   = Convert.FromBase64String(base64);

                //Create video directory
                var dirPath  = "";
                var fileName = "";
                var filePath = "";
                switch (model.For)
                {
                case "Course":
                    Course course = repository.Courses
                                    .FirstOrDefault(c => c.CourseId == model.ForId);

                    dirPath = Path.Combine(hostingEnvironment.WebRootPath,
                                           $@"UsersData\{model.UserId}\Courses\{course.CourseId}");
                    System.IO.Directory.CreateDirectory(dirPath);
                    fileName = $@"{course.Title}.mp4";
                    filePath = Path.Combine(dirPath, fileName);

                    //save course video
                    Video video = new Video
                    {
                        Title     = $"{course.Title}Video",
                        CreatedBy = model.UserId,
                        Status    = "Public",
                        For       = "Course",
                        ForId     = course.CourseId,
                        DateAdded = DateTime.Now,
                        VideoPath = $@"/UsersData/{model.UserId}/Courses/{course.CourseId}/{course.Title}.mp4"
                    };
                    repository.SaveVideo(video);
                    course.VideoId = repository.Videos
                                     .FirstOrDefault(v => v.ForId == course.CourseId).Id;
                    repository.SaveCourse(course);

                    videoId = course.VideoId;

                    break;

                case "Presentation":
                    Presentation presentation = repository.Presentations
                                                .FirstOrDefault(p => p.PresentationId == model.ForId);

                    dirPath = Path.Combine(hostingEnvironment.WebRootPath,
                                           $@"UsersData\{user.Id}\Presentations\");
                    System.IO.Directory.CreateDirectory(dirPath);
                    fileName = $@"{presentation.PresentationId}.mp4";
                    filePath = Path.Combine(dirPath, fileName);

                    //save presentation video
                    Video video2 = new Video
                    {
                        Title     = $"{presentation.Title}Video",
                        CreatedBy = model.UserId,
                        Status    = "Public",
                        For       = "Presentation",
                        ForId     = presentation.PresentationId,
                        DateAdded = DateTime.Now,
                        VideoPath = $@"/UsersData/{user.Id}/Presentations/{presentation.PresentationId}.mp4"
                    };
                    repository.SaveVideo(video2);
                    presentation.VideoId = repository.Videos
                                           .FirstOrDefault(v => v.ForId == presentation.PresentationId).Id;
                    repository.SavePresentation(presentation);
                    videoId = presentation.VideoId;

                    break;

                case "Representation":
                    Representation representation = repository.Representations
                                                    .FirstOrDefault(r => r.RepresentationId == model.ForId);
                    dirPath = Path.Combine(hostingEnvironment.WebRootPath,
                                           $@"UsersData\{user.Id}\Representations");
                    System.IO.Directory.CreateDirectory(dirPath);
                    fileName = $@"{representation.RepresentationId}.mp4";
                    filePath = Path.Combine(dirPath, fileName);

                    //save representation vide path
                    Video video3 = new Video
                    {
                        Title     = $"{representation.Title}Video",
                        CreatedBy = model.UserId,
                        Status    = "Public",
                        For       = "Representation",
                        ForId     = representation.RepresentationId,
                        DateAdded = DateTime.Now,
                        VideoPath = $@"/UsersData/{user.Id}/Representations/{representation.RepresentationId}.mp4"
                    };
                    repository.SaveVideo(video3);
                    representation.VideoId = repository.Videos
                                             .FirstOrDefault(v => v.Id == representation.RepresentationId).Id;
                    repository.SaveRepresentation(representation);
                    videoId = representation.VideoId;

                    break;
                }

                using (var videoFile = new FileStream(filePath, FileMode.Create))
                {
                    videoFile.Write(data, 0, data.Length);
                    videoFile.Flush();
                }

                return(RedirectToAction("Index", new { id = user.Id, videoId }));
            }
            else
            {
                ModelState.AddModelError("", "User Not Found");
            }

            return(Redirect(model.returnUrl));
        }