コード例 #1
0
        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));
        }
コード例 #2
0
        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
            }));
        }
コード例 #3
0
        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));
        }
コード例 #4
0
 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);
     }
 }
コード例 #5
0
        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));
        }
コード例 #6
0
        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));
            }
        }
コード例 #7
0
        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")
                       ));
        }
コード例 #8
0
 public void UpdateVideoRating(VideoRating vr)
 {
     db.Entry(vr).State = EntityState.Modified;
     db.SaveChanges();
 }
コード例 #9
0
 public void CreateVideoRating(VideoRating vr)
 {
     db.VideoRatings.Add(vr);
     db.SaveChanges();
 }