Exemplo n.º 1
0
 public int GetQuestionLikes(int questionId)
 {
     using (var context = new QASiteContext(_connectionString))
     {
         return(context.QuestionLikes.Count(q => q.QuestionId == questionId));
     }
 }
Exemplo n.º 2
0
        public void AddQuestion(Question question, IEnumerable <string> tags)
        {
            using (var ctx = new QASiteContext(_connectionString))
            {
                ctx.Questions.Add(question);
                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();
            }
        }
Exemplo n.º 3
0
 private Tag GetTag(string name)
 {
     using (var ctx = new QASiteContext(_connectionString))
     {
         return(ctx.Tags.FirstOrDefault(t => t.Name == name));
     }
 }
Exemplo n.º 4
0
 public User GetByEmail(string email)
 {
     using (var context = new QASiteContext(_connectionString))
     {
         return(context.Users.FirstOrDefault(u => u.Email == email));
     }
 }
Exemplo n.º 5
0
 public void AddAnswer(Answer answer)
 {
     using (var context = new QASiteContext(_connectionString))
     {
         context.Answers.Add(answer);
         context.SaveChanges();
     }
 }
Exemplo n.º 6
0
        public void AddUser(User user, string password)
        {
            user.PasswordHash = PasswordHelper.HashPassword(password);

            using (var context = new QASiteContext(_connectionString))
            {
                context.Users.Add(user);
                context.SaveChanges();
            }
        }
Exemplo n.º 7
0
 private int AddTag(string name)
 {
     using (var ctx = new QASiteContext(_connectionString))
     {
         var tag = new Tag {
             Name = name
         };
         ctx.Tags.Add(tag);
         ctx.SaveChanges();
         return(tag.Id);
     }
 }
Exemplo n.º 8
0
 public void AddQuestionLike(int questionId, int userId)
 {
     using (var context = new QASiteContext(_connectionString))
     {
         var like = new QuestionLike
         {
             QuestionId = questionId,
             UserId     = userId
         };
         context.QuestionLikes.Add(like);
         context.SaveChanges();
     }
 }
Exemplo n.º 9
0
 public IEnumerable <Question> Get()
 {
     using (var context = new QASiteContext(_connectionString))
     {
         return(context.Questions
                .Include(q => q.Answers)
                .Include(q => q.Likes)
                .Include(q => q.QuestionsTags)
                .ThenInclude(q => q.Tag)
                .OrderByDescending(q => q.Date)
                .ToList());
     }
 }
Exemplo n.º 10
0
 public Question Get(int id)
 {
     using (var context = new QASiteContext(_connectionString))
     {
         return(context.Questions
                .Include(q => q.User)
                .ThenInclude(q => q.LikedQuestions)
                .Include(q => q.Answers)
                .ThenInclude(a => a.User)
                .Include(q => q.Likes)
                .Include(u => u.QuestionsTags)
                .ThenInclude(qt => qt.Tag)
                .FirstOrDefault(q => q.Id == id));
     }
 }