public async Task <ActionResult <Scolding> > ScoldingForPet(int id) { var pet = await _context.Pets.FindAsync(id); if (pet == null) { return(NotFound()); } var deathDate = DateTime.Now; if ((deathDate - pet.LastInteractedWithDate).TotalDays > 3) { pet.IsDead = true; await _context.SaveChangesAsync(); return(Ok(new { Message = "Your pet is dead" })); } else { pet.LastInteractedWithDate = DateTime.Now; Scolding scoldings = new Scolding(); scoldings.PetId = pet.Id; scoldings.When = DateTime.Now; pet.HappinessLevel -= 5; _context.Scoldings.Add(scoldings); await _context.SaveChangesAsync(); return(Ok(scoldings)); } }
public async Task <ActionResult <Scolding> > CreateScolding(int id, Scolding newScolding) // | | // | Player deserialized from JSON from the body // | // GameNight ID comes from the URL { // First, lets find the game night (by using the ID) var pet = await _context.Pets.FindAsync(id); // If the game doesn't exist: return a 404 Not found. if (pet == null || pet.IsDead == true) { // Return a `404` response to the client indicating we could not find a game night with this id return(NotFound()); } // Associate the player to the given game night. newScolding.PetId = pet.Id; newScolding.When = DateTime.Now; pet.HappinessLevel -= 5; pet.LastInteractedWithDate = DateTime.Now; // Add the player to the database _context.Scoldings.Add(newScolding); await _context.SaveChangesAsync(); // Return the new player to the response of the API return(Ok(newScolding)); }
public async Task <ActionResult <Playtime> > PostScolding(int id) { var scoldingPet = await _context.Pets.FindAsync(id); if (scoldingPet.IsDead) { return(Ok("Your Tamagotchi has perished...")); } if (scoldingPet.HappinessLevel - 5 < 0) { return(Ok("Why? The Tamagotchi can't take any more scolding...")); } Scolding scolding = new Scolding(); scolding.PetId = id; scoldingPet.HappinessLevel -= 5; await _context.Scoldings.AddAsync(scolding); 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 (!PetExists(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(scolding)); }
public async Task <ActionResult <Pet> > CreatingScolding(int id, Scolding scolding) { var pet = await _context.Pets.FindAsync(id); if (pet == null) { return(NotFound()); } scolding.PetId = pet.Id; pet.HappinessLevel -= 5; _context.Scoldings.Add(scolding); await _context.SaveChangesAsync(); return(Ok(scolding)); }
public async Task <ActionResult <Pet> > PostScoldPetByIdAsync(int petId, Scolding scolding) { var pet = await _context.Pets.FindAsync(petId); if (pet == null) { return(NotFound()); } scolding.When = DateTime.Now; scolding.PetId = pet.PetId; _context.Scoldings.Add(scolding); pet.HappinessLevel -= 5; await _context.SaveChangesAsync(); return(Ok(pet)); }
public async Task <ActionResult <Scolding> > CreateScoldingForPet(int id, Scolding scolding) { var pet = await _context.Pets.FindAsync(id); if (pet == null) { return(NotFound()); } scolding.PetId = pet.Id; var updateHappiness = pet.HappinessLevel - 5; pet.HappinessLevel = updateHappiness; _context.Scoldings.Add(scolding); await _context.SaveChangesAsync(); return(Ok(scolding)); }
public async Task <ActionResult <Scolding> > AddScoldingForPet(int id, Scolding scolding) { var pet = await _context.Pets.FindAsync(id); if (pet == null) { return(NotFound()); } scolding.PetId = pet.Id; scolding.When = DateTime.Now; _context.Scoldings.Add(scolding); pet.HappinessLevel -= 5; await _context.SaveChangesAsync(); return(Ok(scolding)); }
public async Task <ActionResult <Scolding> > CreateScoldingForPet(int id, Scolding scolding) { var pet = await _context.Pets.FindAsync(id); if (pet == null) { return(NotFound()); } scolding.PetId = pet.Id; scolding.When = DateTime.Now; _context.Scoldings.Add(scolding); // Subtract five from pet happiness level. pet.HappinessLevel -= 5; // Adventure mode: Update LastInteractedWithDate pet.LastInteractedWithDate = DateTime.Now; await _context.SaveChangesAsync(); return(Ok(scolding)); }
public async Task <ActionResult <Playtime> > CreatePlaytimeForPet(int id, Scolding scolding) { // Find the pet in the database using `FindAsync` to look it up by id var pet = await _context.Pets.FindAsync(id); // If we didn't find anything, we receive a `null` in return if (pet == null) { // Return a `404` response to the client indicating we could not find a pet with this id return(NotFound()); } scolding.PetId = pet.Id; pet.HappinessLevel -= 5; // Indicate to the database context we want to add this new record _context.Scoldings.Add(scolding); await _context.SaveChangesAsync(); return(Ok(scolding)); }
public async Task <ActionResult <Scolding> > CreateScoldingForPet(int id, Scolding scolding) { // Find the pet by ID var pet = await _context.Pets.FindAsync(id); // If the pet doesn't exist, return 404 if (pet == null) { return(NotFound()); } // Associate the scolding to the pet scolding.PetId = pet.Id; // Adjust levels for pet pet.HappinessLevel -= 5; // Add the scolding to the database _context.Scoldings.Add(scolding); await _context.SaveChangesAsync(); // return the new scolding to the response of the API return(Ok(scolding)); }
public async Task <ActionResult <Playtime> > CreateScoldingForPets(int id) { var pet = await _context.Pets.FindAsync(id); if (pet == null) { // Return a `404` response to the client indicating we could not find a game with this id return(NotFound()); } // Associate the player to the given game. pet.HappinessLevel -= 5; pet.HungerLevel -= 3; var scolding = new Scolding(); scolding.When = DateTime.Now; scolding.PetId = pet.Id; // Add the player to the database _context.Scoldings.Add(scolding); await _context.SaveChangesAsync(); // Return the new player to the response of the API return(Ok(scolding)); }