Exemple #1
0
        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;
        }
Exemple #2
0
        // POST
        public void Common(string Filter, string FilterData)
        {
            string jsonReturn = "";

            try
            {
                switch (Filter)
                {
                case "ErrorHandle":      // log error into eusCommon.dbo.Admin_ErrorLog
                {
                    // Parse out JSON data
                    JObject o = JObject.Parse(FilterData);

                    string user          = o.SelectToken("user").ToString();
                    string detail        = o.SelectToken("detail").ToString();
                    string action        = o.SelectToken("action").ToString();
                    string errorLocation = o.SelectToken("errorLocation").ToString();
                    string level         = o.SelectToken("level").ToString();

                    if (level == "high")
                    {
                        // email admin to notify !!
                    }


                    using (eusCommonEntities ent = new eusCommonEntities())
                    {
                        Admin_ErrorLog log = new Admin_ErrorLog();

                        log.User          = user;
                        log.Action        = action;
                        log.Details       = detail;
                        log.ErrorLocation = errorLocation;
                        log.Level         = level;
                        log.Timestamp     = DateTime.Now;
                        log.IsOpen        = true;

                        ent.Admin_ErrorLog.Add(log);
                        ent.SaveChanges();

                        break;
                    }
                }

                case "Email":
                {
                    // Send email !
                    break;
                }

                default:
                {
                    //var seriesContent = new Dictionary<string, string>
                    //        {
                    //            {"error", "true"} , {"message", "Controller: eusVote_Topic.Topic doesn't have matching filter for filter = " + Filter }
                    //        };

                    //jsonReturn = JsonConvert.SerializeObject(seriesContent);
                    break;
                }
                }
            }
            catch (Exception ex)
            {
                var seriesContent = new Dictionary <string, string>
                {
                    { "user", "inside catch" }, { "detail", ex.ToString() }, { "action", "Failed to log error." },
                    { "errorLocation", "CommonController > ErrorHandle " }
                };

                jsonReturn = JsonConvert.SerializeObject(seriesContent);

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

            //return jsonReturn;
        }
Exemple #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;
        }
        // POST
        public void Common(string Filter, string FilterData)
        {
            string jsonReturn = "";

            try
            {
                switch (Filter)
                {
                    case "ErrorHandle":  // log error into eusCommon.dbo.Admin_ErrorLog
                        {
                            // Parse out JSON data
                            JObject o = JObject.Parse(FilterData);

                            string user = o.SelectToken("user").ToString();
                            string detail =  o.SelectToken("detail").ToString();
                            string action =  o.SelectToken("action").ToString();
                            string errorLocation =  o.SelectToken("errorLocation").ToString();
                            string level = o.SelectToken("level").ToString();

                            if (level == "high")
                            {
                                // email admin to notify !!
                            }

                            using (eusCommonEntities ent = new eusCommonEntities())
                            {
                                Admin_ErrorLog log = new Admin_ErrorLog();

                                log.User = user;
                                log.Action = action;
                                log.Details = detail;
                                log.ErrorLocation = errorLocation;
                                log.Level = level;
                                log.Timestamp = DateTime.Now;
                                log.IsOpen = true;

                                ent.Admin_ErrorLog.Add(log);
                                ent.SaveChanges();

                                break;
                            }
                        }

                    case "Email":
                        {
                            // Send email !
                            break;
                        }

                    default:
                        {
                            //var seriesContent = new Dictionary<string, string>
                            //        {
                            //            {"error", "true"} , {"message", "Controller: eusVote_Topic.Topic doesn't have matching filter for filter = " + Filter }
                            //        };

                            //jsonReturn = JsonConvert.SerializeObject(seriesContent);
                            break;
                        }
                }
            }
            catch (Exception ex)
            {
                var seriesContent = new Dictionary<string, string>
                {
                    {"user", "inside catch"}, {"detail", ex.ToString()}, {"action", "Failed to log error."},
                    {"errorLocation", "CommonController > ErrorHandle "}
                };

                jsonReturn = JsonConvert.SerializeObject(seriesContent);

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

            //return jsonReturn;
        }