Ejemplo n.º 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();
            }
        }
Ejemplo n.º 2
0
 public User GetUserByEmail(string email)
 {
     using (var context = new QASiteContext(_connectionString))
     {
         return(context.Users.FirstOrDefault(u => u.Email == email));
     }
 }
Ejemplo n.º 3
0
 private Tag GetTag(string name)
 {
     using (var ctx = new QASiteContext(_connectionString))
     {
         return(ctx.Tags.FirstOrDefault(t => t.Name.ToLower() == name.ToLower()));
     }
 }
Ejemplo n.º 4
0
 public int GetLikes(int questionId)
 {
     using (var context = new QASiteContext(_connectionString))
     {
         return(context.Likes.Where(l => l.QuestionId == questionId).Count());
     }
 }
Ejemplo n.º 5
0
 public void AddAnswer(Answer answer)
 {
     using (var context = new QASiteContext(_connectionString))
     {
         context.Answers.Add(answer);
         context.SaveChanges();
     }
 }
Ejemplo n.º 6
0
 public void LikeQuestion(Like like)
 {
     using (var context = new QASiteContext(_connectionString))
     {
         context.Likes.Add(like);
         context.SaveChanges();
     }
 }
Ejemplo n.º 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();
     }
 }
Ejemplo n.º 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);
     }
 }
Ejemplo n.º 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));
     }
 }
Ejemplo n.º 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());
     }
 }