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 CarpoolUpdate(string Name, string Description, int CarpoolId)
        {
            if (HttpContext.Session.GetInt32("LoggedInUserId") is null)
            {
                return(RedirectToAction("Index", "LoginReg"));
            }

            Carpool thisCarpool = dbContext.Carpools.Where(c => c.Id == CarpoolId).FirstOrDefault();

            thisCarpool.Name        = Name;
            thisCarpool.Description = Description;
            dbContext.Update(thisCarpool);
            dbContext.SaveChanges();
            return(RedirectToAction("CarpoolDefault", new { id = CarpoolId }));
        }
        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 OnPost()
        {
            if (!"requestTeam".Equals(action))
            {
                return(OnGet());
            }
            PoolContext dbCtx = new PoolContext();

            availableTeams   = dbCtx.Teams.OrderBy(t => t.Name).Where(t => !dbCtx.Owners.Select(o => o.TeamId).Contains(t.TeamId)).ToList();
            ownerAssignments = dbCtx.Owners.OrderBy(o => o.Username).ToList();
            checkCurrentOwnership(dbCtx);
            String userId   = HttpContext.User.FindFirst(ClaimTypes.Name).Value.ToString();
            String fullName = findFullName();

            if (canRequest)
            {
                Team  chosenTeam = availableTeams.ToArray()[new Random().Next(availableTeams.Count)];
                Owner newOwner   = new Owner {
                    Username = userId,
                    Team     = chosenTeam,
                    FullName = fullName
                };
                dbCtx.Owners.Add(newOwner);
                dbCtx.SaveChanges();
            }
            dbCtx.Dispose();
            return(OnGet());
        }
Beispiel #5
0
        public async Task <IActionResult> Post(PoolDatabase context, int id)
        {
            var pollModel = new PoolDatabase()
            {
                poll_description = context.Description
            };

            using (var PollDBContext = _context.Database.BeginTransaction())
            {
                try
                {
                    _context.Values.Add(pollModel);
                    _context.SaveChanges();
                    context.poll_id = pollModel.poll_id;

                    ICollection <Option> options = new HashSet <Option>();
                    _context.Options.ToList().ForEach(
                        description => options.Add(
                            new Option()
                    {
                        option_description = description.ToString(),
                        poll_id            = context.Id
                    })
                        );

                    _context.Options.AddRange(options);
                    _context.SaveChanges();
                    PollDBContext.Commit();
                }

                catch (Exception ex)
                {
                    PollDBContext.Rollback();
                }
            }
            return(Ok(new { poll_id = context.Id }));
        }
Beispiel #6
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 ProfileUpdate(UpdateUser form)
        {
            if (HttpContext.Session.GetInt32("LoggedInUserId") is null)
            {
                return(RedirectToAction("Index", "LoginReg"));
            }

            User logged_in_user = dbContext.Users.FirstOrDefault(u => u.Id == HttpContext.Session.GetInt32("LoggedInUserId"));

            ViewBag.logged_in_user = logged_in_user;

            if (ModelState.IsValid)
            {
                var hasher = new PasswordHasher <UpdateUser>();

                var result = hasher.VerifyHashedPassword(form, logged_in_user.PwHash, form.Password);

                if (result == 0)
                {
                    ModelState.AddModelError("UpdateEmail", "Please enter current password to update profile");
                    return(View("Profile"));
                }

                logged_in_user.FirstName = form.FirstName;
                logged_in_user.LastName  = form.LastName;
                logged_in_user.Email     = form.Email;
                logged_in_user.Phone     = form.Phone;
                logged_in_user.UpdatedAt = DateTime.Now;
                dbContext.SaveChanges();
                return(RedirectToAction("Profile"));
            }
            else
            {
                return(View("Profile"));
            }
        }
        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"));
            }
        }