public async Task <bool> TagMovie(TaggingDTO taggingDTO) { if (!_repo.MovieExists(taggingDTO.MovieId)) { ApiHelper.MovieObject movieObject = await ApiHelper.ApiProcessor .LoadMovieAsync(taggingDTO.MovieId); if (movieObject == null) { return(false); } MovieDTO movieDTO = Mapper.MovieObjectToMovieDTO(movieObject); if (!(await CreateMovie(movieDTO))) { return(false); } } MovieTagUser movieTagUser = new MovieTagUser(); movieTagUser.ImdbId = taggingDTO.MovieId; movieTagUser.TagName = taggingDTO.TagName; movieTagUser.UserId = taggingDTO.UserId; movieTagUser.IsUpvote = taggingDTO.IsUpvote; if (_repo.MovieTagUserExists(movieTagUser)) { _repo.UpdateMovieTagUser(movieTagUser); } else { _repo.AddMovieTagUser(movieTagUser); } return(true); }
/// <summary> /// Updates a User's existing Movie Tag in the database. /// </summary> /// <param name="movieTagUser"></param> /// <returns></returns> public void UpdateMovieTagUser(MovieTagUser movieTagUser) { var movieTag = GetMovieTag(movieTagUser.ImdbId, movieTagUser.TagName); if (movieTag == null) { movieTag = new MovieTag(); movieTag.ImdbId = movieTagUser.ImdbId; movieTag.TagName = movieTagUser.TagName; movieTag.VoteSum = 0; _dbContext.MovieTags.Add(movieTag); } bool isUpvote = movieTagUser.IsUpvote ?? true; if (isUpvote) { if (movieTag.VoteSum < int.MaxValue) { movieTag.VoteSum += 1; } } else { if (movieTag.VoteSum > int.MinValue) { movieTag.VoteSum -= 1; } } _dbContext.MovieTagUsers.Update(movieTagUser); _dbContext.SaveChanges(); }
public void TestAddMovieTagUser() { var sut1 = new MovieTagUser() { ImdbId = "Anis", TagName = "Bad", UserId = "12345", IsUpvote = true }; MovieTagUser result1; using (var context2 = new Cinephiliacs_MovieContext(dbOptions)) { context2.Database.EnsureCreated(); var msr = new RepoLogic(context2); msr.AddMovieTagUser(sut1); result1 = context2.MovieTagUsers.FirstOrDefault(ma => ma.ImdbId == "Anis"); } Assert.Equal("Anis", result1.ImdbId); }
/// <summary> /// Adds tags to the movie specified in the argument based on the /// provided list of words and their definitions. /// </summary> /// <param name="words"></param> private async Task AutoTagMovie(List <string> words, string movieId) { foreach (var word in words) { Word dbWord = _repo.GetWord(word); if (dbWord == null) { var wordObject = await ApiHelper.ApiProcessor.LoadDefinitionAsync(word); bool wordIsTag = ProcessWordObject(word, wordObject); if (wordIsTag) { var movieTagUser = new MovieTagUser(); movieTagUser.ImdbId = movieId; movieTagUser.UserId = "~AutoGenerated"; movieTagUser.TagName = wordObject.Word; movieTagUser.IsUpvote = true; if (!_repo.MovieTagUserExists(movieTagUser)) { _repo.AddMovieTagUser(movieTagUser); } } } else { if (dbWord.IsTag) { var movieTagUser = new MovieTagUser(); movieTagUser.ImdbId = movieId; movieTagUser.UserId = "~AutoGenerated"; movieTagUser.TagName = dbWord.BaseWord; movieTagUser.IsUpvote = true; if (!_repo.MovieTagUserExists(movieTagUser)) { _repo.AddMovieTagUser(movieTagUser); } } } } }
/// <summary> /// Adds a User's Tag Vote for a Movie to the database /// and increments the MovieTag's VoteSum. /// Adds the Tag to the database if it does not exist. /// Adds the MovieTag to the database if it does not /// exist. /// </summary> /// <param name="movieTagUser"></param> /// <returns></returns> public void AddMovieTagUser(MovieTagUser movieTagUser) { if (!TagExists(movieTagUser.TagName)) { Tag tag = new Tag(); tag.TagName = movieTagUser.TagName; tag.IsBanned = false; _dbContext.Tags.Add(tag); } var movieTag = GetMovieTag(movieTagUser.ImdbId, movieTagUser.TagName); if (movieTag == null) { movieTag = new MovieTag(); movieTag.ImdbId = movieTagUser.ImdbId; movieTag.TagName = movieTagUser.TagName; movieTag.VoteSum = 0; _dbContext.MovieTags.Add(movieTag); } bool isUpvote = movieTagUser.IsUpvote ?? true; if (isUpvote) { if (movieTag.VoteSum < int.MaxValue) { movieTag.VoteSum += 1; } } else { if (movieTag.VoteSum > int.MinValue) { movieTag.VoteSum -= 1; } } _dbContext.MovieTagUsers.Add(movieTagUser); _dbContext.SaveChanges(); }
/// <summary> /// Returns true iff the MovieTagUser specified by the /// argument exists. /// </summary> /// <param name="movieTagUser"></param> /// <returns></returns> public bool MovieTagUserExists(MovieTagUser movieTagUser) { return(_dbContext.MovieTagUsers.FirstOrDefault(mtu => mtu.ImdbId == movieTagUser.ImdbId && mtu.TagName == movieTagUser.TagName && mtu.UserId == movieTagUser.UserId) != null); }