Example #1
0
 public int GetLikesByQuestionId(int id)
 {
     using (var context = new QuestionsTagsContext(_conn))
     {
         return(context.Questions.FirstOrDefault(q => q.Id == id).Likes.Count);
     }
 }
Example #2
0
 public IEnumerable <Answer> GetAnswersByQuestionId(int id)
 {
     using (var context = new QuestionsTagsContext(_conn))
     {
         return(context.Answers.Where(a => a.QuestionId == id).ToList());
     }
 }
Example #3
0
 public IEnumerable <Question> GetQuestions()
 {
     using (var context = new QuestionsTagsContext(_conn))
     {
         return(context.Questions.OrderByDescending(q => q.DatePosted).ToList());
     }
 }
Example #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));
     }
 }
Example #5
0
 private Tag GetTag(string name)
 {
     using (var context = new QuestionsTagsContext(_conn))
     {
         return(context.Tags.FirstOrDefault(t => t.Name == name));
     }
 }
Example #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();
     }
 }
Example #7
0
 public User GetUser(User user)
 {
     using (var context = new QuestionsTagsContext(_conn))
     {
         return(context.Users.FirstOrDefault(u => u.Email == user.Email));
     }
 }
Example #8
0
 public void AddAnswer(Answer answer)
 {
     using (var context = new QuestionsTagsContext(_conn))
     {
         context.Answers.Add(answer);
         context.SaveChanges();
     }
 }
Example #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));
     }
 }
Example #10
0
 public User GetUserByEmail(string email)
 {
     using (var context = new QuestionsTagsContext(_conn))
     {
         var user = context.Users.FirstOrDefault(u => u.Email == email);
         return(user);
     }
 }
Example #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);
     }
 }
Example #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);
     }
 }
Example #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);
     }
 }
Example #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();
     }
 }
Example #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();
            }
        }