Beispiel #1
0
        /// <summary>
        /// Add the answer to Database
        /// </summary>
        /// <param name="data">Answer</param>
        public static void AddAnswer(Answer data, int questionID, int userid)
        {
            using (var db = new QaAContext())
            {
                data.Date       = DateTime.Now;
                data.UserId     = userid;
                data.QuestionId = questionID;
                db.Answers.Add(data);
                db.SaveChanges();

                HttpContext.Current.Cache.UpdateCache("AnsweredQuestions" + userid, data);
                HttpContext.Current.Cache.UpdateCache("AnswersToQuestion" + questionID, data);
                HttpContext.Current.Cache.UpdateCache("AnswersToQuestion" + questionID + "User" + userid, data);

                //Send mails to the subscripted users
                var         question       = db.Questions.Where(q => q.Id == questionID).SingleOrDefault();
                IUserMailer usermailer     = new UserMailer();
                var         tagsOfQuestion = question.QuestionHasTags.Select(s => s.TagId).ToList();
                var         tos            = db.UserProfiles.Where(up => (up.Subscriptions.Any(u => tagsOfQuestion.Any(x => x == u.TagId)) && up.Email != null && up.IsVerified == true)).ToList();

                foreach (var user in tos)
                {
                    usermailer.NewAnswer(question, user, data).SendAsync();
                }
            }
        }
Beispiel #2
0
 /// <summary>
 /// Get the Tag by tagname
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public static Tag GetTagByName(string name)
 {
     using (var db = new QaAContext())
     {
         var q = (from t in db.Tags where t.Name == name select t).SingleOrDefault();
         return(q);
     }
 }
Beispiel #3
0
 /// <summary>
 /// Get the title based on key (to Ajax request)
 /// </summary>
 /// <param name="key">keyparameter</param>
 /// <returns></returns>
 public static List <Question> AjaxSearchTitle(string key)
 {
     using (var db = new QaAContext())
     {
         var questions = (from q in db.Questions where q.Title.Contains(key) select q).ToList();
         return(questions);
     }
 }
Beispiel #4
0
 /// <summary>
 /// Get the x number of the latest questions
 /// </summary>
 /// <param name="x">Number of the question</param>
 /// <returns></returns>
 public static List <Question> GetXLatestQuestion(int x)
 {
     using (var db = new QaAContext())
     {
         var q = HttpContext.Current.Cache.GetFromCache("LatestQuestions", () => db.Questions.OrderByDescending(v => v.Date).Take(x).ToList());
         return(q);
     }
 }
Beispiel #5
0
 /// <summary>
 /// Get the questions based on Question ID
 /// </summary>
 /// <param name="id"> Question ID</param>
 /// <returns></returns>
 public static Question GetQuestion(int id)
 {
     using (var db = new QaAContext())
     {
         var ques = HttpContext.Current.Cache.GetFromCache("GetQuestion" + id, () => (db.Questions.Where(q => q.Id == id).SingleOrDefault()));
         return(ques);
     }
 }
Beispiel #6
0
 /// <summary>
 ///Get the all questions from chronology(descending)
 /// </summary>
 /// <returns></returns>
 public static List <Question> GetAllQuestion()
 {
     using (var db = new QaAContext())
     {
         var q = HttpContext.Current.Cache.GetFromCache("AllQuestions", () => (from qs in db.Questions select qs).OrderByDescending(v => v.Date).ToList());
         return(q);
     }
 }
Beispiel #7
0
 /// <summary>
 /// Get the tags based on question ID
 /// </summary>
 /// <param name="id">question Id</param>
 /// <returns></returns>
 public static List <Tag> GetAllTagToOneQuestion(int id)
 {
     using (var db = new QaAContext())
     {
         var q = db.QuestionHasTags.Where(t => t.QuestionId == id).Select(t => t.Tag).ToList();
         return(q);
     }
 }
Beispiel #8
0
 /// <summary>
 /// List 6 tag to one user.
 /// </summary>
 /// <param name="userid"></param>
 /// <returns></returns>
 public static List <Tag> SixTagToOneUser(int userid)
 {
     using (var db = new QaAContext())
     {
         var q = db.UserHasSubscribes.Where(uhs => uhs.UserId == userid).Select(t => t.Tag).Take(6).ToList();
         return(q);
     }
 }
Beispiel #9
0
 /// <summary>
 /// Get the list of tags, where the tag starts with tagpart
 /// </summary>
 /// <param name="tagpart"></param>
 /// <returns></returns>
 public static List <Tag> GetStartwithTag(string tagpart)
 {
     using (var db = new QaAContext())
     {
         var q = (from t in db.Tags where t.Name.StartsWith(tagpart) select t).ToList();
         return(q);
     }
 }
Beispiel #10
0
 /// <summary>
 /// List the all tag
 /// </summary>
 /// <returns></returns>
 public static List <Tag> GetAllTag()
 {
     using (var db = new QaAContext())
     {
         var q = HttpContext.Current.Cache.GetFromCache("Tags", () => (from t in db.Tags select t).ToList());
         return(q);
     }
 }
Beispiel #11
0
 /// <summary>
 /// Get all subcribed tag's count to one user
 /// </summary>
 /// <param name="userid"></param>
 /// <returns></returns>
 public static int AllSubcribeTagsCountToOneUser(int userid)
 {
     using (var db = new QaAContext())
     {
         var q = db.UserHasSubscribes.Where(uhs => uhs.UserId == userid).Select(t => t.Tag).ToList();
         return(q.Count);
     }
 }
Beispiel #12
0
 /// <summary>
 /// Get the number of all answers from the id of the questions
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public static int GetAllAnswerNumberToOneQuestion(int id)
 {
     using (var db = new QaAContext())
     {
         var q = HttpContext.Current.Cache.GetFromCache("AnswersToQuestion" + id, () => (from ans in db.Answers where ans.QuestionId == id select ans).ToList()).Count();
         return(q);
     }
 }
Beispiel #13
0
 /// <summary>
 /// List all subcribed user's count to one tag
 /// </summary>
 /// <param name="tagid"></param>
 /// <returns></returns>
 public static int AllSubcribeUsersCountToOneTag(int tagid)
 {
     using (var db = new QaAContext())
     {
         var q = db.UserHasSubscribes.Where(uhs => uhs.TagId == tagid).Select(u => u.UserProfile).ToList();
         return(q.Count);
     }
 }
Beispiel #14
0
        /// <summary>
        /// Get the answer from the id
        /// </summary>
        /// <param name="id">Answer's id</param>
        /// <returns></returns>
        public static Answer GetAnswer(int id)
        {
            using (var db = new QaAContext())
            {
                var ans = HttpContext.Current.Cache.GetFromCache("GetAnswer" + id, () => db.Answers.Where(a => a.Id == id).SingleOrDefault());

                return(ans);
            }
        }
Beispiel #15
0
 /// <summary>
 /// Get the reputation of the user (Reputation=Rating of User's Questions + Rating of User's Answers)
 /// </summary>
 /// <param name="userid">User Id</param>
 /// <returns></returns>
 public static int GetUserRating(int userid)
 {
     using (var db = new QaAContext())
     {
         var qrating = db.QuestionHasVotes.Where(qhv => qhv.Question.UserId == userid).Select(s => s.Rating).ToList();
         var arating = db.AnswerHasVotes.Where(ahv => ahv.Answer.UserId == userid).Select(s => s.Rating).ToList();
         return((qrating == null ? 0 : qrating.Sum()) + (arating == null ? 0 : arating.Sum()));
     }
 }
Beispiel #16
0
        public static EmailIdentifier GetEmailDatasByHash(string hash)
        {
            using (var db = new QaAContext())
            {
                var data = db.EmailIdentifiers.Where(ei => ei.Hash == hash).FirstOrDefault();

                return(data);
            }
        }
Beispiel #17
0
 /// <summary>
 /// Get the UserProfil based on ID
 /// </summary>
 /// <param name="id">User Id</param>
 /// <returns></returns>
 public static UserProfile GetUserById(int id)
 {
     using (var db = new QaAContext())
     {
         var q = (from u in db.UserProfiles
                  where u.UserId == id
                  select u).SingleOrDefault();
         return(q);
     }
 }
Beispiel #18
0
 /// <summary>
 /// Add email to the user
 /// </summary>
 /// <param name="up">UserProfile</param>
 public static void AddEmail(UserProfile up)
 {
     using (var db = new QaAContext())
     {
         var user = db.UserProfiles.Where(u => u.UserId == up.UserId).SingleOrDefault();
         user.Email      = up.Email;
         user.IsVerified = false;
         db.SaveChanges();
     }
 }
Beispiel #19
0
        /// <summary>
        /// List the all user
        /// </summary>
        /// <returns></returns>
        public static List <UserProfile> GetAllUsers()
        {
            using (var db = new QaAContext())
            {
                var q = from u in db.UserProfiles
                        select u;

                return(q.ToList());
            }
        }
Beispiel #20
0
 /// <summary>
 /// Get the tag based on ID
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public static Tag GetTagById(int id)
 {
     using (var db = new QaAContext())
     {
         var q = HttpContext.Current.Cache.GetFromCache("GetTag" + id, () => (from t in db.Tags
                                                                              where t.Id == id
                                                                              select t).SingleOrDefault());
         return(q);
     }
 }
Beispiel #21
0
 /// <summary>
 /// Get all answer from one questions of the user
 /// </summary>
 /// <param name="questionid">Question ID</param>
 /// <param name="userid">User ID</param>
 /// <returns></returns>
 public static List <Answer> GetAllAnswerToOneQuestionFromOneUser(int questionid, int userid)
 {
     using (var db = new QaAContext())
     {
         var answers = HttpContext.Current.Cache.GetFromCache("AnswersToQuestion" + questionid + "User" + userid, () => (from a in db.Answers
                                                                                                                         where a.QuestionId == questionid && a.UserId == userid
                                                                                                                         select a).ToList());
         return(answers);
     }
 }
Beispiel #22
0
 /// <summary>
 /// List the questions of the user from the page
 /// </summary>
 /// <param name="userid">User ID</param>
 /// <param name="pageNumber">Page number</param>
 /// <param name="pageSize">Page size</param>
 /// <param name="total">Number of all such question</param>
 /// <returns></returns>
 public static List <Question> AllQuestionToOneUserToPagedList(int userid, int pageNumber, int pageSize, out int total)
 {
     using (var db = new QaAContext())
     {
         var questions = HttpContext.Current.Cache.GetFromCache("QuestionsByUser" + userid, () => db.Questions.Where(q => q.UserId == userid).OrderByDescending(q => q.Date).ToList());
         //Take the questions according to the page
         questions = questions.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList();
         //Get number of such questions
         total = db.Questions.Where(q => q.UserId == userid).Count();
         return(questions);
     }
 }
Beispiel #23
0
 /// <summary>
 /// Question edit
 /// </summary>
 /// <param name="data">Question's data</param>
 public static void EditQuestion(Question data)
 {
     using (var db = new QaAContext())
     {
         var q            = from question in db.Questions where (question.Id == data.Id) select question;
         var editableData = q.SingleOrDefault();
         editableData.Title   = data.Title;
         editableData.Content = data.Content;
         db.SaveChanges();
         HttpContext.Current.Cache.UpdateCache("GetQuestion" + data.Id, data);
     }
 }
Beispiel #24
0
        /// <summary>
        /// List the issue in question
        /// </summary>
        /// <param name="questionid">question ID</param>
        /// <returns></returns>
        public static List <Question> GetRelatedQuestions(int questionid)
        {
            using (var db = new QaAContext())
            {
                //List all tag based on question ID
                var tags = HttpContext.Current.Cache.GetFromCache("QuestionTags" + questionid, () => TagManager.GetAllTagToOneQuestion(questionid).Select(x => x.Id).ToList());

                //Take the questions, where the question has got tag (which there is in tags), and take the most recent of 10
                var releateds = HttpContext.Current.Cache.GetFromCache("ReleatedQuestions" + questionid, () => db.Questions.Where(q => q.QuestionHasTags.Any(qht => tags.Contains(qht.TagId)) && q.Id != questionid).OrderByDescending(d => d.Date).Take(10).ToList());
                return(releateds);
            }
        }
Beispiel #25
0
 public static string SentEmailHash(int userid, int questionid)
 {
     using (var db = new QaAContext())
     {
         var hash = MyHelpers.MD5Encode("u" + userid.ToString() + "q" + questionid.ToString());
         db.EmailIdentifiers.Add(new EmailIdentifier {
             UserId = userid, QuestionId = questionid, Hash = hash
         });
         db.SaveChanges();
         return(hash);
     }
 }
Beispiel #26
0
 /// <summary>
 /// Answer edit
 /// </summary>
 /// <param name="data">Answer's data</param>
 public static void EditAnswer(Answer data)
 {
     using (var db = new QaAContext())
     {
         var q   = from a in db.Answers where a.Id == data.Id select a;
         var ans = q.SingleOrDefault();
         ans.Content = data.Content;
         db.SaveChanges();
         HttpContext.Current.Cache.UpdateCache("AnswersToQuestion" + data.QuestionId, data);
         HttpContext.Current.Cache.UpdateCache("AnswersToQuestion" + data.QuestionId + "User" + data.UserId, data);
     }
 }
Beispiel #27
0
 /// <summary>
 /// Verify the email of the user
 /// </summary>
 /// <param name="id">UserID</param>
 public static void VerifyEmail(int id)
 {
     using (var db = new QaAContext())
     {
         var user = db.UserProfiles.Where(u => u.UserId == id).SingleOrDefault();
         if (user == null)
         {
             return;
         }
         user.IsVerified = true;
         db.SaveChanges();
     }
 }
Beispiel #28
0
 /// <summary>
 /// Write the user's vote to the question
 /// </summary>
 /// <param name="questionid">question Id</param>
 /// <param name="userid">User Id</param>
 /// <param name="vote">Vote value</param>
 public static void Vote(int questionid, int userid, int vote)
 {
     using (var db = new QaAContext())
     {
         QuestionHasVote add = new QuestionHasVote();
         add.QuestionId = questionid;
         add.UserId     = userid;
         add.Rating     = vote;
         db.QuestionHasVotes.Add(add);
         db.SaveChanges();
         HttpContext.Current.Cache.UpdateCache("QuestionVote" + questionid, add);
     }
 }
Beispiel #29
0
        /// <summary>
        /// List all question based on tag's name
        /// </summary>
        /// <param name="tagName">tagName</param>
        /// <returns></returns>
        public static List <Question> AllQuestionToTag(string tagName)
        {
            using (var db = new QaAContext())
            {
                List <Question> rl      = null;
                var             queryId = db.Tags.Where(tags => tags.Name.Equals(tagName)).SingleOrDefault();
                if (queryId != null)
                {
                    rl = db.Questions.Where(q => q.QuestionHasTags.Any(qht => qht.TagId == queryId.Id)).ToList();
                }

                return(rl);
            }
        }
Beispiel #30
0
        /// <summary>
        /// Get the list of question from the page, which the user answered
        /// </summary>
        /// <param name="userid">User ID</param>
        /// <param name="pageNumber">Page number</param>
        /// <param name="pageSize">Page size</param>
        /// <param name="total">Number of all such question</param>
        /// <returns></returns>
        public static List <Question> AnsweredQuestionsToPagedList(int userid, int pageNumber, int pageSize, out int total)
        {
            using (var db = new QaAContext())
            {
                //The user answered question
                var ques = HttpContext.Current.Cache.GetFromCache("AnsweredQuestions" + userid, () => (db.Questions.Where(q => q.Answers.Any(a => a.UserId == userid)).OrderByDescending(quest => quest.Date).Select(t => t).ToList()));
                //Take the questions according to the page
                ques = ques.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList();
                //Take number of user's questions, which the user answered
                total = db.Questions.Where(q => q.Answers.Any(a => a.UserId == userid)).Count();

                return(ques);
            }
        }