public IActionResult GetAllVideos() { IEnumerable <Video> videos = new List <Video>(); List <VideoDTO> videosDTO = new List <VideoDTO>(); videos = _videoData.GetAllForAdmin(); if (videos.Count() <= 0) { return(NoContent()); } foreach (var v in videos) { v.Owner = _userData.GetById(v.OwnerId); v.Owner.UserVideos = null; VideoDTO videoDTO = VideoDTO.ConvertVideoToDTO(v); videosDTO.Add(videoDTO); } var contentType = Request.ContentType; if (contentType != null) { if (contentType.Equals("application/json")) { return(Json(videosDTO)); } else if (contentType.Equals("text/html")) { return(View("Videos", videosDTO)); } return(StatusCode(415)); } return(Json(videosDTO)); }
public IActionResult BlockVideo(int id) { var contentType = Request.ContentType; Video video = _videoData.GetById(id); if (video == null) { return(NotFound()); } if (video.Blocked == true) { return(BadRequest()); } video.Blocked = true; video = _videoData.Update(video); video.Owner = _userData.GetById(video.OwnerId); VideoDTO videoDTO = VideoDTO.ConvertVideoToDTO(video); if (contentType != null) { if (contentType.Equals("application/json")) { return(Json(videoDTO)); } return(StatusCode(415)); } return(Json("Success")); }
public IActionResult EditVideo(VideoDTO videoDTO, IFormFile image) { if (ModelState.IsValid) { Video editVideo = _videoData.GetById(videoDTO.Id); if (editVideo == null) { return(NotFound()); } editVideo.AllowComments = videoDTO.AllowComments; editVideo.AllowRaiting = videoDTO.AllowRaiting; editVideo.Description = videoDTO.Description; editVideo.Name = videoDTO.Name; editVideo.Visibility = videoDTO.Visibility; if (image != null) { var username = HttpContext.Session.GetString("LoggedInUserUsername"); editVideo.PhotoUrl = AddImage(editVideo.Name, username, image); } editVideo.Owner = _userData.GetById(editVideo.OwnerId); VideoDTO video = VideoDTO.ConvertVideoToDTO(editVideo); editVideo = _videoData.Update(editVideo); var contentType = Request.ContentType; if (contentType != null) { if (contentType.Equals("application/json")) { return(Json(video)); } else if (contentType.Equals("text/html")) { //return RedirectToAction(nameof(GetAllUsers)); } return(RedirectToAction(nameof(GetVideoById), video.Id)); } return(Json(video)); } else { return(BadRequest()); } }
public IActionResult Home() { var loggedInUserUsername = HttpContext.Session.GetString("LoggedInUserUsername"); var loggedInUserRole = HttpContext.Session.GetString("LoggedInUserRole"); var loggedInUserId = HttpContext.Session.GetString("LoggedInUserId"); IEnumerable <Video> top5Videos = new List <Video>(); IEnumerable <Video> allVideos = new List <Video>(); List <VideoDTO> top5VideosDTO = new List <VideoDTO>(); List <VideoDTO> allVideosDTO = new List <VideoDTO>(); if (loggedInUserRole == null) { top5Videos = _videoData.GetTop5("NoUser", 0); allVideos = _videoData.GetAllNoUser(); } else { if (loggedInUserRole.Equals("Admin")) { top5Videos = _videoData.GetTop5("Admin", long.Parse(loggedInUserId)); allVideos = _videoData.GetAllForAdmin(); } else { top5Videos = _videoData.GetTop5("User", long.Parse(loggedInUserId)); allVideos = _videoData.GetAllForUser(long.Parse(loggedInUserId)); } } foreach (var v in top5Videos) { v.Owner = _userData.GetById(v.OwnerId); v.Owner.UserVideos = null; VideoDTO videoDTO = VideoDTO.ConvertVideoToDTO(v); top5VideosDTO.Add(videoDTO); } foreach (var v in allVideos) { v.Owner = _userData.GetById(v.OwnerId); v.Owner.UserVideos = null; VideoDTO videoDTO = VideoDTO.ConvertVideoToDTO(v); allVideosDTO.Add(videoDTO); } HomeVideoViewModel homeVideoViewModel = new HomeVideoViewModel(); homeVideoViewModel.AllVideos = allVideosDTO; homeVideoViewModel.Top5Videos = top5VideosDTO; var contentType = Request.ContentType; if (contentType != null) { if (contentType.Equals("application/json")) { return(Json(homeVideoViewModel)); } else if (contentType.Equals("text/html")) { return(View("~/Views/Shared/HomePage.cshtml", homeVideoViewModel)); } return(StatusCode(415)); } return(View("~/Views/Shared/HomePage.cshtml", homeVideoViewModel)); }
public IActionResult GetVideoById(long id) { var contentType = Request.ContentType; Video video = new Video(); if (contentType != null) { video = _videoData.GetById(id); if (video.Name == null) { return(NotFound()); } } else { var loggedInUserRole = HttpContext.Session.GetString("LoggedInUserRole"); if (loggedInUserRole == null) { video = _videoData.GetById(id); if (video == null) { return(NotFound()); } if (video.Blocked == true || video.Visibility == Visibility.Private) { return(StatusCode(401)); } } else { if (loggedInUserRole.Equals("0")) { video = _videoData.GetById(id); if (video == null) { return(NotFound()); } } else { long loggedInUserId = long.Parse(HttpContext.Session.GetString("LoggedInUserId")); video = _videoData.GetById(id); if (video == null) { return(NotFound()); } if (video.OwnerId != loggedInUserId && (video.Blocked == true || video.Visibility == Visibility.Private)) { return(StatusCode(401)); } } } } video.Owner = _userData.GetById(video.OwnerId); video.Comments = _commentData.GetCommentsByVideo(video.Id); foreach (var comment in video.Comments) { comment.User = _userData.GetById(comment.UserId); } video.NumberOfViews++; video = _videoData.Update(video); SingleVideoDTO singleVideoDTO = SingleVideoDTO.ConvertVideoToDTO(video); if (contentType != null) { if (contentType.Equals("application/json")) { return(Json(singleVideoDTO)); } else if (contentType.Equals("text/html")) { VideoDTO newDTO = VideoDTO.ConvertVideoToDTO(video); return(View("VideoInfo", newDTO)); } return(StatusCode(415)); } return(View("VideoPage", singleVideoDTO)); }
public IActionResult AddVideo(AddVideoDTO videoDTO, IFormFile image) { if (ModelState.IsValid) { var loggedInUserId = HttpContext.Session.GetString("LoggedInUserId"); if (loggedInUserId == null) { return(StatusCode(401)); } User user = _userData.GetById(long.Parse(loggedInUserId)); if (user == null) { return(StatusCode(401)); } if (user.Blocked == true) { return(StatusCode(401)); } Video newVideo = new Video(); newVideo.AllowComments = videoDTO.AllowComments; newVideo.AllowRaiting = videoDTO.AllowRaiting; newVideo.Blocked = false; newVideo.Deleted = false; newVideo.Description = videoDTO.Description; newVideo.CreationDate = DateTime.Today; newVideo.Name = videoDTO.Name; newVideo.NumberOfDislikes = 0; newVideo.NumberOfLikes = 0; newVideo.NumberOfViews = 0; newVideo.OwnerId = user.Id; newVideo.VideoUrl = videoDTO.VideoUrl; newVideo.Visibility = videoDTO.Visibility; if (image != null) { newVideo.PhotoUrl = AddImage(newVideo.Name, user.Username, image); } else { newVideo.PhotoUrl = "defaultVideoImage.jpg"; } newVideo = _videoData.Create(newVideo); VideoDTO video = VideoDTO.ConvertVideoToDTO(newVideo); var contentType = Request.ContentType; user.Followers = _followData.GetFollowers(user.Id); user.Following = _followData.GetFollowings(user.Id); user.UserVideos = _videoData.GetByOwnerId(user.Id); user.LikedVideos = _videoData.GetLikedVideos(user); foreach (var u in user.LikedVideos) { u.Owner = _userData.GetById(u.OwnerId); } SingleUserDTO singleUserDTO = SingleUserDTO.ConvertUserToDTO(user); if (contentType != null) { if (contentType.Equals("application/json")) { return(Json(video)); } else if (contentType.Equals("text/html")) { return(View("UserPage", singleUserDTO)); } return(View("UserPage", singleUserDTO)); } return(Json(video)); } else { return(Json(new VideoDTO())); } }