Esempio n. 1
0
        public ActionResult Create([Bind(Include = "CategoryID,Category")] CategoryList categoryList)
        {
            if (ModelState.IsValid)
            {
                db.CategoryLists.Add(categoryList);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(categoryList));
        }
Esempio n. 2
0
        public ActionResult Create([Bind(Include = "TopicID,Topic1,Tags,CategoryID,UserId,TimeStamp,TopicURL,Search")] Topic topic)
        {
            if (ModelState.IsValid)
            {
                db.Topics.Add(topic);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.CategoryID = new SelectList(db.CategoryLists, "CategoryID", "Category", topic.CategoryID);
            return(View(topic));
        }
Esempio n. 3
0
        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);
        }