Exemple #1
0
        public void AddLikeForQuestion(int questionId, User user)
        {
            using var context = new QADbContext(_connectionString);
            var like = new Likes
            {
                QuestionId = questionId,
                UserId     = user.Id
            };

            context.Likes.Add(like);
            context.SaveChanges();
        }
Exemple #2
0
        private int AddTag(string text)
        {
            using var context = new QADbContext(_connectionString);
            var newTag = new Tag
            {
                Text = text
            };

            context.Tags.Add(newTag);
            context.SaveChanges();
            return(newTag.Id);
        }
Exemple #3
0
        public void AddAnswer(int questionId, string text, int userId)
        {
            using var context = new QADbContext(_connectionString);
            var answer = new Answer
            {
                Date       = DateTime.Now,
                QuestionId = questionId,
                Text       = text,
                UserId     = userId
            };

            context.Answers.Add(answer);
            context.SaveChanges();
        }
Exemple #4
0
        public void AddQuestion(string title, string text, List <string> tags, int userId)
        {
            using var context = new QADbContext(_connectionString);
            var question = new Question
            {
                Date   = DateTime.Now,
                Text   = text,
                Title  = title,
                UserId = userId
            };

            context.Questions.Add(question);
            context.SaveChanges();
            foreach (string tagText in tags)
            {
                var currentTag = context.Tags.FirstOrDefault(tag => tag.Text == tagText);
                var tagId      = 0;
                if (currentTag == null)
                {
                    tagId = AddTag(tagText);
                    context.SaveChanges();
                    currentTag = context.Tags.FirstOrDefault(t => t.Id == tagId);
                }
                else
                {
                    tagId = currentTag.Id;
                }
                context.QuestionsTags.Add(new QuestionsTags
                {
                    QuestionId = question.Id,
                    Question   = question,
                    TagId      = tagId,
                    Tag        = currentTag
                });
                context.SaveChanges();
            }
        }
        public void AddUser(string email, string password, string name)
        {
            var passwordHashed = BCrypt.Net.BCrypt.HashPassword(password);
            var user           = new User
            {
                Email          = email,
                Likes          = new List <Likes>(),
                Name           = name,
                PasswordHashed = passwordHashed
            };

            using var context = new QADbContext(_connectionString);
            context.Users.Add(user);
            context.SaveChanges();
        }