public void AddQuestion(Question question, List <string> tagList)
 {
     using (var context = new QuestionTagContext(_connectionString))
     {
         context.Questions.Add(question);
         context.SaveChanges();
         foreach (string tag in tagList)
         {
             Tag t = GetTagByName(tag);
             int tagId;
             if (t == null)
             {
                 tagId = AddTagAndReturnId(tag);
             }
             else
             {
                 tagId = t.Id;
             }
             context.QuestionsTags.Add(new QuestionsTags
             {
                 QuestionId = question.Id,
                 TagId      = tagId
             });
         }
         context.SaveChanges();
     }
 }
 public int GetCurrentLikes(int questionId)
 {
     using (var context = new QuestionTagContext(_connectionString))
     {
         return(context.LikesQuestions.Where(lq => lq.QuestionId == questionId).Count());
     }
 }
 public User GetUserByEmail(string email)
 {
     using (var context = new QuestionTagContext(_connectionString))
     {
         return(context.Users.FirstOrDefault((u => u.Email == email)));
     }
 }
 public List <Answer> GetAnswersForQuestion(int questionId)
 {
     using (var context = new QuestionTagContext(_connectionString))
     {
         return(context.Answers.Where(a => a.QuestionId == questionId).ToList());
     }
 }
 private Tag GetTagByName(string name)
 {
     using (var context = new QuestionTagContext(_connectionString))
     {
         return(context.Tags.FirstOrDefault(t => t.Name == name));
     }
 }
 public bool IsEmailAvailable(string email)
 {
     using (var context = new QuestionTagContext(_connectionString))
     {
         bool isAvailable = !context.Users.Any(u => u.Email == email);
         return(isAvailable);
     }
 }
 public void AddAnswer(Answer answer)
 {
     using (var context = new QuestionTagContext(_connectionString))
     {
         context.Answers.Add(answer);
         context.SaveChanges();
     }
 }
 public List <Question> GetQuestions()
 {
     using (var context = new QuestionTagContext(_connectionString))
     {
         return(context.Questions.Include(q => q.LikesQuestions)
                .Include(q => q.QuestionsTags).ThenInclude(qt => qt.Tag)
                .OrderByDescending(q => q.DatePosted).ToList());
     }
 }
 public Question GetQuestionById(int questionId)
 {
     using (var context = new QuestionTagContext(_connectionString))
     {
         return(context.Questions
                .Include(q => q.LikesQuestions)
                .Include(q => q.QuestionsTags).ThenInclude(qt => qt.Tag)
                .Include(q => q.Answers).ThenInclude(a => a.User)
                .FirstOrDefault(q => q.Id == questionId));
     }
 }
 public void LikeQuestion(LikesQuestions lq)
 {
     using (var context = new QuestionTagContext(_connectionString))
     {
         context.LikesQuestions.Add(new LikesQuestions
         {
             QuestionId = lq.QuestionId,
             UserId     = lq.UserId
         });
         context.SaveChanges();
     }
 }
 private int AddTagAndReturnId(string name)
 {
     using (var context = new QuestionTagContext(_connectionString))
     {
         var tag = new Tag {
             Name = name
         };
         context.Tags.Add(tag);
         context.SaveChanges();
         return(tag.Id);
     }
 }
        public void AddUser(string email, string password)
        {
            string hash = BCrypt.Net.BCrypt.HashPassword(password);
            User   user = new User {
                Email = email, PasswordHash = hash
            };

            using (var context = new QuestionTagContext(_connectionString))
            {
                context.Users.Add(user);
                context.SaveChanges();
            }
        }