Beispiel #1
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();
            }
        }
Beispiel #2
0
 public User GetUserByEmail(string email)
 {
     using (var context = new QASiteContext(_connectionString))
     {
         return(context.Users.FirstOrDefault(u => u.Email == email));
     }
 }
Beispiel #3
0
 private Tag GetTag(string name)
 {
     using (var ctx = new QASiteContext(_connectionString))
     {
         return(ctx.Tags.FirstOrDefault(t => t.Name.ToLower() == name.ToLower()));
     }
 }
Beispiel #4
0
 public int GetLikes(int questionId)
 {
     using (var context = new QASiteContext(_connectionString))
     {
         return(context.Likes.Where(l => l.QuestionId == questionId).Count());
     }
 }
Beispiel #5
0
 public void AddAnswer(Answer answer)
 {
     using (var context = new QASiteContext(_connectionString))
     {
         context.Answers.Add(answer);
         context.SaveChanges();
     }
 }
Beispiel #6
0
 public void LikeQuestion(Like like)
 {
     using (var context = new QASiteContext(_connectionString))
     {
         context.Likes.Add(like);
         context.SaveChanges();
     }
 }
Beispiel #7
0
 public void AddUser(User user, string password)
 {
     user.PasswordHash = BCrypt.Net.BCrypt.HashPassword(password);
     using (var context = new QASiteContext(_connectionString))
     {
         context.Users.Add(user);
         context.SaveChanges();
     }
 }
Beispiel #8
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);
     }
 }
Beispiel #9
0
 public Question GetQuestionById(int id)
 {
     using (var context = new QASiteContext(_connectionString))
     {
         return(context.Questions
                .Include(q => q.Answers)
                .ThenInclude(a => a.User)
                .Include(q => q.QuestionsTags)
                .ThenInclude(qt => qt.Tag)
                .Include(q => q.Likes)
                .Include(q => q.User)
                .FirstOrDefault(q => q.Id == id));
     }
 }
Beispiel #10
0
 public IEnumerable <Question> GetQuestions()
 {
     using (var context = new QASiteContext(_connectionString))
     {
         return(context.Questions
                .Include(q => q.Answers)
                .ThenInclude(a => a.User)
                .Include(q => q.QuestionsTags)
                .ThenInclude(qt => qt.Tag)
                .Include(q => q.Likes)
                .Include(q => q.User)
                .OrderByDescending(q => q.DatePosted)
                .ToList());
     }
 }