public void AddUser(User user, string password) { string hash = BCrypt.Net.BCrypt.HashPassword(password); using var ctx = new JokesDbContext(_connectionString); user.PasswordHash = hash; ctx.Users.Add(user); ctx.SaveChanges(); }
public void LikeJoke(UserLikedJokes liked) { using var context = new JokesDbContext(_connectionString); if (context.UserLikedJokes.Any(ulj => ulj.UserId == liked.UserId && ulj.JokeId == liked.JokeId)) { context.Database.ExecuteSqlInterpolated(@$ "UPDATE UserLikedJokes SET Liked={liked.Liked}, Date=GETDATE() WHERE UserId={liked.UserId} AND JokeId={liked.JokeId}"); return; } context.UserLikedJokes.Add(liked); context.SaveChanges(); }
public Joke GetRandomJoke() { var client = new HttpClient(); var json = client.GetStringAsync( "https://official-joke-api.appspot.com/jokes/programming/random") .Result; var joke = JsonConvert.DeserializeObject <List <Joke> >(json).First(); using var context = new JokesDbContext(_connectionString); joke.Id = 0; if (!context.Jokes.Any(j => j.Punchline == joke.Punchline)) { context.Jokes.Add(joke); context.SaveChanges(); } return(context.Jokes.Include(j => j.UserLikedJokes).FirstOrDefault(j => j.Punchline == joke.Punchline)); }