public int GetLikesForQuestion(int questionId) { using var context = new QADbContext(_connectionString); var question = context.Questions.Include(q => q.Likes).ThenInclude(l => l.User).FirstOrDefault(q => q.Id == questionId); return(question.Likes.Count); }
public bool CanLike(int userId) { using var context = new QADbContext(_connectionString); var like = context.Likes.FirstOrDefault(l => l.UserId == userId); return(like == null ? true : false); }
public void AddLikeForQuestion(int questionId, User user) { using var context = new QADbContext(_connectionString); var like = new Likes { QuestionId = questionId, UserId = user.Id }; context.Likes.Add(like); context.SaveChanges(); }
private int AddTag(string text) { using var context = new QADbContext(_connectionString); var newTag = new Tag { Text = text }; context.Tags.Add(newTag); context.SaveChanges(); return(newTag.Id); }
public User AuthorizeUser(string email, string password) { using var context = new QADbContext(_connectionString); var user = context.Users.FirstOrDefault(u => u.Email == email); if (user == null) { return(null); } bool isValid = BCrypt.Net.BCrypt.Verify(password, user.PasswordHashed); return(isValid ? user : null); }
public void AddAnswer(int questionId, string text, int userId) { using var context = new QADbContext(_connectionString); var answer = new Answer { Date = DateTime.Now, QuestionId = questionId, Text = text, UserId = userId }; context.Answers.Add(answer); context.SaveChanges(); }
public void AddUser(string email, string password, string name) { var passwordHashed = BCrypt.Net.BCrypt.HashPassword(password); var user = new User { Email = email, Likes = new List <Likes>(), Name = name, PasswordHashed = passwordHashed }; using var context = new QADbContext(_connectionString); context.Users.Add(user); context.SaveChanges(); }
public void AddQuestion(string title, string text, List <string> tags, int userId) { using var context = new QADbContext(_connectionString); var question = new Question { Date = DateTime.Now, Text = text, Title = title, UserId = userId }; context.Questions.Add(question); context.SaveChanges(); foreach (string tagText in tags) { var currentTag = context.Tags.FirstOrDefault(tag => tag.Text == tagText); var tagId = 0; if (currentTag == null) { tagId = AddTag(tagText); context.SaveChanges(); currentTag = context.Tags.FirstOrDefault(t => t.Id == tagId); } else { tagId = currentTag.Id; } context.QuestionsTags.Add(new QuestionsTags { QuestionId = question.Id, Question = question, TagId = tagId, Tag = currentTag }); context.SaveChanges(); } }
public Question GetQuestionById(int id) { using var context = new QADbContext(_connectionString); return(context.Questions.Include(q => q.Answers).ThenInclude(a => a.user).Include(q => q.QuestionsTags).ThenInclude(qt => qt.Tag).Include(q => q.User).FirstOrDefault(q => q.Id == id)); }
public List <Question> GetAllQuestions() { using var context = new QADbContext(_connectionString); return(context.Questions.OrderByDescending(q => q.Date).Include(q => q.Likes).Include(q => q.Answers).Include(q => q.QuestionsTags).ThenInclude(qt => qt.Tag).ToList()); }
public User GetUser(string email) { using var context = new QADbContext(_connectionString); return(context.Users.FirstOrDefault(u => u.Email == email)); }