Esempio n. 1
0
        public IActionResult Register(RegViewModel model)
        {
            if (HttpContext.Session.GetInt32("UserId") != null)
            {
                return(RedirectToAction("Home", "Ideas"));
            }

            if (ModelState.IsValid)
            {
                User ExistingUser = _context.Users.SingleOrDefault(u => u.Email == model.Email);
                if (ExistingUser != null)
                {
                    ViewBag.Message = "Email is already in use.";
                    return(View("Index", model));
                }
                User NewUser = new User
                {
                    Name     = model.Name,
                    Alias    = model.Alias,
                    Email    = model.Email,
                    Password = model.Password
                };
                PasswordHasher <User> Hasher = new PasswordHasher <User>();
                NewUser.Password = Hasher.HashPassword(NewUser, model.Password);
                _context.Add(NewUser);
                _context.SaveChanges();
                int UserId = _context.Users.Last().UserId;
                HttpContext.Session.SetInt32("UserId", UserId);
                return(RedirectToAction("Home", "Ideas"));
            }

            return(View("Index", model));
        }
Esempio n. 2
0
        public async Task <IActionResult> Create([Bind("ID,Name,UserID,Title,Problem,Solution,Status,Team,UploadDate")] Idea idea)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user.Firstname != null)
            {
                idea.Name = user.Firstname + " " + user.Lastname;
            }
            else
            {
                idea.Name = "Anon";
            }
            idea.UserID     = user.Id;
            idea.Status     = "Pending";
            idea.Team       = user.Team;
            idea.UploadDate = DateTime.Now;
            if (ModelState.IsValid)
            {
                _context.Add(idea);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(idea));
        }
        public async Task <IActionResult> Create(DetailsViewModel model, string returnUrl)
        {
            Comment comment = model.NewComment;
            User    user    = (User)RouteData.Values["User"];

            comment.UserId = user.Id;

            if (comment.Text != null && comment.Text.Length > 0)
            {
                _context.Add(comment);
                await _context.SaveChangesAsync();

                return(Redirect(returnUrl));
            }
            return(Redirect(returnUrl));
        }
Esempio n. 4
0
        public async Task <IActionResult> Create([Bind("Text")] Idea idea)
        {
            User user = (User)RouteData.Values["User"];

            idea.UserId = user.Id;
            if (idea.Text != null && idea.Text.Length > 0)
            {
                _context.Add(idea);
                await _context.SaveChangesAsync();

                TempData["Success"] = true;
                return(RedirectToAction(nameof(MyIdeas)));
            }
            ModelState.AddModelError(string.Empty, "Specify your idea.");
            return(View(idea));
        }
Esempio n. 5
0
        public async Task <IActionResult> CreateComment([Bind("CommentID,IdeaID,UserID,UserComment")] Comment comment)
        {
            var user = await _userManager.FindByIdAsync(comment.UserID);

            var  subscriptions = from i in _context.Subscribe select i;
            var  idea          = from i in _context.Idea select i;
            Idea userIdea;

            userIdea = await idea.Where(i => i.ID.Equals(comment.IdeaID)).FirstOrDefaultAsync();

            subscriptions    = subscriptions.Where(i => i.IdeaID.Equals(comment.IdeaID) && i.Subscribed == true);
            comment.UserID   = user.Id;
            comment.UserName = user.Firstname + " " + user.Lastname;
            string message = user.Firstname + " " + user.Lastname + " has commented on idea: " + _context.Idea.Where(i => i.ID == comment.IdeaID).FirstOrDefault().Title;

            if (ModelState.IsValid)
            {
                _context.Add(comment);
                await _context.SaveChangesAsync();

                foreach (var subscription in subscriptions)
                {
                    Notification newNotification = new Notification
                    {
                        IdeaID              = comment.IdeaID,
                        UserID              = comment.UserID,
                        TargetUserID        = subscription.UserID,
                        Username            = userIdea.Name,
                        NotificationMessage = message,
                        Checked             = false,
                        NotificationDate    = System.DateTime.Now
                    };
                    _context.Notifcation.Add(newNotification);
                }
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(IdeasController.Index), "Ideas"));
            }
            return(RedirectToAction(nameof(HomeController.Error), "Error"));
        }
Esempio n. 6
0
        public IActionResult Like(int id)
        {
            if (HttpContext.Session.GetInt32("LoggedIn") == 1)
            {
                User UserChecker = _context.Users.SingleOrDefault(x => x.Id == HttpContext.Session.GetInt32("UserId"));

                List <Like> LikeChecker = _context.Likes.Where(x => x.IdeaId == id).ToList();

                bool flag = false;

                foreach (var l in LikeChecker)
                {
                    if (l.UserId == UserChecker.Id)
                    {
                        flag = true;
                    }
                }

                if (flag == false)
                {
                    Like NewLike = new Like {
                        IdeaId    = id,
                        UserId    = (int)UserChecker.Id,
                        CreatedAt = DateTime.Now
                    };

                    _context.Add(NewLike);
                    _context.SaveChanges();
                }

                return(RedirectToAction("ideaboard"));
            }

            else
            {
                return(View("index"));
            }
        }
Esempio n. 7
0
 public IActionResult AddNew(IdeaViewModel model)
 {
     if (!LoggedIn())
     {
         return(RedirectToAction("Index", "Home"));
     }
     else
     {
         if (ModelState.IsValid)
         {
             Idea NewIdea = new Idea
             {
                 Description = model.Description,
                 UserId      = (int)HttpContext.Session.GetInt32("UserId")
             };
             _context.Add(NewIdea);
             _context.SaveChanges();
             return(RedirectToAction("Home"));
         }
         ViewBag.User = _context.Users.Single(u => u.UserId == HttpContext.Session.GetInt32("UserId"));
         return(View("Home", model));
     }
 }
        public async Task <IActionResult> SubscribeToIdea([Bind("SubscriptionID,UserID,Subscribed,IdeaID")] Subscribe subscribe)
        {
            var user = await _userManager.GetUserAsync(User);

            var  subscriptions = from i in _context.Subscribe select i;
            var  idea          = from i in _context.Idea select i;
            Idea userIdea;

            userIdea = await idea.Where(i => i.ID.Equals(subscribe.IdeaID)).FirstOrDefaultAsync();

            subscriptions    = subscriptions.Where(i => i.IdeaID.Equals(subscribe.IdeaID) && i.Subscribed == true);
            subscribe.UserID = user.Id;
            string message = "";

            if (subscribe.Subscribed == true)
            {
                subscribe.Subscribed = false;
                message = user.Firstname + " " + user.Lastname + " has Unsubscribed to the idea: " + _context.Idea.Where(i => i.ID == subscribe.IdeaID).FirstOrDefault().Title;
            }
            else if (subscribe.Subscribed == false)
            {
                subscribe.Subscribed = true;
                message = user.Firstname + " " + user.Lastname + " has Subscribed to the idea: " + _context.Idea.Where(i => i.ID == subscribe.IdeaID).FirstOrDefault().Title;
            }
            if (ModelState.IsValid)
            {
                if (!_context.Subscribe.Any(e => e.UserID.Equals(user.Id) && e.IdeaID == subscribe.IdeaID))
                {
                    _context.Add(subscribe);
                    await _context.SaveChangesAsync();
                }
                else
                {
                    try
                    {
                        _context.Subscribe.Update(subscribe);
                        await _context.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!SubscribeExists(subscribe.SubscriptionID))
                        {
                            return(NotFound());
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
                foreach (var subscription in subscriptions)
                {
                    Notification newNotification = new Notification
                    {
                        IdeaID              = subscribe.IdeaID,
                        UserID              = subscribe.UserID,
                        TargetUserID        = subscription.UserID,
                        Username            = userIdea.Name,
                        NotificationMessage = message,
                        Checked             = false,
                        NotificationDate    = System.DateTime.Now
                    };
                    _context.Notifcation.Add(newNotification);
                }
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(IdeasController.Index), "Ideas"));
            }
            return(RedirectToAction(nameof(HomeController.Error), "Error"));
        }