Exemple #1
0
 public int GetLikesByQuestionId(int id)
 {
     using (var context = new QuestionsTagsContext(_conn))
     {
         return(context.Questions.FirstOrDefault(q => q.Id == id).Likes.Count);
     }
 }
Exemple #2
0
 public IEnumerable <Answer> GetAnswersByQuestionId(int id)
 {
     using (var context = new QuestionsTagsContext(_conn))
     {
         return(context.Answers.Where(a => a.QuestionId == id).ToList());
     }
 }
Exemple #3
0
 public IEnumerable <Question> GetQuestions()
 {
     using (var context = new QuestionsTagsContext(_conn))
     {
         return(context.Questions.OrderByDescending(q => q.DatePosted).ToList());
     }
 }
Exemple #4
0
 public Question GetQuestionById(int id)
 {
     using (var context = new QuestionsTagsContext(_conn))
     {
         return(context.Questions.Include(t => t.QuestionsTags).Include(l => l.Likes).Include(a => a.Answers).FirstOrDefault(q => q.Id == id));
     }
 }
Exemple #5
0
 private Tag GetTag(string name)
 {
     using (var context = new QuestionsTagsContext(_conn))
     {
         return(context.Tags.FirstOrDefault(t => t.Name == name));
     }
 }
Exemple #6
0
 public void AddQuestion(Question q, IEnumerable <string> tags)
 {
     using (var context = new QuestionsTagsContext(_conn))
     {
         context.Questions.Add(q);
         foreach (string tag in tags)
         {
             Tag t = GetTag(tag);
             int tagId;
             if (t == null)
             {
                 tagId = AddTag(tag);
             }
             else
             {
                 tagId = t.Id;
             }
             context.QuestionsTags.Add(new QuestionsTags
             {
                 QuestionId = q.Id,
                 TagId      = tagId
             });
         }
         context.SaveChanges();
     }
 }
Exemple #7
0
 public User GetUser(User user)
 {
     using (var context = new QuestionsTagsContext(_conn))
     {
         return(context.Users.FirstOrDefault(u => u.Email == user.Email));
     }
 }
Exemple #8
0
 public void AddAnswer(Answer answer)
 {
     using (var context = new QuestionsTagsContext(_conn))
     {
         context.Answers.Add(answer);
         context.SaveChanges();
     }
 }
Exemple #9
0
 public bool DidntLikeYet(int questionId, int userId)
 {
     using (var context = new QuestionsTagsContext(_conn))
     {
         var question = context.Questions.Include(l => l.Likes).FirstOrDefault(q => q.Id == questionId);
         return(!question.Likes.Any(l => l.UserId == userId));
     }
 }
Exemple #10
0
 public User GetUserByEmail(string email)
 {
     using (var context = new QuestionsTagsContext(_conn))
     {
         var user = context.Users.FirstOrDefault(u => u.Email == email);
         return(user);
     }
 }
Exemple #11
0
 public IEnumerable <Tag> GetTagsByQuestion(IEnumerable <QuestionsTags> ids)
 {
     using (var context = new QuestionsTagsContext(_conn))
     {
         List <Tag> tags = new List <Tag>();
         foreach (QuestionsTags qt in ids)
         {
             tags.Add(context.Tags.FirstOrDefault(t => t.Id == qt.TagId));
         }
         return(tags);
     }
 }
Exemple #12
0
 private int AddTag(string name)
 {
     using (var context = new QuestionsTagsContext(_conn))
     {
         var tag = new Tag {
             Name = name
         };
         context.Tags.Add(tag);
         context.SaveChanges();
         return(tag.Id);
     }
 }
Exemple #13
0
 public IEnumerable <Tag> GetTagsByQuestionId(int id)
 {
     using (var context = new QuestionsTagsContext(_conn))
     {
         List <Tag>           tags = new List <Tag>();
         List <QuestionsTags> qsts = context.Questions.Include(q => q.QuestionsTags).FirstOrDefault(q => q.Id == id).QuestionsTags.ToList();
         foreach (QuestionsTags qt in qsts)
         {
             tags.Add(qt.Tag);
         }
         return(tags);
     }
 }
Exemple #14
0
 public void AddUser(User u)
 {
     using (var context = new QuestionsTagsContext(_conn))
     {
         var user = new User
         {
             Name     = u.Name,
             Email    = u.Email,
             Password = HashPassword(u.Password)
         };
         context.Users.Add(user);
         context.SaveChanges();
     }
 }
Exemple #15
0
        public void AddLike(int questionId, User u)
        {
            using (var context = new QuestionsTagsContext(_conn))
            {
                var   user = GetUser(u);
                Likes like = new Likes
                {
                    UserId = user.Id,
                    //User = user,
                    QuestionId = questionId,
                    //Question = context.Questions.FirstOrDefault(q => q.Id == questionId)
                };
                context.Likes.Add(like);

                context.SaveChanges();
            }
        }