Пример #1
0
        private void AddNewPet()
        {
            var NewPet = PetCreation();

            _petService.AddNewPet(NewPet);
            Console.Clear();
            Console.WriteLine("pet was added sucsessfully");
        }
Пример #2
0
 public IActionResult Post([FromBody] Pet pet)
 {
     try
     {
         _petService.AddNewPet(pet);
         return(StatusCode(201, pet));
     }
     catch (Exception e)
     {
         return(StatusCode(500, e.Message));
     }
 }
Пример #3
0
        public ActionResult <Pet> Post([FromBody] Pet pet)
        {
            try
            {
                var pettype   = pet.Type;
                var petReturn = petService.AddNewPet(pet);

                return(Created("", petReturn));
            }
            catch (Exception e)
            {
                return(StatusCode(418, e.Message));
            }
        }
Пример #4
0
        public ActionResult <Pet> Post([FromBody] Pet thePet)
        {
            if (string.IsNullOrEmpty(thePet.PetName) || thePet.PetType == null || thePet.PetColor == null || thePet.PetBirthday == null || thePet.PetSoldDate == null || string.IsNullOrEmpty(thePet.PetPreviousOwner) || thePet.PetOwner == null)
            {
                return(BadRequest("You have not entered all the required Pet data"));
            }
            PetType thePetType = thePet.PetType;

            if (thePetType.PetTypeId == 0)
            {
                if (string.IsNullOrEmpty(thePetType.PetTypeName))
                {
                    return(BadRequest("You have not entered all the information for a new PetType, please enter an id of an existing type, or a name for a new one."));
                }
            }
            List <PetColorPet> thePetColor = thePet.PetColor;

            foreach (var color in thePetColor)
            {
                if (color.petColorId == 0)
                {
                    if (string.IsNullOrEmpty(color.petColor.PetColorName))
                    {
                        return(BadRequest("You have not entered all the information for a new PetType, please enter an id of an existing type, or a name for a new one."));
                    }
                }
            }


            Owner theOwner = thePet.PetOwner;

            if (theOwner.OwnerId == 0)
            {
                if (string.IsNullOrEmpty(theOwner.OwnerFirstName) || string.IsNullOrEmpty(theOwner.OwnerLastName) || string.IsNullOrEmpty(theOwner.OwnerAddress) || string.IsNullOrEmpty(theOwner.OwnerPhoneNr) || string.IsNullOrEmpty(theOwner.OwnerEmail))
                {
                    return(BadRequest("You have not entered all the required Owner data, please enter the id of an existing owner, or all the info of a new one."));
                }
            }
            try
            {
                return(Created("Successfully created the following pet: ", _petService.AddNewPet(thePet)));
            }
            catch (Exception e)
            {
                return(StatusCode(500, e.Message));
            }
        }
Пример #5
0
        public async Task <IActionResult> AddPet([FromBody] AddNewPetViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var result = await _petService.AddNewPet(model);

                return(Ok(result));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Пример #6
0
        private void AddNewPet(string userName)
        {
            Console.WriteLine($"Hi {userName}, please enter the name of the new pet:");
            var thePetName = Console.ReadLine();

            var selectedSpecies = SelectSpecies(userName);
            int theSelectedSpecies;

            if (!int.TryParse(selectedSpecies, out theSelectedSpecies))
            {
                Console.WriteLine("You didn't enter a Nr for species, starting over.");
                AddNewPet(userName);
            }

            Console.WriteLine($"{userName}, please enter the colour of the pet:");
            var theColour = Console.ReadLine();

            Console.WriteLine($"Please enter the date of the pet's birthday in the format dd-mm-yyyy, {userName}:");
            var      selectedBirthday = Console.ReadLine();
            DateTime theSelectedBirthday;

            if (!DateTime.TryParse(selectedBirthday, out theSelectedBirthday))
            {
                Console.WriteLine("You didn't enter a propper date format, starting over.");
                AddNewPet(userName);
            }

            Console.WriteLine($"Please enter the date of purchase in the format dd-mm-yyyy, {userName}:");
            var      selectedPurchaseDate = Console.ReadLine();
            DateTime theSelectedPurchaseDate;

            if (!DateTime.TryParse(selectedPurchaseDate, out theSelectedPurchaseDate))
            {
                Console.WriteLine("You didn't enter a propper date format, starting over.");
                AddNewPet(userName);
            }

            Console.WriteLine($"{userName}, please enter the name of the previous owner:");
            var thePreviousOwner = Console.ReadLine();

            Console.WriteLine($"{userName}, please enter the price of the pet in £, nr only:");
            var  petPrice = Console.ReadLine();
            long thePetPrice;

            if (!long.TryParse(petPrice, out thePetPrice))
            {
                Console.WriteLine("You didn't enter a propper price, starting over.");
                AddNewPet(userName);
            }

            Console.WriteLine($"Almost done {userName}, please enter the name or ID of the owner who owns this pet, enter a negative numeric value to create a new owner.");
            //Can't use method FindOwnerByNameOrID as i need to be able to create a new one in the same menu.
            var   ownerID = Console.ReadLine();
            int   theOwnerID;
            Owner newOwner = null;

            if (int.TryParse(ownerID, out theOwnerID))
            {
                if (theOwnerID >= 0)
                {
                    newOwner = _ownerService.FindOwnerByID(theOwnerID);
                }
                else
                {
                    Console.WriteLine($"Creating a new owner now");
                    newOwner = AddNewOwner(userName);
                }
            }
            else
            {
                List <Owner> theLookedForOwners = _ownerService.FindOwnersByName(ownerID);
                if (theLookedForOwners.Count <= 0)
                {
                    Console.WriteLine($"I am sorry {userName}, I could not find any owners with that name, please start over.");
                    FindPetsByOwner(userName);
                }
                else if (theLookedForOwners.Count == 1)
                {
                    newOwner = theLookedForOwners[0];
                }
                else
                {
                    Console.WriteLine($"I am sorry {userName} i have found {theLookedForOwners.Count} Owners by that name:");
                    foreach (var owner in theLookedForOwners)
                    {
                        PrintOwner(owner);
                    }
                    Console.WriteLine($"Please enter the ID of the owner whose pets you want to see:");
                    if (int.TryParse(Console.ReadLine(), out theOwnerID))
                    {
                        newOwner = _ownerService.FindOwnerByID(theOwnerID);
                    }
                    else
                    {
                        Console.WriteLine($"You have not given me a valid ID, please try again.");
                        FindPetsByOwner(userName);
                    }
                }
            }
            if (newOwner == null)
            {
                Console.WriteLine("Something unexpected has gone wrong with the owner, starting over.");
                AddNewPet(userName);
            }
            Pet theNewPet = _petService.AddNewPet(thePetName, theSelectedSpecies, theColour, theSelectedBirthday, theSelectedPurchaseDate, thePreviousOwner, thePetPrice, newOwner);

            Console.WriteLine($"Congratulatons {userName}, you have successfully added {theNewPet.PetName}, with the ID: {theNewPet.PetId}. \n");
            DisplayMenu(userName);
        }