public async Task <ActionResult <Place> > InsertPlace(PlaceForm place) { var city = new City(); city.Name = place.city; city.Npa = place.zip; var ctrlCity = new CitiesController(_context); await ctrlCity.PostCity(city); var cities = await _context.Cities.ToListAsync(); var insertedCity = cities.Where(x => x.Name == city.Name && x.Npa == city.Npa).First(); var location = new Location(); location.IdCity = insertedCity.IdCity; location.Lat = place.lat; location.Long = place.Long; location.Address = place.address; location.IdRegion = place.idRegion; var ctrlLocation = new LocationsController(_context); await ctrlLocation.PostLocation(location); var locations = await _context.Locations.ToListAsync(); var insertLocation = locations.Where(x => x.Address == location.Address && x.Lat == location.Lat && x.Long == location.Long && x.IdRegion == location.IdRegion).First(); var insertPlace = new Place(); // Add creator ID based on the Auth0 User ID found in the JWT token insertPlace.Creator = User.Claims.First(i => i.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier").Value; insertPlace.CreateAt = DateTime.Now; insertPlace.Name = place.name; insertPlace.Description = place.description; insertPlace.Email = place.email; insertPlace.Website = place.website; insertPlace.PhoneNumber = place.phoneNumber; insertPlace.IsOpenSunday = place.isOpenSunday; insertPlace.IsOpenSpecialDay = place.isOpenSpecialDay; insertPlace.IsVerified = place.isVerified; insertPlace.IdLocation = insertLocation.IdLocation; insertPlace.IdCategory = place.idCategory; insertPlace.IdType = place.idType; _context.Places.Add(insertPlace); await _context.SaveChangesAsync(); return(CreatedAtAction(nameof(GetPlace), new { id = insertPlace.IdPlace }, insertPlace)); }
public async Task <IActionResult> PutPlace(long id, PlaceForm place) { if (id != place.idPlace) { return(BadRequest()); } var oldPlace = await _context.Places.FindAsync(id); oldPlace.IdCategory = place.idCategory; oldPlace.IdType = place.idType; oldPlace.IsVerified = place.isVerified; oldPlace.IsOpenSpecialDay = place.isOpenSpecialDay; oldPlace.IsOpenSunday = place.isOpenSunday; oldPlace.Name = place.name; oldPlace.Description = place.description; oldPlace.Email = place.email; oldPlace.Website = place.website; oldPlace.PhoneNumber = place.phoneNumber; //City var city = new City(); city.Npa = place.zip; city.Name = place.city; var ctrlCity = new CitiesController(_context); await ctrlCity.PostCity(city); var cities = await _context.Cities.ToListAsync(); var insertedCity = cities.Where(x => x.Name == city.Name && x.Npa == city.Npa).First(); //Location var oldLocation = await _context.Locations.FindAsync(oldPlace.IdLocation); oldLocation.IdCity = insertedCity.IdCity; oldLocation.IdRegion = place.idRegion; oldLocation.Lat = place.lat; oldLocation.Long = place.Long; oldLocation.Address = place.address; var ctrlLocation = new LocationsController(_context); await ctrlLocation.PutLocation(oldLocation.IdLocation, oldLocation); var locations = await _context.Locations.ToListAsync(); var insertedLocation = locations.Where(x => x.Address == oldLocation.Address && x.Lat == oldLocation.Lat && x.Long == oldLocation.Long).First(); oldPlace.IdLocation = insertedLocation.IdLocation; _context.Entry(oldPlace).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PlaceExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }