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); }
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); }
private Task configSendGridasync(IdentityMessage message) { string emailTo = message.Destination; try { MailMessage mailMsg = new MailMessage(); // To mailMsg.To.Add(new MailAddress(emailTo, emailTo)); // From //mailMsg.From = new MailAddress("*****@*****.**", "support"); mailMsg.From = new MailAddress("*****@*****.**", "eusVille"); // Subject and multipart/alternative Body mailMsg.Subject = "Welcome to eusVille"; // string text = message.Body; string html = @"<p><b>" + message.Body + " </b></p>"; //string html = @"<p><b>html body</b></p>"; //mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain)); //mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html)); //mailMsg.Body = "<div style=@quot;background-color:green@quot><bold>" + message.Body + " </bold></div>"; //string html = HttpContext.Current.Server.MapPath("../RegisterConfirmation.html"); mailMsg.IsBodyHtml = true; mailMsg.Body = html; //.Replace("#Replace", message.Body); // Init SmtpClient and send using SendGrid //SmtpClient smtpClient = new SmtpClient("smtp.sendgrid.net", Convert.ToInt32(587)); //System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["SendGridUser"], // ConfigurationManager.AppSettings["SendGridPassword"]); SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", Convert.ToInt32(587)); System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["SendMailUser"], ConfigurationManager.AppSettings["SendMailPassword"]); smtpClient.Credentials = credentials; smtpClient.EnableSsl = true; smtpClient.Send(mailMsg); } catch (Exception ex) { //Console.WriteLine(ex.Message); var seriesContent = new Dictionary <string, string> { { "user", emailTo }, { "detail", "Error trying to register/send confirmation email for new user: "******"action", "New user registration for " + emailTo }, { "errorLocation", "App_Start > IdentityConfig.cs" }, { "level", "high" } }; string jsonReturn = JsonConvert.SerializeObject(seriesContent); // Log error CommonController cont = new CommonController(); cont.Common("ErrorHandle", jsonReturn); } return(Task.FromResult(0)); //var myMessage = new SendGridMessage(); //myMessage.AddTo(message.Destination); //myMessage.From = new System.Net.Mail.MailAddress( // "*****@*****.**", "Jae K"); //myMessage.Subject = message.Subject; //myMessage.Text = message.Body; //myMessage.Html = message.Body; //var credentials = new NetworkCredential( // ConfigurationManager.AppSettings["SendGridUser"], // ConfigurationManager.AppSettings["SendGridPassword"] // ); //// Create a Web transport for sending email. //var transportWeb = new System.Web(credentials); //// Send the email. //if (transportWeb != null) //{ // return transportWeb.DeliverAsync(myMessage); //} //else //{ // return Task.FromResult(0); //} }