public ActionResult VideoPage(long?id) { if (id == null) { return(View("Error")); } Video currentVideo = _videosRepository.GetVideoById(id); if (currentVideo == null) { return(View("Error")); } if (currentVideo.Blocked == true || currentVideo.User.Blocked == true || currentVideo.VideoType == "PRIVATE") { if (!(UsersHelper.LoggedInUserIsAdmin(Session) && !UsersHelper.LoggedInUserIsBlocked(Session)) && !UsersHelper.LoggedInUserIsOnHisPage(Session, currentVideo.VideoOwner)) { return(View("Error")); } } currentVideo.ViewsCount += 1; _videosRepository.UpdateVideo(currentVideo); if (UsersHelper.LoggedInUserUsername(Session) != null) { bool exists = CheckIfSubbed(currentVideo.VideoOwner); VideoRating rating = GetVideoRatingForVideo(id); ViewBag.Subbed = exists; ViewBag.Rating = rating?.IsLike; } var video = VideoDTO.ConvertVideoToDTO(currentVideo); return(View(video)); }
public async Task <JsonNetResult> GetRatings(GetRatingsViewModel model) { // We definitely want the overall rating info, so start there Task <VideoRating> ratingTask = _ratings.GetRating(model.VideoId); // If a user is logged in, we also want their rating Guid?userId = User.GetCurrentUserId(); Task <UserVideoRating> userRatingTask = null; if (userId.HasValue) { userRatingTask = _ratings.GetRatingFromUser(model.VideoId, userId.Value); } // Await data appropriately VideoRating ratingData = await ratingTask; UserVideoRating userRating = null; if (userRatingTask != null) { userRating = await userRatingTask; } return(JsonSuccess(new RatingsViewModel { VideoId = ratingData.VideoId, CurrentUserLoggedIn = userId.HasValue, CurrentUserRating = userRating == null ? 0 : userRating.Rating, RatingsCount = ratingData.RatingsCount, RatingsSum = ratingData.RatingsTotal })); }
public JsonResult CreateNewVideoRating(long videoId, bool newRating) { VideoRating vr = new VideoRating { LikeOwner = UsersHelper.LoggedInUserUsername(Session), VideoID = videoId, LikeDate = DateTime.Now, IsLike = newRating }; _videoRatingRepository.CreateVideoRating(vr); Video video = _videosRepository.GetVideoById(vr.VideoID); if (newRating) { video.LikesCount += 1; } else { video.DislikesCount += 1; } _videosRepository.UpdateVideo(video); string returnMessage = (newRating == true) ? "like" : "dislike"; return(Json(new { returnMessage, video.LikesCount, video.DislikesCount }, JsonRequestBehavior.AllowGet)); }
public VideoRating GetVideoRatingForVideo(long?id) { if (UsersHelper.LoggedInUserUsername(Session) == null) { return(null); } using (var videoRatingRepository = new VideoRatingRepository(new MyTubeDBEntities())) { VideoRating rating = videoRatingRepository.GetVideoRating((long)id, UsersHelper.LoggedInUserUsername(Session)); return(rating); } }
public JsonResult AlterExistingVideoRating(VideoRating vr, bool newRating) { string returnMessage = ""; Video video = _videosRepository.GetVideoById(vr.VideoID); //true = like, false = dislike if (vr.IsLike) { if (newRating) { video.LikesCount -= 1; returnMessage = "neutral"; } else { video.LikesCount -= 1; video.DislikesCount += 1; returnMessage = "dislike"; } } else { if (newRating) { video.LikesCount += 1; video.DislikesCount -= 1; returnMessage = "like"; } else { video.DislikesCount -= 1; returnMessage = "neutral"; } } _videosRepository.UpdateVideo(video); vr.IsLike = newRating; if (returnMessage == "neutral") { _videoRatingRepository.DeleteVideoRating(vr.LikeID); } else { _videoRatingRepository.UpdateVideoRating(vr); } return(Json(new { returnMessage, video.LikesCount, video.DislikesCount }, JsonRequestBehavior.AllowGet)); }
public JsonResult CreateVideoRating(long videoId, bool newRating) { if (UsersHelper.LoggedInUserUsername(Session) == null) { return(null); } string username = UsersHelper.LoggedInUserUsername(Session); VideoRating vr = _videoRatingRepository.GetVideoRating(videoId, username); if (vr != null) { return(AlterExistingVideoRating(vr, newRating)); } else { return(CreateNewVideoRating(videoId, newRating)); } }
public async Task <string> RateVideo(string videoId, VideoRating rating, AccessToken token) { string like = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><entry xmlns=\"http://www.w3.org/2005/Atom\"" + " xmlns:yt=\"http://gdata.youtube.com/schemas/2007\"><yt:rating value=\"" + rating.ToString() + "\"/></entry>"; List <KeyValuePair <string, string> > k = new List <KeyValuePair <string, string> >(); k.Add(new KeyValuePair <string, string>("Host", "gdata.youtube.com")); k.Add(new KeyValuePair <string, string>("X-GData-Key", "key=AIzaSyCH5E5roCLWZgTmZK938wTMFgT3BYXhsvI")); k.Add(new KeyValuePair <string, string>("GData-Version", "2.1")); k.Add(new KeyValuePair <string, string>("Authorization", token.token_type + " " + token.access_token)); return(await Util.MakeWebPostRequestAsync( string.Format("https://gdata.youtube.com/feeds/api/videos/{0}/ratings", videoId), k, like, new System.Net.Http.Headers.MediaTypeHeaderValue("application/atom+xml") )); }
public void UpdateVideoRating(VideoRating vr) { db.Entry(vr).State = EntityState.Modified; db.SaveChanges(); }
public void CreateVideoRating(VideoRating vr) { db.VideoRatings.Add(vr); db.SaveChanges(); }