コード例 #1
0
 private void AddLike(int jokeId, int userId, bool liked)
 {
     using (var context = new JokesContext(_connectionString))
     {
         var like = new UserLikedJoke
         {
             JokeId    = jokeId,
             UserId    = userId,
             Liked     = liked,
             DateLiked = DateTime.Now
         };
         context.UserLikedJokes.Add(like);
         context.SaveChanges();
     }
 }
コード例 #2
0
 public void Like(int jokeId, int userId, bool liked)
 {
     using (var context = new JokesContext(_connectionString))
     {
         UserLikedJoke likedjoke = context.UserLikedJokes.FirstOrDefault(ulj => ulj.JokeId == jokeId &&
                                                                         ulj.UserId == userId);
         if (likedjoke == null)
         {
             AddLike(jokeId, userId, liked);
         }
         else
         {
             UpdateLike(jokeId, userId, liked);
         }
     }
 }