public IActionResult Register(RegUser form)
        {
            if (ModelState.IsValid)
            {
                if (dbContext.Users.Any(u => u.Email == form.Email))
                {
                    ModelState.AddModelError("Email", "Email already in use");
                    return(View("Registration"));
                }

                User newUser = new User()
                {
                    FirstName = form.FirstName,
                    LastName  = form.LastName,
                    Email     = form.Email,
                    Phone     = form.Phone,
                };
                PasswordHasher <User> Hasher = new PasswordHasher <User>();
                newUser.PwHash = Hasher.HashPassword(newUser, form.Password);

                dbContext.Add(newUser);
                dbContext.SaveChanges();

                HttpContext.Session.SetInt32("LoggedInUserId", newUser.Id);

                return(RedirectToAction("Dashboard", "Carpool"));
            }
            return(View("Registration"));
        }
        public IActionResult RidershipJoin(int userId, int carpoolId)
        {
            if (HttpContext.Session.GetInt32("LoggedInUserId") is null)
            {
                return(RedirectToAction("Index", "LoginReg"));
            }

            if (HttpContext.Session.GetInt32("LoggedInUserId") == userId)
            {
                Ridership newRidership = new Ridership()
                {
                    UserId    = userId,
                    CarpoolId = carpoolId
                };

                dbContext.Add(newRidership);
                dbContext.SaveChanges();
                return(RedirectToAction("CarpoolDefault", "Carpool", new { id = carpoolId }));
            }
            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();

            ViewBag.logged_in_user = logged_in_user;
            ModelState.AddModelError("Error", "Cannot join ridership for another user");
            return(View("Dashboard", "Carpool"));
        }
        public IActionResult CarpoolCreate(CreateCarpool form)
        {
            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 (ModelState.IsValid)
            {
                Carpool newCarpool = new Carpool()
                {
                    Name   = form.Name,
                    UserId = logged_in_user.Id
                };

                dbContext.Add(newCarpool);
                dbContext.SaveChanges();

                int createdCarpoolId = dbContext.Carpools.Last().Id;
                return(RedirectToAction("CarpoolEdit", new { id = createdCarpoolId }));
            }
            else
            {
                ViewBag.logged_in_user = logged_in_user;
                return(View("Dashboard"));
            }
        }
Beispiel #4
0
        public async Task <IActionResult> LocationCreate(FormLocation form)
        {
            User CurrentUser = dbContext.Users.Where(u => u.Id == HttpContext.Session.GetInt32("LoggedInUserId")).FirstOrDefault();

            if (CurrentUser == null)
            {
                return(RedirectToAction("Logout", "LoginReg"));
            }

            if (ModelState.IsValid)
            {
                Location newLocation = new Location()
                {
                    LocationNickname = form.LocationNickname,
                    Address          = form.Address,
                    City             = form.City,
                    State            = form.State,
                    Zip    = form.Zip,
                    UserId = CurrentUser.Id,
                };

                string fullAddress = newLocation.Address + "+" + newLocation.City + "+" + newLocation.State + "+" + newLocation.Zip;


                var place = await HttpService.GetGeoCode(fullAddress);

                if (place[0] != "ERROR")
                {
                    System.Console.WriteLine($"Lat: {place[0]} Long: {place[1]}");
                    string coords = "{lat: " + place[0] + ", lng: " + place[1] + "}";
                    newLocation.Coords = coords;

                    dbContext.Add(newLocation);
                    dbContext.SaveChanges();
                    return(Redirect($"/commute/new/{HttpContext.Session.GetInt32("cpId")}"));
                }
                else
                {
                    System.Console.WriteLine("Invalid address");
                    ModelState.AddModelError("Address", "Invalid Address");
                    return(View("LocationNew"));
                }
            }
            return(View("LocationNew"));
        }
        public IActionResult CommuteCreate(CommuteForm form)
        {
            if (HttpContext.Session.Keys.Contains("LoggedInUserId"))
            {
                if (ModelState.IsValid)
                {
                    if (form.Monday == true)
                    {
                        Commute newCommute = new Commute();
                        newCommute.ArriveBy        = form.ArriveBy;
                        newCommute.Day             = DayOfWeek.Monday;
                        newCommute.StartLocationId = form.StartLocationId;
                        newCommute.EndLocationId   = form.EndLocationId;
                        newCommute.CarpoolId       = form.CarpoolId;

                        dbContext.Add(newCommute);
                        dbContext.SaveChanges();
                    }
                    if (form.Tuesday == true)
                    {
                        Commute newCommute = new Commute();
                        newCommute.ArriveBy        = form.ArriveBy;
                        newCommute.Day             = DayOfWeek.Tuesday;
                        newCommute.StartLocationId = form.StartLocationId;
                        newCommute.EndLocationId   = form.EndLocationId;
                        newCommute.CarpoolId       = form.CarpoolId;

                        dbContext.Add(newCommute);
                        dbContext.SaveChanges();
                    }
                    if (form.Wednesday == true)
                    {
                        Commute newCommute = new Commute();
                        newCommute.ArriveBy        = form.ArriveBy;
                        newCommute.Day             = DayOfWeek.Wednesday;
                        newCommute.StartLocationId = form.StartLocationId;
                        newCommute.EndLocationId   = form.EndLocationId;
                        newCommute.CarpoolId       = form.CarpoolId;

                        dbContext.Add(newCommute);
                        dbContext.SaveChanges();
                    }
                    if (form.Thursday == true)
                    {
                        Commute newCommute = new Commute();
                        newCommute.ArriveBy        = form.ArriveBy;
                        newCommute.Day             = DayOfWeek.Thursday;
                        newCommute.StartLocationId = form.StartLocationId;
                        newCommute.EndLocationId   = form.EndLocationId;
                        newCommute.CarpoolId       = form.CarpoolId;

                        dbContext.Add(newCommute);
                        dbContext.SaveChanges();
                    }
                    if (form.Friday == true)
                    {
                        Commute newCommute = new Commute();
                        newCommute.ArriveBy        = form.ArriveBy;
                        newCommute.Day             = DayOfWeek.Friday;
                        newCommute.StartLocationId = form.StartLocationId;
                        newCommute.EndLocationId   = form.EndLocationId;
                        newCommute.CarpoolId       = form.CarpoolId;

                        dbContext.Add(newCommute);
                        dbContext.SaveChanges();
                    }
                    if (form.Saturday == true)
                    {
                        Commute newCommute = new Commute();
                        newCommute.ArriveBy        = form.ArriveBy;
                        newCommute.Day             = DayOfWeek.Saturday;
                        newCommute.StartLocationId = form.StartLocationId;
                        newCommute.EndLocationId   = form.EndLocationId;
                        newCommute.CarpoolId       = form.CarpoolId;

                        dbContext.Add(newCommute);
                        dbContext.SaveChanges();
                    }
                    if (form.Sunday == true)
                    {
                        Commute newCommute = new Commute();
                        newCommute.ArriveBy        = form.ArriveBy;
                        newCommute.Day             = DayOfWeek.Sunday;
                        newCommute.StartLocationId = form.StartLocationId;
                        newCommute.EndLocationId   = form.EndLocationId;
                        newCommute.CarpoolId       = form.CarpoolId;

                        dbContext.Add(newCommute);
                        dbContext.SaveChanges();
                    }
                    return(RedirectToAction("CarpoolEdit", "Carpool", new { id = form.CarpoolId }));
                }
                else
                {
                    List <Location> allLocations = dbContext.Locations
                                                   .OrderByDescending(u => u.CreatedAt)
                                                   .ToList();
                    ViewBag.ListofLocations = allLocations;
                    ViewBag.carpoolid       = HttpContext.Session.GetInt32("cpId");
                    return(View("CommuteNew"));
                }
            }
            else
            {
                return(RedirectToAction("Index", "LoginReg"));
            }
        }