Ejemplo n.º 1
0
        public void AddQuestion(Question question, IEnumerable <string> tags)
        {
            using var ctx = new QAContext(_connectionString);
            ctx.Questions.Add(question);
            ctx.SaveChanges();
            foreach (string tag in tags)
            {
                Tag t = GetTag(tag);
                int tagId;
                if (t == null)
                {
                    tagId = AddTag(tag);
                }
                else
                {
                    tagId = t.Id;
                }
                ctx.QuestionsTags.Add(new QuestionsTags
                {
                    QuestionId = question.Id,
                    TagId      = tagId
                });
            }

            ctx.SaveChanges();
        }
Ejemplo n.º 2
0
 public List <Question> GetQuestionsForTag(string name)
 {
     using var ctx = new QAContext(_connectionString);
     return(ctx.Questions.Include(q => q.QuestionsTags)
            .ThenInclude(qt => qt.Tag)
            .Where(c => c.QuestionsTags.Any(t => t.Tag.Name == name))
            .ToList());
 }
Ejemplo n.º 3
0
 public List <Question> GetAllQuestions()
 {
     using var ctx = new QAContext(_connectionString);
     return(ctx.Questions.OrderByDescending(q => q.DatePosted).Include(q => q.Likes)
            .Include(q => q.Answers)
            .Include(q => q.QuestionsTags)
            .ThenInclude(qt => qt.Tag)
            .ToList());
 }
Ejemplo n.º 4
0
        public void AddUser(User user, string password)
        {
            string hash = BCrypt.Net.BCrypt.HashPassword(password);

            using var ctx     = new QAContext(_connectionString);
            user.PasswordHash = hash;
            ctx.Users.Add(user);
            ctx.SaveChanges();
        }
Ejemplo n.º 5
0
 public Question GetQuestionById(int id)
 {
     using var ctx = new QAContext(_connectionString);
     return(ctx.Questions.Include(q => q.Answers).Include(q => q.Likes)
            .ThenInclude(a => a.User)
            .Include(q => q.QuestionsTags)
            .ThenInclude(qt => qt.Tag)
            .Include(q => q.User)
            .FirstOrDefault(q => q.Id == id));
 }
Ejemplo n.º 6
0
        private int AddTag(string name)
        {
            using var ctx = new QAContext(_connectionString);
            var tag = new Tag {
                Name = name
            };

            ctx.Tags.Add(tag);
            ctx.SaveChanges();
            return(tag.Id);
        }
Ejemplo n.º 7
0
        public void AddLike(int questionId, int userId)
        {
            using var context = new QAContext(_connectionString);
            var like = new Like
            {
                QuestionId = questionId,
                UserId     = userId
            };

            context.Likes.Add(like);
            context.SaveChanges();
        }
Ejemplo n.º 8
0
 private Tag GetTag(string name)
 {
     using var ctx = new QAContext(_connectionString);
     return(ctx.Tags.FirstOrDefault(t => t.Name == name));
 }
Ejemplo n.º 9
0
 public bool IsEmailAvailable(string email)
 {
     using var ctx = new QAContext(_connectionString);
     return(!ctx.Users.Any(u => u.Email == email));
 }
Ejemplo n.º 10
0
 public User GetByEmail(string email)
 {
     using var ctx = new QAContext(_connectionString);
     return(ctx.Users.Include(u => u.Likes).FirstOrDefault(u => u.Email == email));
 }
Ejemplo n.º 11
0
 public int GetLikes(int id)
 {
     using var context = new QAContext(_connectionString);
     return(context.Likes.Where(l => l.QuestionId == id).Count());
 }
Ejemplo n.º 12
0
 public void AddAnswer(Answer answer)
 {
     using var ctx = new QAContext(_connectionString);
     ctx.Answers.Add(answer);
     ctx.SaveChanges();
 }