Exemple #1
0
        public bool Login(string email, string password)
        {
            using var context = new QADbContext(_connectionString);
            User user = context.Users.FirstOrDefault(u => u.Email == email);

            return(BCrypt.Net.BCrypt.Verify(password, user.PasswordHash));
        }
Exemple #2
0
        public void AddQuestion(Question q, List <string> tags)
        {
            using var context = new QADbContext(_connectionString);

            context.Questions.Add(q);
            context.SaveChanges();
            foreach (string name in tags)
            {
                Tag t = GetTag(name);
                int tagId;
                if (t == null)
                {
                    tagId = AddTag(name);
                }
                else
                {
                    tagId = t.Id;
                }
                context.QuestionsTags.Add(new QuestionsTags()
                {
                    QuestionId = q.Id, TagId = tagId
                });
            }
            context.SaveChanges();
        }
Exemple #3
0
        public User GetByEmail(string email)
        {
            using var context = new QADbContext(_connectionString);
            User user = context.Users.FirstOrDefault(u => u.Email == email);

            return(user);
        }
Exemple #4
0
 public void AddUser(User u, string password)
 {
     u.PasswordHash    = BCrypt.Net.BCrypt.HashPassword(password);
     using var context = new QADbContext(_connectionString);
     context.Users.Add(u);
     context.SaveChanges();
 }
Exemple #5
0
 public void AddLike(int questionId, int userId)
 {
     using var context = new QADbContext(_connectionString);
     context.Likes.Add(new Likes {
         QuestionId = questionId, UserId = userId
     });
     context.SaveChanges();
 }
Exemple #6
0
 public List <Question> GetQuestionsForTag(string name)
 {
     using var context = new QADbContext(_connectionString);
     return(context.Questions.Include(q => q.QuestionsTags)
            .ThenInclude(qt => qt.Tag)
            .Include(q => q.Answers)
            .Where(q => q.QuestionsTags.Any(qt => qt.Tag.Name == name))
            .Include(q => q.User)
            .Include(q => q.Likes)
            .OrderByDescending(q => q.DateAsked).ToList());
 }
Exemple #7
0
 public Question GetQuestionById(int id)
 {
     using var context = new QADbContext(_connectionString);
     return(context.Questions
            .Include(q => q.User)
            .Include(q => q.Answers)
            .ThenInclude(a => a.User)
            .Include(q => q.QuestionsTags)
            .ThenInclude(qt => qt.Tag)
            .Include(q => q.Likes)
            .FirstOrDefault(q => q.Id == id));
 }
Exemple #8
0
 public List <Question> GetQuestions()
 {
     using var context = new QADbContext(_connectionString);
     return(context.Questions
            .Include(q => q.User)
            .Include(q => q.QuestionsTags)
            .ThenInclude(qt => qt.Tag)
            .Include(q => q.Answers)
            .ThenInclude(a => a.User)
            .Include(q => q.Likes)
            .OrderByDescending(q => q.DateAsked).ToList());
 }
Exemple #9
0
        private int AddTag(string name)
        {
            using var context = new QADbContext(_connectionString);
            Tag t = new Tag()
            {
                Name = name
            };

            context.Tags.Add(t);
            context.SaveChanges();
            return(t.Id);
        }
Exemple #10
0
 private Tag GetTag(string name)
 {
     using var context = new QADbContext(_connectionString);
     return(context.Tags.FirstOrDefault(t => t.Name == name));
 }
Exemple #11
0
 public void AddAnswer(Answer a)
 {
     using var context = new QADbContext(_connectionString);
     context.Answers.Add(a);
     context.SaveChanges();
 }
Exemple #12
0
 public int GetCurrentLikes(int id)
 {
     using var context = new QADbContext(_connectionString);
     return(context.Likes.Count(l => l.QuestionId == id));
 }