コード例 #1
0
 public void AddQuestion(Question question, List <string> tags)
 {
     using (var context = new StackOverflowContext(_connectionString))
     {
         context.Questions.Add(question);
         foreach (var tag in tags)
         {
             var tagFromDB = context.Tags.FirstOrDefault(t => t.Text == tag);
             int tagID;
             if (tagFromDB == null)
             {
                 var newTag = new Tag {
                     Text = tag
                 };
                 context.Tags.Add(newTag);
                 context.SaveChanges();
                 tagID = newTag.ID;
             }
             else
             {
                 context.SaveChanges();
                 tagID = tagFromDB.ID;
             }
             context.QuestionsTags.Add(new QuestionsTags
             {
                 QuestionID = question.ID,
                 TagID      = tagID
             });;
         }
         context.SaveChanges();
     }
 }
コード例 #2
0
 public void AddAnswer(Answer answer)
 {
     using (var context = new StackOverflowContext(_connectionString))
     {
         context.Answers.Add(answer);
         context.SaveChanges();
     }
 }
コード例 #3
0
 public void LikeAnswer(LikedAnswers like)
 {
     using (var context = new StackOverflowContext(_connectionString))
     {
         context.LikedAnswers.Add(like);
         context.SaveChanges();
     }
 }
コード例 #4
0
 public void LikeQuestion(LikedQuestions like)
 {
     using (var context = new StackOverflowContext(_connectionString))
     {
         context.LikedQuestions.Add(like);
         context.SaveChanges();
     }
 }
コード例 #5
0
 public List <Answer> GetAnswers(int id)
 {
     using (var context = new StackOverflowContext(_connectionString))
     {
         return(context.Answers.Include(a => a.User).Include(a => a.LikedAnswers)
                .Where(a => a.QuestionID == id).ToList());
     }
 }
コード例 #6
0
 public Question GetQuestion(int id)
 {
     using (var context = new StackOverflowContext(_connectionString))
     {
         return(context.Questions.Include(q => q.LikedQuestions).Include(q => q.QuestionsTags).ThenInclude(qt => qt.Tag)
                .FirstOrDefault(q => q.ID == id));
     }
 }
コード例 #7
0
 public List <Question> GetQuestions()
 {
     using (var context = new StackOverflowContext(_connectionString))
     {
         return(context.Questions.Include(q => q.LikedQuestions).
                Include(q => q.QuestionsTags).ThenInclude(qt => qt.Tag).OrderByDescending(o => o.DatePosted).ToList());
     }
 }
コード例 #8
0
 public int GetUserID(string email)
 {
     using (var context = new StackOverflowContext(_connectionString))
     {
         var user = context.Users.FirstOrDefault(u => u.Email == email);
         return(user == null ? 0 : user.ID);
     }
 }
コード例 #9
0
 public void AddUser(User user, string password)
 {
     using (var context = new StackOverflowContext(_connectionString))
     {
         user.PasswordHash = BCrypt.Net.BCrypt.HashPassword(password);
         context.Users.Add(user);
         context.SaveChanges();
     }
 }
コード例 #10
0
 public bool Login(string email, string password)
 {
     using (var context = new StackOverflowContext(_connectionString))
     {
         if (context.Users.FirstOrDefault(u => u.Email == email) == null)
         {
             return(false);
         }
         return(BCrypt.Net.BCrypt.Verify(password, context.Users
                                         .FirstOrDefault(u => u.Email == email).PasswordHash ?? ""));
     }
 }