public async Task <ActionResult <ScavengerHunt> > PostScavengerHunt(int parkId) { // Indicate to the database context we want to add this new record var newScavengerHunt = new ScavengerHunt(); _context.ScavengerHunts.Add(newScavengerHunt); await _context.SaveChangesAsync(); var park = await _context.Parks. Include(park => park.AreaOfTheParks). ThenInclude(areaOfThePark => areaOfThePark.HiddenMickeys). Where(park => park.Id == parkId). FirstOrDefaultAsync(); var allMickeys = new List <HiddenMickey.Models.HiddenMickey>(); foreach (var areaOfThePark in park.AreaOfTheParks) { foreach (var mickey in areaOfThePark.HiddenMickeys) { allMickeys.Add(mickey); } } //Shuffle Mickeys for (var rightIndex = allMickeys.Count() - 1; rightIndex >= 1; rightIndex--) { var randomNumberGenerator = new Random(); var leftIndex = randomNumberGenerator.Next(rightIndex); var leftMickey = allMickeys[rightIndex]; var rightMickey = allMickeys[leftIndex]; allMickeys[rightIndex] = rightMickey; allMickeys[leftIndex] = leftMickey; } var someMickeys = allMickeys.Take(10); foreach (var mickey in someMickeys) { var scavengerHuntMickey = new ScavengerHuntMickey { HiddenMickeyId = mickey.Id, ScavengerHuntId = newScavengerHunt.Id, }; _context.ScavengerHuntMickeys.Add(scavengerHuntMickey); } await _context.SaveChangesAsync(); var scavengerHuntWithMickeys = await _context.ScavengerHunts. Where(scavengerHunt => scavengerHunt.Id == newScavengerHunt.Id). Include(scavengerHunt => scavengerHunt.ScavengerHuntMickeys). ThenInclude(scavengerHuntMickey => scavengerHuntMickey.HiddenMickey). FirstOrDefaultAsync(); // Return a response that indicates the object was created (status code `201`) and some additional // headers with details of the newly created object. return(CreatedAtAction("GetScavengerHunt", new { id = scavengerHuntWithMickeys.Id }, scavengerHuntWithMickeys)); }
private void OnEnable() { if (Instance != null && Instance != this) { Destroy(this.gameObject); } else { Instance = this; } }
public async Task <IActionResult> PutScavengerHunt(int id, ScavengerHunt scavengerHunt) { // If the ID in the URL does not match the ID in the supplied request body, return a bad request if (id != scavengerHunt.Id) { return(BadRequest()); } // Tell the database to consider everything in scavengerHunt to be _updated_ values. When // the save happens the database will _replace_ the values in the database with the ones from scavengerHunt _context.Entry(scavengerHunt).State = EntityState.Modified; try { // Try to save these changes. await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { // Ooops, looks like there was an error, so check to see if the record we were // updating no longer exists. if (!ScavengerHuntExists(id)) { // If the record we tried to update was already deleted by someone else, // return a `404` not found return(NotFound()); } else { // Otherwise throw the error back, which will cause the request to fail // and generate an error to the client. throw; } } // Return a copy of the updated data return(Ok(scavengerHunt)); }