public async Task <IActionResult> Create([Bind("DayId,FirstName,LastName")] Customer customer) { if (ModelState.IsValid) { //add IdenityUserId to customer object before it gets added to DB var userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier); customer.IdentityUserId = userId; TrashCollection.Models.Address address = new TrashCollection.Models.Address(); customer.Address = address; _context.Add(customer); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["IdentityUserId"] = new SelectList(_context.Users, "Id", "Id", customer.IdentityUserId); ViewData["DayId"] = new SelectList(_context.Weekdays, "Id", "Id", customer.DayId); var pickups = _context.Pickups.Where(p => p.CustomerId == customer.Id && p.Confirmed == false); ViewBag.pickups = pickups.ToList(); ViewBag.ConfirmedPickups = _context.Pickups.Where(p => p.CustomerId == customer.Id && p.Confirmed == true); foreach (Pickup item in ViewBag.ConfirmedPickups) { customer.Balance += item.Price; } return(View(customer)); }
public async Task <IActionResult> CreateAddressPage(TrashCollection.Models.Address address) { //Thought about deleting old addresses, decided it's not important in the scope of this excercise //try //{ // var deleteaddress = _context.Addresses.Where(c => c.Id == address.Id).Single(); // _context.Addresses.Remove(deleteaddress); // await _context.SaveChangesAsync(); //} //catch (Exception) //{ // //if the entry doesnt exist, do nothing. //} _context.Update(address); await _context.SaveChangesAsync(); var userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier); var customer = _context.Customers.Where(c => c.IdentityUserId == userId).Include(c => c.Address).Include(c => c.IdentityUser).Include(c => c.Weekday).Single(); customer.AddressId = address.Id; _context.Update(customer); await _context.SaveChangesAsync(); TrashCollection.Models.Address getcoord = _context.Addresses.Where(a => a.Id == customer.AddressId).Single(); ApiKey apikey = _context.ApiKeys.Where(k => k.Id == 1).Single(); //why is retrieving the string adding \r\n to my key? that cost me an hour. string key = apikey.Key.Substring(0, 32); string request = "http://www.mapquestapi.com/geocoding/v1/address?key=" + key + "&street=" + getcoord.AddressLineOne.Replace(' ', '+') + "&city=" + getcoord.City.Replace(' ', '+') + "&state=" + getcoord.State.Replace(' ', '+') + "&postalcode=" + getcoord.ZipCode; var addressdata = await client.GetAsync(request); //addressdata.Content.Headers.ContentDisposition.Name.WhoeverDesignedThis.CanEatMyShorts.tostring(); //begin horror show string letmesee = await addressdata.Content.ReadAsStringAsync(); //JObject letstryit = JObject.Parse(letmesee); //still not very friendly. if the mess below didnt work I would dig here some more. char[] splitter = new char[2] { '{', '}' }; string[] spilit = letmesee.Split(splitter); string[] splitmore = spilit[12].Split(':'); int commalocation = splitmore[1].IndexOf(','); string lattitude = splitmore[1].Substring(0, commalocation);//these backslashes make dealing with this extra confusing string longitude = splitmore[2]; TrashCollection.Models.Address setcoord = _context.Addresses.Where(a => a.Id == customer.AddressId).Single(); setcoord.Coordinate = lattitude + ", " + longitude; //holy crap thats a mess, but I don't know how to make and recieve a model without messing with my already established database _context.Addresses.Update(setcoord); await _context.SaveChangesAsync(); //_context.Update(setcoord); //await _context.SaveChangesAsync(); var pickups = _context.Pickups.Where(p => p.CustomerId == customer.Id && p.Confirmed == false); ViewBag.pickups = pickups.ToList(); ViewBag.ConfirmedPickups = _context.Pickups.Where(p => p.CustomerId == customer.Id && p.Confirmed == true); foreach (Pickup item in ViewBag.ConfirmedPickups) { customer.Balance += item.Price; } return(View("Index", customer)); }