예제 #1
0
        public IActionResult RidershipLeave(int userId, int carpoolId)
        {
            if (HttpContext.Session.GetInt32("LoggedInUserId") is null)
            {
                return(RedirectToAction("Index", "LoginReg"));
            }

            User logged_in_user = dbContext.Users.Where(u => u.Id == HttpContext.Session.GetInt32("LoggedInUserId"))
                                  .Include(u => u.carpools)
                                  .Include(u => u.riderships)
                                  .ThenInclude(r => r.carpool)
                                  .FirstOrDefault();

            if (HttpContext.Session.GetInt32("LoggedInUserId") == userId)
            {
                Ridership ridershipToRemove = dbContext.Riderships.Where(r => (r.CarpoolId == carpoolId) && (r.UserId == userId))
                                              .FirstOrDefault();
                dbContext.Remove(ridershipToRemove);
                dbContext.SaveChanges();


                ViewBag.logged_in_user = logged_in_user;

                return(RedirectToAction("Dashboard", "Carpool"));
            }

            ViewBag.logged_in_user = logged_in_user;
            ModelState.AddModelError("Error", "Cannot leave ridership for another user");
            return(View("Dashboard", "Carpool"));
        }
예제 #2
0
        public IActionResult CarpoolDelete(int id)
        {
            if (HttpContext.Session.GetInt32("LoggedInUserId") is null)
            {
                return(RedirectToAction("Index", "LoginReg"));
            }

            Carpool carpoolToRemove = dbContext.Carpools.Where(c => c.Id == id)
                                      .Include(c => c.user)
                                      .FirstOrDefault();

            User logged_in_user = dbContext.Users.Where(u => u.Id == HttpContext.Session.GetInt32("LoggedInUserId"))
                                  .Include(u => u.carpools)
                                  .Include(u => u.riderships)
                                  .ThenInclude(r => r.carpool)
                                  .FirstOrDefault();

            if (carpoolToRemove is null)
            {
                ViewBag.logged_in_user = logged_in_user;
                ModelState.AddModelError("Error", "Carpool for deletion does not exist");
                return(View("Dashboard"));
            }
            if (logged_in_user.Id == carpoolToRemove.user.Id)
            {
                dbContext.Remove(carpoolToRemove);
                dbContext.SaveChanges();
                ViewBag.logged_in_user = logged_in_user;
                return(RedirectToAction("Dashboard"));
            }

            ViewBag.logged_in_user = logged_in_user;
            ModelState.AddModelError("Error", "Insufficient privileges to delete that carpool");
            return(View("Dashboard"));
        }
예제 #3
0
        public IActionResult CommuteDelete(int id)
        {
            if (HttpContext.Session.GetInt32("LoggedInUserId") is null)
            {
                return(RedirectToAction("Index", "LoginReg"));
            }

            Commute thisCommute = dbContext.Commutes
                                  .Where(u => u.Id == id)
                                  .FirstOrDefault();

            dbContext.Remove(thisCommute);
            dbContext.SaveChanges();
            int carpoolId = thisCommute.CarpoolId;

            return(RedirectToAction("CarpoolEdit", "Carpool", new { id = carpoolId }));
        }