public IActionResult Register(UserRegisterViewModel model, IFormFile ProfilePic) //Register User Route //pass the file from the form { if (ModelState.IsValid) { if (ProfilePic != null) //aka if a picture was uploaded { var filename = Path.Combine(HE.WebRootPath + "/images", Path.GetFileName(ProfilePic.FileName)); //stores a string of where the new file root should be String filestring = GetRandString(); //returns a string of numbers to randomize the file names String[] newfile = filename.Split("."); //creates an array of the file string before the period and after so we can add the randomized string String newFileString = newfile[0] + filestring + "." + newfile[1]; //puts the string back together including the random string String[] splitrootfile = newFileString.Split("wwwroot"); //creates a string with the path necessary to store and retrieve the image from the images folder ProfilePic.CopyTo(new FileStream(newFileString, FileMode.Create)); //stores the new file into our full path which is what we made prior to splitting by wwwroot User newUser = new User { FirstName = model.FirstName, LastName = model.LastName, Email = model.Email, UserName = model.UserName, Password = model.Password, ProfilePic = splitrootfile[1], //store only the second half of the split path into database. We only need this part of the path to access the images Token = 1, Score = 0, UserLevel = 0 }; PasswordHasher <User> Hasher = new PasswordHasher <User>(); newUser.Password = Hasher.HashPassword(newUser, newUser.Password); //Hash password _context.Add(newUser); _context.SaveChanges(); User activeUser = _context.users.Single(u => (string)u.Email == (string)model.Email); //re-obtain newly created User for Id information if (activeUser.UserId == 1) { activeUser.UserLevel = 9;//First user to admin _context.SaveChanges(); } HttpContext.Session.SetString("userName", activeUser.UserName); HttpContext.Session.SetInt32("activeUser", activeUser.UserId); TempData["pic"] = splitrootfile[1]; //for testing only to display the image path return(RedirectToAction("Dashboard", "Cleanup")); //Go to actual site } } return(View("RegistrationPartial")); //Failed registration attempt goes here }
public IActionResult AddCleanup(CleanupViewModel model) { int?activeId = HttpContext.Session.GetInt32("activeUser"); if (activeId != null) //Checked to make sure user is actually logged in { User activeUser = _context.users.Single(u => u.UserId == (int)activeId); if (activeUser.Token > 0) { if (ModelState.IsValid) { CleanupEvent newCleanup = new CleanupEvent { Title = model.Title, DescriptionOfArea = model.DescriptionOfArea, DescriptionOfTrash = model.DescriptionOfTrash, UserId = (int)activeId, Pending = true, Value = 0, Latitude = model.Latitude, Longitude = model.Longitude }; _context.Add(newCleanup); activeUser.Token -= 1; _context.SaveChanges(); CleanupEvent freshCleanup = _context.cleanups.OrderBy(c => c.CreatedAt).Reverse().First(); return(RedirectToAction("AddPhoto", new { id = freshCleanup.CleanupId })); } } else { ViewBag.error = "Insufficient tokens to report trash, go and help out more!"; } return(View("NewCleanup")); } return(RedirectToAction("Index", "User")); }