Ejemplo n.º 1
0
 public IEnumerable <Joke> GetJokes()
 {
     using (var context = new JokesContext(_conn))
     {
         return(context.Jokes.Include(j => j.UserLikedJokes).ToList());
     }
 }
Ejemplo n.º 2
0
 public User GetUserByEmail(string email)
 {
     using (var context = new JokesContext(_conn))
     {
         return(context.Users.FirstOrDefault(u => u.Email == email));
     }
 }
Ejemplo n.º 3
0
 public bool DidUserDislike(int userId, int jokeId)
 {
     using (var context = new JokesContext(_conn))
     {
         return(context.UserLikedJokes.Any(l => l.UserId == userId &&
                                           l.JokeId == jokeId &&
                                           l.Liked == false));
     }
 }
Ejemplo n.º 4
0
 public void UpdateLike(int userId, int jokeId, bool like)
 {
     using (var context = new JokesContext(_conn))
     {
         context.Database.ExecuteSqlCommand(
             "UPDATE UserLikedJokes SET Liked=@liked WHERE JokeId=@jokeId AND UserId=@userId",
             new SqlParameter("@liked", like),
             new SqlParameter("@jokeId", jokeId),
             new SqlParameter("@userId", userId));
     }
 }
Ejemplo n.º 5
0
 public Joke GenerateRandomJoke()
 {
     using (var context = new JokesContext(_conn))
     {
         var    client = new HttpClient();
         string url    = "https://official-joke-api.appspot.com/jokes/programming/random";
         string json   = client.GetStringAsync(url).Result;
         var    result = JsonConvert.DeserializeObject <IEnumerable <Joke> >(json).First();
         context.Jokes.Add(result);
         context.SaveChanges();
         return(result);
     }
 }
Ejemplo n.º 6
0
 public void AddUser(User u)
 {
     using (var context = new JokesContext(_conn))
     {
         var user = new User
         {
             Name     = u.Name,
             Email    = u.Email,
             Password = HashPassword(u.Password)
         };
         context.Users.Add(user);
         context.SaveChanges();
     }
 }
Ejemplo n.º 7
0
 public int GetDislikesByJokeId(int id)
 {
     using (var context = new JokesContext(_conn))
     {
         var dislikes = context.Jokes.Include(j => j.UserLikedJokes).FirstOrDefault(j => j.Id == id).UserLikedJokes.Where(ulj => ulj.Liked == false);
         if (dislikes == null)
         {
             return(0);
         }
         else
         {
             return(dislikes.Count());
         }
     }
 }
Ejemplo n.º 8
0
 public bool CanStillLike(UserLikedJokes ulj)
 {
     using (var context = new JokesContext(_conn))
     {
         var like = context.UserLikedJokes.FirstOrDefault(l => l.UserId == ulj.UserId &&
                                                          l.JokeId == ulj.JokeId);
         if (like != null)
         {
             return(like.Time.AddMinutes(5) > DateTime.Now);
         }
         else
         {
             return(true);
         }
     }
 }
Ejemplo n.º 9
0
 public void DislikeJoke(int userId, int jokeId)
 {
     using (var context = new JokesContext(_conn))
     {
         if (context.UserLikedJokes.Any(l => l.UserId == userId && l.JokeId == jokeId))
         {
             UpdateLike(userId, jokeId, false);
         }
         else
         {
             context.UserLikedJokes.Add(new UserLikedJokes
             {
                 UserId = userId,
                 JokeId = jokeId,
                 //Liked = false,
                 Time = DateTime.Now
             });
             //UpdateLike(userId, jokeId, false);
             context.SaveChanges();
             UpdateLike(userId, jokeId, false);
         }
     }
 }