コード例 #1
0
        public ActionResult Comments(int id)
        {
            IEnumerable <CommentModel> comments;

            using (eusCommonEntities entCommon = new eusCommonEntities())
            {
                using (eusVoteEntities entVote = new eusVoteEntities())
                {
                    var listUsers    = new List <AspNetUser>(entCommon.AspNetUsers);
                    var listComments = new List <Comment>(entVote.Comments);

                    // Do a join to listUsers to get the UserName
                    comments = (from c in listComments
                                join u in listUsers on c.UserID equals u.UserID
                                where (c.AnswerID == id)
                                select new CommentModel
                    {
                        Author = u.UserName,
                        Text = c.Comment1,
                        UserID = u.UserID
                    });
                }
            }
            return(Json(comments, JsonRequestBehavior.AllowGet));
        }
コード例 #2
0
ファイル: TopicController.cs プロジェクト: nanonerd/eusVille
        // Create child action to render comments
        public PartialViewResult Comments(int answerID)
        {
            using (eusVoteEntities ent = new eusVoteEntities())
            {
                var comments = (from c in entVote.Comments
                                where c.AnswerID == answerID
                                orderby c.TimeStamp descending
                                select c).ToList();

                // This is the URL used in the partial view to go to page that displays all comments for answerID
                ViewBag.Url = "/comments/" + answerID.ToString();
                return(PartialView(comments));
            }
        }
コード例 #3
0
ファイル: TopicController.cs プロジェクト: nanonerd/eusVille
        public string Topic(string Filter, string FilterData)
        {
            string jsonReturn = "";
            string userName   = "";

            // If user is logged in, get UserID. To get to this point, user must be logged in.
            if (User.Identity.IsAuthenticated)
            {
                userName = User.Identity.Name;
            }
            else
            {
                throw new Exception("Must be logged in to post.");
            }

            //FilterData = null;   // to test errorHandle
            try
            {
                switch (Filter)
                {
                case "SetRating":
                {
                    // Parse out JSON data
                    JObject o = JObject.Parse(FilterData);

                    int    userID   = (int)o.SelectToken("UserID");
                    int    answerID = (int)o.SelectToken("AnswerID");
                    int    rating   = (int)o.SelectToken("Rating");
                    string comment  = (string)o.SelectToken("Comment");

                    using (eusVoteEntities entVote = new eusVoteEntities())
                    {
                        // Save comment only if not empty.
                        if (!string.IsNullOrWhiteSpace(comment))
                        {
                            Comment com = new Comment();
                            com.AnswerID  = answerID;
                            com.UserID    = userID;
                            com.Comment1  = comment;
                            com.TimeStamp = DateTime.Now;

                            entVote.Comments.Add(com);
                            entVote.SaveChanges();
                        }

                        // Logic to set rating
                        var record = (from e in entVote.Ratings
                                      where e.UserID == userID &&
                                      e.AnswerID == answerID
                                      select e).FirstOrDefault();

                        // Set the rating for an answer per this user
                        if (record != null)          // If exist, then Update rating
                        {
                            record.Rating1 = rating;
                            entVote.SaveChanges();
                        }
                        else           // If not exist, then Insert new rating per this user
                        {
                            Rating r = new Rating();
                            r.UserID   = userID;
                            r.AnswerID = answerID;
                            r.Rating1  = rating;

                            entVote.Ratings.Add(r);
                            entVote.SaveChanges();
                        }

                        //////////////////////////////////////////////////////////////////////////
                        //////// Update the RatingScore for the Answer/Answer in the Rating table
                        //////////////////////////////////////////////////////////////////////////

                        // Get all the rating values for the answer (will be summed up in RatingScore calc below)
                        var totalRatings = (from t in entVote.Ratings
                                            where t.AnswerID == answerID
                                            select t.Rating1);

                        // Get the answer info for the answer that is being updated
                        var answer = (from op in entVote.Answers
                                      where op.AnswerID == answerID
                                      select op).FirstOrDefault();

                        // If record is null, then Insert was done so increment count.
                        if (record == null)
                        {
                            if (answer.Count == null)
                            {
                                answer.Count = 1;
                            }
                            else
                            {
                                answer.Count++;
                            }
                        }

                        // Calcuate new avg rating based on latest numbers
                        answer.RatingScore = Math.Round(((decimal)totalRatings.Sum() / (decimal)totalRatings.Count()), 2);
                        entVote.SaveChanges();

                        // Create dictionary of values to return
                        var seriesContent = new Dictionary <string, string>
                        {
                            { "error", "false" }, { "count", answer.Count.ToString() }, { "score", answer.RatingScore.ToString() }
                        };

                        // Serialize the dictionary to return as json
                        jsonReturn = JsonConvert.SerializeObject(seriesContent);
                        break;
                    }
                }

                case "CommentPost":
                {
                    // Parse out JSON data
                    JObject o = JObject.Parse(FilterData);

                    int      userID   = (int)o.SelectToken("UserID");
                    int      answerID = (int)o.SelectToken("AnswerID");
                    string   comment  = (string)o.SelectToken("Comment");
                    DateTime stamp    = DateTime.Now;

                    using (eusVoteEntities entVote = new eusVoteEntities())
                    {
                        Comment com = new Comment();
                        com.AnswerID  = answerID;
                        com.UserID    = userID;
                        com.Comment1  = comment;
                        com.TimeStamp = stamp;

                        entVote.Comments.Add(com);
                        entVote.SaveChanges();
                    }
                    break;
                }

                default:
                {
                    var seriesContent = new Dictionary <string, string>
                    {
                        { "error", "true" }, { "message", "Controller: eusVote > TopicController > Topic [POST] doesn't have matching filter for filter = " + Filter }
                    };

                    jsonReturn = JsonConvert.SerializeObject(seriesContent);
                    break;
                }
                }
            }
            catch (Exception ex)
            {
                // Create error data to serialize to JSON.
                var seriesContent = new Dictionary <string, string>
                {
                    { "user", userName }, { "detail", ex.Message }, { "action", Filter },
                    { "errorLocation", "eusVote > TopicController > Topic [POST] " + Filter }, { "level", "high" }
                };

                // Serialize error data.
                jsonReturn = JsonConvert.SerializeObject(seriesContent);

                // Log error
                CommonController cont = new CommonController();
                cont.Common("ErrorHandle", jsonReturn);

                return(jsonReturn);
            }

            return(jsonReturn);
        }
コード例 #4
0
ファイル: TopicController.cs プロジェクト: nanonerd/eusVille
        public string TopicGet(string Filter, string FilterData)
        {
            string jsonReturn = "";

            try
            {
                switch (Filter)
                {
                case "AnswersGet":
                {
                    int topicID = Convert.ToInt32(FilterData);

                    // Get topic title for id.
                    var topicTitle = (from t in entVote.Topics
                                      where t.TopicID == topicID
                                      select t.Topic1).FirstOrDefault();

                    ViewBag.TopicTitle = topicTitle;
                    ViewBag.TopicID    = topicID;

                    // Get top 8 answers for topic based on TopicID.
                    //var answers = (from o in entVote.Answers
                    //               where o.TopicID == topicID
                    //               orderby o.RatingScore descending
                    //               select o).Take(8);

                    // If user is logged in, get UserID and set it to ViewBag to pass to the View.
                    var userID = 0;

                    if (User.Identity.IsAuthenticated)
                    {
                        var      userName = User.Identity.Name;
                        Identity ident    = new Identity();
                        userID         = (int)ident.GetUserID(userName);
                        ViewBag.UserID = userID.ToString();
                    }
                    else
                    {
                        ViewBag.UserID = "0";
                    }

                    var answers = entVote.GetTopicAnswers(topicID, userID).ToList();

                    ViewBag.Count = answers.Count();

                    jsonReturn = JsonConvert.SerializeObject(answers, Formatting.Indented,
                                                             new JsonSerializerSettings
                        {
                            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                        });


                    break;
                }

                case "CommentsGet":
                {
                    int answerID = Convert.ToInt32(FilterData);

                    using (eusVoteEntities entVote = new eusVoteEntities())
                    {
                        var comments = (from c in entVote.Comments
                                        where c.AnswerID == answerID
                                        select c).Take(5).ToArray();

                        jsonReturn = JsonConvert.SerializeObject(comments, Formatting.Indented,
                                                                 new JsonSerializerSettings
                            {
                                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                            });
                    }

                    break;
                }

                default:
                {
                    break;
                }
                }
            }
            catch (Exception ex)
            {
                var seriesContent = new Dictionary <string, string>
                {
                    { "user", "test" }, { "detail", ex.ToString() }, { "action", Filter },
                    { "errorLocation", "eusVote > TopicController > Topic > Get > " + Filter }, { "level", "high" }
                };

                jsonReturn = JsonConvert.SerializeObject(seriesContent);

                // Log error
                CommonController cont = new CommonController();
                cont.Common("ErrorHandle", jsonReturn);

                return(jsonReturn);
            }

            return(jsonReturn);
        }
コード例 #5
0
        // GET: Comment View
        public ActionResult Index(int id)   // id is answerID (e.g., 10 for Minato Sushi Restaurant)
        {
            // If user is logged in, get UserID and set it to ViewBag to pass to the View.
            if (User.Identity.IsAuthenticated)
            {
                using (eusCommonEntities entCommon = new eusCommonEntities())
                {
                    var userName = User.Identity.Name;

                    var userID = (from i in entCommon.AspNetUsers
                                  where i.UserName == User.Identity.Name
                                  select i.UserID).FirstOrDefault();

                    ViewBag.UserName = userName;
                    ViewBag.UserID   = userID.ToString();
                }
            }
            else
            {
                ViewBag.UserID = "0";
            }

            using (eusVoteEntities entVote = new eusVoteEntities())
            {
                var answer = (from o in entVote.Answers
                              where o.AnswerID == id
                              select new { o.TopicID, o.Title }).FirstOrDefault();

                ViewBag.AnswerTitle = answer.Title;

                var topic = (from t in entVote.Topics
                             where t.TopicID == answer.TopicID
                             select new { t.Topic1, t.TopicURL }).FirstOrDefault();

                // Topic title and id to be passed into comments view to create ReturnURL
                ViewBag.TopicTitle    = topic.Topic1;
                ViewBag.TopicTitleURL = topic.TopicURL;
                ViewBag.TopicID       = answer.TopicID.ToString();
            }

            ViewBag.AnswerID = id.ToString();

            // Get base URL (e.g., http://www.eusVille.com)
            ViewBag.BaseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/') + "/";

            IEnumerable <CommentModel> comments;

            using (eusCommonEntities entCommon = new eusCommonEntities())
            {
                using (eusVoteEntities entVote = new eusVoteEntities())
                {
                    var listUsers    = new List <AspNetUser>(entCommon.AspNetUsers);
                    var listComments = new List <Comment>(entVote.Comments);

                    // Do a join to listUsers to get the UserName
                    comments = (from c in listComments
                                join u in listUsers on c.UserID equals u.UserID
                                where (c.AnswerID == id)
                                select new CommentModel
                    {
                        Author = u.UserName,
                        Text = c.Comment1,
                        UserID = u.UserID
                    });
                }
            }

            return(View(comments));
        }