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