コード例 #1
0
        public List <Answer> GetAnswersByAnswerToId(int postId)
        {
            using var db = new StackOverflowContext();
            var answers = db.Answers.Where(x => x.AnswerToId == postId).Select(x => x).ToList();

            return(answers);
        }
コード例 #2
0
        //
        // GET: /AccountRegisterApi/
        public HttpResponseMessage PostRegister(AccountRegisterModel model)
        {
            if (!model.Email.IsNullOrWhiteSpace() && !model.Password.IsNullOrWhiteSpace() &&
                !model.ConfirmPassword.IsNullOrWhiteSpace() && !model.Surname.IsNullOrWhiteSpace() &&
                !model.Name.IsNullOrWhiteSpace() && model.ConfirmPassword == model.Password)
            {
                var context = new StackOverflowContext();
                var account = new Account
                {
                    CreationDate   = DateTime.Now,
                    Email          = model.Email,
                    IsVerified     = true,
                    LastLogDate    = DateTime.Now,
                    Name           = model.Name + " " + model.Surname,
                    Password       = model.Password,
                    ViewsToProfile = 0
                };

                context.Accounts.Add(account);
                context.SaveChanges();

                HttpResponseMessage response = this.Request.CreateResponse(HttpStatusCode.Created, model);
                return(response);
            }

            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest));
        }
コード例 #3
0
        public Answer GetAnswer(int postId)
        {
            using var db = new StackOverflowContext();
            var answer = db.Answers.Find(postId);

            return(answer);
        }
コード例 #4
0
        public Profile GetProfile(string email)
        {
            using var db = new StackOverflowContext();
            var profile = db.Profiles.Where(x => x.Email == email).Select(x => x).FirstOrDefault();

            return(profile);
        }
コード例 #5
0
        public ActionResult AddNewComment(Guid parentId, string description, Guid qId)
        {
            if (description == "undefined")
            {
                return(RedirectToAction("QuestionDetails", "Question", new { id = qId }));
            }
            var        comment = new Comment();
            var        context = new StackOverflowContext();
            HttpCookie cookie  = Request.Cookies[FormsAuthentication.FormsCookieName];

            if (cookie != null)
            {
                FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
                Guid ownerId = Guid.Parse(ticket.Name);
                comment.CreationDate = DateTime.Now;
                comment.Description  = description;
                comment.Owner        = context.Accounts.Find(ownerId);
                comment.ReferenceToQuestionOrAnswer = parentId;
                comment.Votes             = 0;
                comment.QuestionReference = qId;
                context.Comments.Add(comment);
                context.SaveChanges();
            }
            return(RedirectToAction("QuestionDetails", "Question", new{ id = qId }));
        }
コード例 #6
0
        public List <Question> GetQuestions(int [] postIds)
        {
            using var db = new StackOverflowContext();
            var questions = db.Questions.Where(x => postIds.Contains(x.PostId)).Select(x => x).ToList();

            return(questions);
        }
コード例 #7
0
        public List <Bookmark> GetAllBookmarks(int profileId)
        {
            using var db = new StackOverflowContext();
            var bookmarks = db.Bookmarks.Where(x => x.ProfileId == profileId).Select(x => x).ToList();

            return(bookmarks);
        }
コード例 #8
0
        public List <Comment> GetCommentsByPostIds(int[] postIds)
        {
            using var db = new StackOverflowContext();
            var comments = db.Comments.Where(x => postIds.Contains(x.PostId)).Select(x => x).ToList();

            return(comments);
        }
コード例 #9
0
        public List <Post> GetPosts(int[] postIds)
        {
            using var db = new StackOverflowContext();
            var posts = db.Posts.Where(x => postIds.Contains(x.PostId)).Select(x => x).ToList();

            return(posts);
        }
コード例 #10
0
        public Profile GetProfile(int profileId)
        {
            using var db = new StackOverflowContext();
            var profile = db.Profiles.Find(profileId);

            return(profile);
        }
コード例 #11
0
        public ActionResult DisLikeAnswer(Guid id, Guid qId)
        {
            var        context = new StackOverflowContext();
            var        answer  = context.Answers.Find(id);
            var        votes   = context.Votes;
            HttpCookie cookie  = Request.Cookies[FormsAuthentication.FormsCookieName];

            if (cookie != null)
            {
                FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
                Guid ownerId = Guid.Parse(ticket.Name);

                if (Enumerable.Any(votes, v => v.AccountReference == ownerId && v.ReferenceToQuestionOrAnswer == id))
                {
                    return(RedirectToAction("QuestionDetails", "Question", new { id = qId }));
                }
            }


            answer.Votes--;
            //context.Answers.Find(id).Votes++;

            context.SaveChanges();

            return(RedirectToAction("QuestionDetails", "Question", new { id = qId }));
        }
コード例 #12
0
        public Bookmark DeleteBookmark(int bookmarkId, int profileId)
        {
            using var db = new StackOverflowContext();
            var bookmark = db.Bookmarks.Find(bookmarkId);

            if (bookmark == null)
            {
                return(null);
            }
            if (profileId != bookmark.ProfileId)
            {
                return(null);
            }

            try
            {
                db.Bookmarks.Remove(bookmark);
                db.SaveChanges();
                return(bookmark);
            }
            catch (Exception e)
            {
                return(null);
            }
        }
コード例 #13
0
        public User GetUser(int userId)
        {
            using var db = new StackOverflowContext();
            var user = db.Users.Find(userId);

            return(user);
        }
コード例 #14
0
        public ActionResult AddAnswer(string desc)
        {
            var QuestionId = Guid.Parse(Session["CurrentQ"].ToString());

            if (desc.Split().Length < 5 || desc.Length < 50)
            {
                return(RedirectToAction("QuestionDetails", "Question", new { id = QuestionId, errorMessage = "Description Must Contain At Least 50 Characters and 5 Words" }));
            }
            if (ModelState.IsValid)
            {
                Guid       id        = Guid.Parse(QuestionId.ToString());
                var        context   = new StackOverflowContext();
                var        newAnswer = new Answer();
                HttpCookie cookie    = Request.Cookies[FormsAuthentication.FormsCookieName];
                if (cookie != null)
                {
                    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
                    Guid ownerId = Guid.Parse(ticket.Name);
                    newAnswer.Votes             = 0;
                    newAnswer.AnswerText        = desc;
                    newAnswer.Owner             = context.Accounts.FirstOrDefault(x => x.Id == ownerId);
                    newAnswer.ModificationDate  = DateTime.Now;
                    newAnswer.CreationDate      = DateTime.Now;
                    newAnswer.IsBestAnswer      = false;
                    newAnswer.QuestionReference = context.Questions.Find(id);
                    newAnswer.NumberOfViews     = 0;
                    context.Answers.Add(newAnswer);
                    context.SaveChanges();
                }
                return(RedirectToAction("QuestionDetails", "Question", new{ id = QuestionId }));
            }
            return(RedirectToAction("QuestionDetails", "Question", new{ id = QuestionId }));
        }
コード例 #15
0
        public ActionResult PasswordRecovery(PasswordRecovery model)
        {
            var context = new StackOverflowContext();
            var account = context.Accounts.FirstOrDefault(x => x.Email == model.email);

            if (account == null)
            {
                model.Error = String.Format("Email <strong>{0}</strong> Does Not Exist", model.email);
                model.email = "";
                return(View(model));
            }

            //    return RedirectToAction("PasswordDisplay",new{password = account.Password});
            string host = Request.Url.GetLeftPart(UriPartial.Authority);
            string message;

            if (host.Contains("localhost:"))
            {
                message = "Click to get your Password: "******"/Account/ChangePassword?id=" + account.Id;
            }
            else
            {
                message = "Click to get your Password: "******"http://stackop4.apphb.com/Account/ChangePassword?id=" + account.Id;
            }
            new MailService().SendMail(model.email, message);
            model = new PasswordRecovery
            {
                Success = "An Email Has Been Sent With Instructions to Recover your Password",
                email   = ""
            };
            return(View(model));
        }
コード例 #16
0
        //
        // GET: /Comment/
        public ActionResult CommentList(Guid id, Guid qId)
        {
            var context = new StackOverflowContext();
            var m       = new CommentListCollectionModel()
            {
                Comments        = new ListStack <CommentListModel>(),
                ParentReference = id, QuestionReference = qId
            };

            IList <CommentListModel> models = new ListStack <CommentListModel>();

            var comments = context.Comments.Include(x => x.Owner);

            foreach (Comment com in comments)
            {
                if (com.ReferenceToQuestionOrAnswer == id)
                {
                    var c = new CommentListModel
                    {
                        CreationDate = com.CreationDate,
                        Description  = com.Description,
                        OwnerIf      = com.Owner.Id,
                        ReferenceToQuestionOrAnswer = com.ReferenceToQuestionOrAnswer,
                        ReferenceToQuestion         = com.QuestionReference,
                        OwnerName = com.Owner.Name
                    };
                    m.Comments.Add(c);
                }
            }
            return(PartialView(m));
        }
コード例 #17
0
        public Bookmark UpdateBookmark(int bookmarkId, int profileId, string note)
        {
            using var db = new StackOverflowContext();
            var bookmark = db.Bookmarks.Find(bookmarkId);

            if (bookmark == null)
            {
                return(null);
            }
            if (bookmark.ProfileId != profileId)
            {
                return(null);
            }

            try
            {
                bookmark.Note = note;
                db.SaveChanges();
                return(bookmark);
            }
            catch (Exception e)
            {
                return(null);
            }
        }
コード例 #18
0
        public Post GetPost(int postId)
        {
            using var db = new StackOverflowContext();
            var post = db.Posts.Find(postId);

            return(post);
        }
コード例 #19
0
        public List <User> GetUsers(int[] userIds)
        {
            using var db = new StackOverflowContext();
            var users = db.Users.Where(x => userIds.Contains(x.UserId)).Select(x => x).ToList();

            return(users);
        }
コード例 #20
0
        public Question GetQuestion(int postId)
        {
            using var db = new StackOverflowContext();
            var question = db.Questions.Find(postId);

            return(question);
        }
コード例 #21
0
        public IActionResult GetPost(int id)
        {
            var post = _dataService.GetPost(id);

            if (post == null)
            {
                return(NotFound());
            }

            bool isQuestion;
            var  model = Mapper.Map <PostModel>(post);

            using (var context = new StackOverflowContext()) {
                if (context.Answers.Any(i => i.Id == post.Id))
                {
                    isQuestion = false;
                }
                else
                {
                    isQuestion = true;
                }
            }

            model.Author  = Url.Link(nameof(GetAuthorOfPost), new { postId = post.Id });
            model.Answers = isQuestion == true?Url.Link(nameof(GetAnswers), new { questionId = post.Id }) : null;

            model.Comments        = Url.Link(nameof(GetComments), new { postId = post.Id });
            model.ClickHereToMark = Url.Link(nameof(MarkController.Mark), new { postId = post.Id });
            return(Ok(model));
        }
コード例 #22
0
        public ActionResult QuestionDetails(Guid id, string errorMessage = null)
        {
            var context  = new StackOverflowContext();
            var context2 = new StackOverflowContext();
            var question = context.Questions.Find(id);
            QuestionDetailsModel details = new QuestionDetailsModel();

            details.Description  = question.Description;
            details.Title        = question.Title;
            details.Score        = question.Votes;
            details.QuestionId   = id;
            details.ErrorMessage = errorMessage;
            HttpCookie cookie = Request.Cookies[FormsAuthentication.FormsCookieName];

            if (cookie != null)
            {
                FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
                Guid ownerId = Guid.Parse(ticket.Name);
                details.UserHasVoted =
                    (context2.Votes.FirstOrDefault(
                         x => x.AccountReference == ownerId && x.ReferenceToQuestionOrAnswer == id) != null);
            }
            else
            {
                details.UserHasVoted = false;
            }
            ++(question.NumberOfViews);
            Session["CurrentQ"] = question.Id;
            context.SaveChanges();
            ViewData["qId"] = id;
            return(View(details));
        }
コード例 #23
0
        public ActionResult RecentQuestions(Guid ownerId)
        {
            IList <QuestionListModel> models = new ListStack <QuestionListModel>();
            var context = new StackOverflowContext();
            var que = context.Questions.Include(r => r.Owner).OrderByDescending(y => y.CreationDate).ToList();
            int i, count = 0;

            for (i = 0; i < que.Count; i++)
            {
                if (count >= 5 || que.ElementAt(i).Owner.Id != ownerId)
                {
                    break;
                }
                QuestionListModel model = new QuestionListModel();
                model.Title        = que.ElementAt(i).Title;
                model.Votes        = que.ElementAt(i).Votes;
                model.CreationTime = que.ElementAt(i).CreationDate;
                model.OwnerName    = que.ElementAt(i).Owner.Name;
                model.QuestionId   = que.ElementAt(i).Id;
                model.OwnerId      = que.ElementAt(i).Owner.Id;
                model.Views        = que.ElementAt(i).NumberOfViews;
                var trimmed   = que.ElementAt(i).Description.Trim();
                var substring = trimmed.Substring(0, Math.Min(10, trimmed.Length));
                model.QuestionPreview = substring + "...";
                count++;
                models.Add(model);
            }
            return(PartialView(models));
        }
コード例 #24
0
        private int AnswerCount(Guid qId)
        {
            var context = new StackOverflowContext();
            var count   = context.Answers.Include(r => r.QuestionReference).Count(a => a.QuestionReference.Id == qId);

            return(count);
        }
コード例 #25
0
        public List <WordWeight> GetWordCloud(int postId)
        {
            using var db = new StackOverflowContext();
            //var wordCloud = db.Weighted_Inverted_Index.FromSqlRaw("select wordcloud({0});", postId).ToList();
            var wordCloud = db.Weighted_Inverted_Index.Where(x => x.PostId == postId).Select(x => x.MultiplyWeight()).ToList();

            return(wordCloud);
        }
コード例 #26
0
        public List <Question> SearchQuestions(string[] keywords)
        {
            using var db = new StackOverflowContext();
            var query     = CreateSearchQuestionsQuery(keywords);
            var questions = db.Questions.FromSqlRaw(query).ToList();

            return(questions);
        }
コード例 #27
0
        public List <Question> GetLinkedQuestions(int postId)
        {
            using var db = new StackOverflowContext();
            var links     = db.Links.Where(x => x.PostId == postId).Select(x => x.LinkPostId).ToList();
            var questions = db.Questions.Where(x => links.Contains(x.PostId)).Select(x => x).ToList();

            return(questions);
        }
コード例 #28
0
        public void Setup()
        {
            var option = new DbContextOptionsBuilder <StackOverflowContext>();
            var conn   = "Server=.;Database=StackOverflow2010;Trusted_Connection=True;";

            option.UseSqlServer(conn);

            _context = new StackOverflowContext(option.Options);
        }
コード例 #29
0
        public ActionResult PasswordDisplay(string id)
        {
            PasswordDisplayModel model = new PasswordDisplayModel();
            var context = new StackOverflowContext();
            var account = context.Accounts.Find(Guid.Parse(id));

            model.Password = account.Password;

            return(View(model));
        }
コード例 #30
0
        public ActionResult DeleteAnswer(Guid id, Guid qId)
        {
            var context = new StackOverflowContext();
            var answer  = context.Answers.Include(x => x.Owner).Include(y => y.QuestionReference).FirstOrDefault(z => z.Id == id);

            context.Answers.Remove(answer);
            context.SaveChanges();

            return(RedirectToAction("QuestionDetails", "Question", new { id = qId }));
        }