private SectionedVideo GetSectionedVideo(int videoId, int userId, int sectionId) { SectionedVideo sectionedVideo = _database .SectionedVideos .Where(x => x.VideoId == videoId && x.UserId == userId && x.SectionId == sectionId) .FirstOrDefault(); return(sectionedVideo); }
public void HandleDislike(int videoId, int userId) { Section likedSection = _database.Sections.Where(x => x.Name == Constants.Strings.Sections.Liked).FirstOrDefault(); Section dislikedSection = _database.Sections.Where(x => x.Name == Constants.Strings.Sections.Disliked).FirstOrDefault(); if (likedSection == null) { throw new NotImplementedException("liked section doesnt exist or section name is wrong"); } if (dislikedSection == null) { throw new NotImplementedException("disliked section doesnt exist or section name is wrong"); } SectionedVideo userLikedVideo = GetSectionedVideo(videoId, userId, likedSection.Id); SectionedVideo userDislikedVideo = GetSectionedVideo(videoId, userId, dislikedSection.Id); Video video = _database.Videos.Find(videoId); if (userLikedVideo != null) { video.Likes--; _database.Videos.Update(video); _database.SectionedVideos.Remove(userLikedVideo); } if (userDislikedVideo == null) { video.Dislikes++; _database.Videos.Update(video); userDislikedVideo = new SectionedVideo() { VideoId = videoId, UserId = userId, SectionId = dislikedSection.Id, }; _database.SectionedVideos.Add(userDislikedVideo); } else { video.Dislikes--; _database.Videos.Update(video); _database.SectionedVideos.Remove(userDislikedVideo); } _database.SaveChanges(); }
public void HandleView(int videoId, int?userId) { Video video = _database.Videos.Find(videoId); if (userId == null) { video.Views++; _database.Videos.Update(video); _database.SaveChanges(); return; } Section historySection = _database.Sections.Where(x => x.Name == Constants.Strings.Sections.History).FirstOrDefault(); if (historySection == null) { throw new NotImplementedException("history section doesnt exist or section name is wrong"); } SectionedVideo userWatchedVideo = GetSectionedVideo(videoId, (int)userId, historySection.Id); if (userWatchedVideo == null) { userWatchedVideo = new SectionedVideo() { VideoId = videoId, UserId = (int)userId, SectionId = historySection.Id, }; _database.SectionedVideos.Add(userWatchedVideo); } video.Views++; _database.Videos.Update(video); _database.SaveChanges(); }