public List <Joke> ViewJokes() { using var context = new JokesDbContext(_connectionString); var jokes = context.Jokes.Include(j => j.UserLikedJokes).ToList(); return(jokes); }
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)); }
public Joke GetJokeById(int id) { using var context = new JokesDbContext(_connectionString); return(context.Jokes.Include(j => j.UserLikedJokes).FirstOrDefault(j => j.Id == id)); }
public User GetByEmail(string email) { using var ctx = new JokesDbContext(_connectionString); return(ctx.Users.FirstOrDefault(u => u.Email == email)); }