public void AddJoke(Joke joke)
 {
     using (var context = new JokesContext(_connectionString))
     {
         context.Jokes.Add(joke);
         context.SaveChanges();
     }
 }
示例#2
0
        public void AddUser(User user, string password)
        {
            user.PasswordHash = BCrypt.Net.BCrypt.HashPassword(password);

            using (var context = new JokesContext(_connectionString))
            {
                context.Users.Add(user);
                context.SaveChanges();
            }
        }
        public void InteractWithJoke(int userId, int jokeId, bool like)
        {
            using (var context = new JokesContext(_connectionString))
            {
                var userLike = context.UserJokeLikes.FirstOrDefault(u => u.UserId == userId && u.JokeId == jokeId);
                if (userLike == null)
                {
                    context.UserJokeLikes.Add(new UserJokeLike
                    {
                        UserId = userId,
                        JokeId = jokeId,
                        Like   = like,
                        Date   = DateTime.Now
                    });
                }
                else
                {
                    userLike.Like = like;
                    userLike.Date = DateTime.Now;
                }

                context.SaveChanges();
            }
        }