public ActionResult LikeIdea(int idea_id)
 {
     using (var context = new ENSE496Entities())
     {
         if (Session["UserID"] != null)
         {
             int userId = Convert.ToInt32(Session["UserID"]);
             if (context.Likes.Where(x => x.User_id == userId && x.Idea_id == idea_id).Any()) //checks if user has any inactive likes already stored in DB
             {
                 context.Likes.Where(x => x.User_id == userId && x.Idea_id == idea_id).First().Active = true;
             }
             else
             {
                 Like newLike = new Like();
                 newLike.Idea_id = idea_id;
                 newLike.User_id = userId;
                 newLike.Active  = true;
                 context.Likes.Add(newLike);
             }
             context.SaveChanges();
             return(Redirect(Request.UrlReferrer.ToString()));
         }
         return(View());
     }
 }
 public ActionResult Subscribe(int idea_id)
 {
     using (var context = new ENSE496Entities())
     {
         if (Session["UserID"] != null)
         {
             int userId = Convert.ToInt32(Session["UserID"]);
             //if (context.Subscriptions.Where(x => x.User_id == userId && x.Idea_id == idea_id).Any()) //checks if user has any inactive likes already stored in DB
             //{
             //    context.Subscriptions.Where(x => x.User_id == userId && x.Idea_id == idea_id).First().Active = true;
             //}
             //else
             //{
             Subscription subscription = new Subscription();
             subscription.Idea_id = idea_id;
             subscription.User_id = userId;
             subscription.Active  = true;
             context.Subscriptions.Add(subscription);
             //}
             context.SaveChanges();
             return(Redirect(Request.UrlReferrer.ToString()));
         }
         return(RedirectToAction("Index"));
     }
 }
 public ActionResult DeleteIdea(int id)
 {
     using (var context = new ENSE496Entities()) {
         context.Ideas.Remove(context.Ideas.Where(x => x.Id == id).FirstOrDefault());
         context.Status_log.Remove(context.Status_log.Where(x => x.Idea_id == id).FirstOrDefault());
         context.SaveChanges();
         return(RedirectToAction("MyIdeas"));
     }
 }
 public ActionResult EditIdea(Idea ideaParam)
 {
     using (var context = new ENSE496Entities()) {
         /****Need the Id to change the correct record in the db*/
         //context.Ideas.Where(x => x.Id == ideaParam.Id).FirstOrDefault().Status = "Pending";
         //context.Ideas.Where(x => x.Id == ideaParam.Id).FirstOrDefault().Title = ideaParam.Title;
         //context.Ideas.Where(x => x.Id == ideaParam.Id).FirstOrDefault().Description  = ideaParam.Description;
         //****
         context.SaveChanges();
         return(RedirectToAction("Index"));
     }
 }
 public ActionResult CreateComment(Comment commentParam)
 {
     using (var context = new ENSE496Entities())
     {
         if (Session["UserID"] != null)
         {
             if (String.IsNullOrEmpty(commentParam.Comment1))
             {
                 TempData["UserMessage"] = new MessageViewModel()
                 {
                     CssClassName = "alert-danger", Message = "You cannot leave the comment field empty..."
                 };
                 return(RedirectToAction("ViewIdea", new { id = commentParam.Idea_id }));
             }
             else
             {
                 Idea    idea       = context.Ideas.Where(x => x.Id == commentParam.Idea_id).FirstOrDefault();
                 Comment newComment = new Comment();
                 newComment.Idea_id      = commentParam.Idea_id;
                 newComment.Comment1     = commentParam.Comment1;
                 newComment.User_id      = Convert.ToInt32(Session["UserID"]);
                 newComment.Submitted_on = DateTime.Now;
                 newComment.Active       = true;
                 context.Comments.Add(newComment);
                 if (newComment.User_id != idea.User_id) //checks to see if status change was made by someone other than who submitted the idea
                 {
                     Notification notification = new Notification();
                     notification.Recipient_id = idea.User_id;
                     notification.Message      = Session["Username"].ToString() + " has commented on your idea. (Idea #" + newComment.Idea_id.ToString() + ")";
                     notification.Active       = true;
                     context.Notifications.Add(notification);
                 }
                 if (context.Subscriptions.Where(x => x.Idea_id == commentParam.Idea_id).Any())
                 {
                     var subs = context.Subscriptions.Where(x => x.Idea_id == commentParam.Idea_id).ToList();
                     foreach (Subscription sub in subs)
                     {
                         Notification notification = new Notification();
                         notification.Recipient_id = sub.User_id;
                         notification.Message      = Session["Username"].ToString() + " has commented on an idea you are subscribed to. (Idea #" + commentParam.Idea_id.ToString() + ")";
                         notification.Active       = true;
                         context.Notifications.Add(notification);
                     }
                 }
                 context.SaveChanges();
                 return(RedirectToAction("ViewIdea", new { id = commentParam.Idea_id }));
             }
             //}
         }
         return(View());
     }
 }
 public ActionResult DismissNotification(int id)
 {
     using (var context = new ENSE496Entities())
     {
         var notification = context.Notifications.Where(x => x.Id == id).FirstOrDefault();
         if (notification != null)
         {
             context.Notifications.Remove(notification);
             context.SaveChanges();
         }
         return(RedirectToAction("Notifications"));
     }
 }
 public ActionResult DeleteIdea(int id)
 {
     using (var context = new ENSE496Entities()) {
         context.Status_log.RemoveRange(context.Status_log.Where(x => x.Idea_id == id).ToList());
         context.Likes.RemoveRange(context.Likes.Where(x => x.Idea_id == id).ToList());
         context.Comments.RemoveRange(context.Comments.Where(x => x.Idea_id == id).ToList());
         context.Subscriptions.RemoveRange(context.Subscriptions.Where(x => x.Idea_id == id).ToList());
         context.Ideas.Remove(context.Ideas.Where(x => x.Id == id).FirstOrDefault());
         //context.Status_log.Remove(context.Status_log.Where(x => x.Idea_id == id).FirstOrDefault());
         //context.Ideas.Where(x => x.Id == id).FirstOrDefault().Active = false;
         context.SaveChanges();
         return(RedirectToAction("Index"));
     }
 }
 public ActionResult UnlikeIdea(int idea_id)
 {
     using (var context = new ENSE496Entities())
     {
         if (Session["UserID"] != null)
         {
             int userId = Convert.ToInt32(Session["UserID"]);
             context.Likes.Where(x => x.User_id == userId && x.Idea_id == idea_id).First().Active = false;
             context.Likes.RemoveRange(context.Likes.Where(x => x.User_id == userId && x.Idea_id == idea_id).ToList());
             context.SaveChanges();
             return(Redirect(Request.UrlReferrer.ToString()));
         }
         return(View());
     }
 }
 public ActionResult Unsubscribe(int idea_id)
 {
     using (var context = new ENSE496Entities())
     {
         if (Session["UserID"] != null)
         {
             int userId = Convert.ToInt32(Session["UserID"]);
             //var subs = context.Subscriptions.Where(x => x.User_id == userId && x.Idea_id == idea_id).ToList();
             //context.Subscriptions.Where(x => x.User_id == userId && x.Idea_id == idea_id);
             context.Subscriptions.RemoveRange(context.Subscriptions.Where(x => x.User_id == userId && x.Idea_id == idea_id).ToList());
             context.SaveChanges();
             return(Redirect(Request.UrlReferrer.ToString()));
         }
         return(RedirectToAction("Index"));
     }
 }
 public ActionResult RemoveSuccessStories(int idea_id)
 {
     using (var context = new ENSE496Entities())
     {
         if (Session["UserID"] != null)
         {
             Idea idea = context.Ideas.Where(x => x.Id == idea_id).FirstOrDefault();
             idea.OnSuccessStories = false;
             context.SaveChanges();
             return(Redirect(Request.UrlReferrer.ToString()));
         }
         else
         {
             return(RedirectToAction("Login"));
         }
     }
 }
 public ActionResult ApproveIdea(Status_log statusLogParam)
 {
     using (var context = new ENSE496Entities())
     {
         Idea       idea      = context.Ideas.Where(x => x.Id == statusLogParam.Idea_id).FirstOrDefault();
         Status_log statusLog = new Status_log();
         statusLog.Current_status  = "Approved";
         statusLog.Previous_status = idea.Status;
         statusLog.Idea_id         = statusLogParam.Idea_id;
         statusLog.User_id         = Convert.ToInt32(Session["UserID"]);
         statusLog.Description     = statusLogParam.Description;
         context.Ideas.Where(x => x.Id == statusLogParam.Idea_id).FirstOrDefault().Status = "Approved";
         context.Status_log.Add(statusLog);
         context.SaveChanges();
         return(RedirectToAction("Index"));
     }
 }
        public ActionResult PlanIdea(Status_log statusLogParam)
        {
            using (var context = new ENSE496Entities())
            {
                if (ModelState.IsValid)
                {
                    Idea       idea      = context.Ideas.Where(x => x.Id == statusLogParam.Idea_id).FirstOrDefault();
                    Status_log statusLog = new Status_log();
                    statusLog.Current_status  = "Planned";
                    statusLog.Previous_status = idea.Status;
                    statusLog.Idea_id         = statusLogParam.Idea_id;
                    statusLog.User_id         = Convert.ToInt32(Session["UserID"]);
                    statusLog.Description     = statusLogParam.Description;
                    context.Ideas.Where(x => x.Id == statusLogParam.Idea_id).FirstOrDefault().Status = "Planned";
                    context.Status_log.Add(statusLog);

                    if (idea.User_id != statusLog.User_id) //checks to see if status change was made by someone other than who submitted the idea
                    {
                        Notification notification = new Notification();
                        notification.Recipient_id = idea.User_id;
                        notification.Message      = Session["Username"].ToString() + " has planned your idea. (Idea #" + statusLog.Idea_id.ToString() + ")";
                        notification.Active       = true;
                        context.Notifications.Add(notification);
                    }
                    if (context.Subscriptions.Where(x => x.Idea_id == statusLog.Idea_id).Any())
                    {
                        var subs = context.Subscriptions.Where(x => x.Idea_id == statusLog.Idea_id).ToList();
                        foreach (Subscription sub in subs)
                        {
                            Notification notification = new Notification();
                            notification.Recipient_id = sub.User_id;
                            notification.Message      = Session["Username"].ToString() + " has planned an idea you are subscribed to. (Idea #" + statusLog.Idea_id.ToString() + ")";
                            notification.Active       = true;
                            context.Notifications.Add(notification);
                        }
                    }
                    context.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                return(View());
            }
        }
 public ActionResult CreateIdea(Idea ideaParam)
 {
     using (var context = new ENSE496Entities()) {
         if (Session["UserID"] != null)
         {
             if (ModelState.IsValid)
             {
                 Idea newIdea = new Idea();
                 newIdea.Title       = ideaParam.Title;
                 newIdea.Description = ideaParam.Description;
                 newIdea.User_id     = Convert.ToInt32(Session["UserID"]);
                 newIdea.Status      = "Pending";
                 context.Ideas.Add(newIdea);
                 context.SaveChanges();
                 return(RedirectToAction("Index"));
             }
         }
         return(View());
     }
 }
 public ActionResult EditIdea(Idea ideaParam)
 {
     using (var context = new ENSE496Entities()) {
         if (ModelState.IsValid)
         {
             context.Ideas.Where(x => x.Id == ideaParam.Id).First().Title       = ideaParam.Title;
             context.Ideas.Where(x => x.Id == ideaParam.Id).First().Description = ideaParam.Description;
             //if (ideaParam.User_id != Convert.ToInt32(Session["UserID"])) //checks to see if edit was made by someone other than who submitted the idea
             //{
             //    Notification notification = new Notification();
             //    notification.Recipient_id = ideaParam.User_id;
             //    notification.Message = Session["Username"].ToString() + " has edited your idea. (Idea #" + ideaParam.Id.ToString() + ")";
             //    notification.Active = true;
             //    context.Notifications.Add(notification);
             //}
             //if (context.Subscriptions.Where(x => x.Idea_id == ideaParam.Id).Any())
             //{
             //    var subs = context.Subscriptions.Where(x => x.Idea_id == ideaParam.Id).ToList();
             //    foreach (Subscription sub in subs)
             //    {
             //        Notification notification = new Notification();
             //        notification.Recipient_id = sub.User_id;
             //        notification.Message = Session["Username"].ToString() + " has edited an idea you are subscribed to. (Idea #" + ideaParam.Id.ToString() + ")";
             //        notification.Active = true;
             //        context.Notifications.Add(notification);
             //    }
             //}
             context.SaveChanges();
             //if (ideaParam.User_id != statusLog.User_id) //checks to see if edit was made by someone other than who submitted the idea
             //{
             //    Notification notification = new Notification();
             //    notification.Recipient_id = idea.User_id;
             //    notification.Message = Session["Username"].ToString() + " has parked your idea. (Idea #" + statusLog.Idea_id.ToString() + ")";
             //    notification.Active = true;
             //    context.Notifications.Add(notification);
             //}
             return(RedirectToAction("Index"));
         }
         return(View());
     }
 }
 public ActionResult CreateIdea(Idea ideaParam)
 {
     using (var context = new ENSE496Entities()) {
         if (Session["UserID"] != null)
         {
             if ((String.IsNullOrEmpty(ideaParam.Title)) || (String.IsNullOrEmpty(ideaParam.Description)))
             {
                 TempData["UserMessage"] = new MessageViewModel()
                 {
                     CssClassName = "alert-danger", Message = "You have left either the title or description empty"
                 };
                 return(RedirectToAction("CreateIdea"));
             }
             else if (ideaParam.Title.Length > 255)
             {
                 TempData["UserMessage"] = new MessageViewModel()
                 {
                     CssClassName = "alert-danger", Message = "The title must be less than 256 characters"
                 };
                 return(RedirectToAction("CreateIdea"));
             }
             else
             {
                 Idea newIdea = new Idea();
                 newIdea.Title       = ideaParam.Title;
                 newIdea.Description = ideaParam.Title;
                 newIdea.User_id     = Convert.ToInt32(Session["UserID"]);
                 newIdea.Status      = "Pending";
                 context.Ideas.Add(newIdea);
                 context.SaveChanges();
                 return(RedirectToAction("Index"));
             }
         }
         return(View());
     }
 }