示例#1
0
        public ActionResult <EggDTO> BreedAnimals(int id, int idOtherAnimal, string name)
        {
            try{
                PetOwner user = GetUser();

                Animal animalOne = GetAnimalWithIDAndUser(id, user);
                Animal animalTwo = GetAnimalWithIDAndUser(idOtherAnimal, user);
                Egg    egg       = animalOne.Breed(animalTwo, name);

                _eggRepo.AddEgg(egg);
                _eggRepo.SaveChanges();

                return(Created($"Api/PetOwner/Eggs/{egg.ID}", new EggDTO(egg)));
            }catch (Exception e) {
                ModelState.AddModelError("Error", e.Message);
                return(BadRequest(ModelState));
            }
        }
        public ActionResult <EggDTO> BreedAnimalFromListing(int id, int idOfAnimal, string name)
        {
            try{
                var listing = GetListing(id);
                var user    = GetOwner();
                var animal  = _aRepo.GetAnimal(idOfAnimal);

                if (animal.Owner != user)
                {
                    throw new Exception("You aren't the owner of the animal you're trying to breed");
                }

                var egg = listing.AcceptBreeding(user, animal, name);
                _eggRepo.AddEgg(egg);

                _listingRepo.RemoveListing(listing);
                _eggRepo.SaveChanges();

                return(Created($"Api/PetOwner/Eggs/{egg.ID}", new EggDTO(egg)));
            } catch (Exception e) {
                ModelState.AddModelError("Error", e.Message);
                return(BadRequest(ModelState));
            }
        }