Ejemplo n.º 1
0
        public static void AddAnimalToDb()
        {
            // Create animal
            var animalName   = "";
            var numberOfSeen = 0;
            var lastSeen     = "";

            System.Console.WriteLine("What is the name of the species?");
            animalName = Console.ReadLine();
            System.Console.WriteLine("How many times have you seen this animal?");
            numberOfSeen = int.Parse(Console.ReadLine());
            System.Console.WriteLine("And where did you last see the per-Animal?");
            // Similar to creating object within Js
            var newAnimal = new SeenAnimals
            {
                Species            = animalName,
                CountOfTimesSeen   = numberOfSeen,
                LocationOfLastSeen = lastSeen
            };
            // Similar to appending to the DOM
            var db = new SafariVacationContext();

            db.SeenAnimalsTable.Add(newAnimal);
            db.SaveChanges();
        }
Ejemplo n.º 2
0
        public ActionResult <SeenAnimals> Post([FromBody] SeenAnimals seenAnimals)
        {
            var db = new SeenAnimalsContext();

            db.SeenAnimals.Add(seenAnimals);
            db.SaveChanges();
            return(seenAnimals);
        }
Ejemplo n.º 3
0
        public ActionResult <SeenAnimals> AddAnimals([FromBody] SeenAnimals incomingSeenAnimals)
        {
            var db = new SafariVacationContext();

            db.SeenAnimals.Add(incomingSeenAnimals);
            db.SaveChanges();
            return(incomingSeenAnimals);
        }
Ejemplo n.º 4
0
        public ActionResult <SeenAnimals> Post([FromBody] SeenAnimals animals)
        {
            var db = new SafariVacationContext();

            db.SeenAnimals.Add(animals);
            db.SaveChanges();
            return(animals);
        }
Ejemplo n.º 5
0
        public ActionResult <SeenAnimals> Put([FromRoute] int id, [FromBody] SeenAnimals updatedData)
        {
            var db          = new SeenAnimalsContext();
            var seenAnimals = db.SeenAnimals.FirstOrDefault(animals => animals.Id == id);

            seenAnimals.Species            = updatedData.Species;
            seenAnimals.CountOfTimesSeen   = updatedData.CountOfTimesSeen;
            seenAnimals.LocationOfLastSeen = updatedData.LocationOfLastSeen;
            db.SaveChanges();
            return(updatedData);
        }
Ejemplo n.º 6
0
        public ActionResult <SeenAnimals> Post([FromBody] string species)
        {
            var animal = new SeenAnimals
            {
                Species            = species,
                LocationOfLastSeen = "Africa",
                CountOfTimesSeen   = 1,
            };

            this.db.SeenAnimals.Add(animal);
            this.db.SaveChanges();
            return(animal);
        }
Ejemplo n.º 7
0
        public ActionResult <SeenAnimals> Delete([FromBody] string species)
        {
            var animal = new SeenAnimals {
                Species = species,
            };
            var db = new SafariAdventureContext();

            db.SeenAnimals.Add(animal);

            db.SaveChanges();

            return(animal);
        }
Ejemplo n.º 8
0
        public ActionResult <SeenAnimals> Put([FromBody] string species)
        {
            var animal = new SeenAnimals {
                Species            = species,
                LocationOfLastSeen = "outside",
            };
            var db = new SafariAdventureContext();

            db.SeenAnimals.Add(animal);

            db.SaveChanges();

            return(animal);
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            Console.WriteLine("What Animals Did You See Today?");
            var species = Console.ReadLine();

            var newAnimal = new SeenAnimals {
                Species = species
            };

            var db = new Safari_VacationContext();

            db.SeenAnimalsTable.Add(newAnimal);
            db.SaveChanges();

            var AllAnimals = db.SeenAnimalsTable;

            foreach (var animal in AllAnimals)
            {
                System.Console.WriteLine(value: $"AllAnimals:{animal.Species}");
            }
        }
Ejemplo n.º 10
0
        public ActionResult <object> UpdateSeenAnimals([FromRoute] int id, [FromBody] SeenAnimals newInformation)
        {
            var db = new SafariVacationContext();

            var seenanimal = db.SeenAnimals.FirstOrDefault(animal => animal.Id == id);

            if (seenanimal != null)
            {
                seenanimal.Species            = newInformation.Species;
                seenanimal.CountOfTimesSeen   = newInformation.CountOfTimesSeen;
                seenanimal.LocationOfLastSeen = newInformation.LocationOfLastSeen;


                db.SaveChanges();
                return(Ok(seenanimal));
            }
            else
            {
                return(NotFound());
            }
        }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            using (var db = new SafariVacationContext())
            {
                //Prompt user for what they want to do to their Safari Collection.
                Console.WriteLine("(A)dd an animal.");
                Console.WriteLine("(D)isplay all animals from the Safari thus far.");
                Console.WriteLine("(U)pdate an animal.");
                Console.WriteLine("(S)pecial commands.");

                var userAction = Console.ReadLine();
                userAction = userAction.ToUpper();

                switch (userAction)
                {
                case "A":
                    addAnimal();
                    break;

                case "D":
                    displayAnimals();
                    break;

                case "U":
                    updateAnimal();
                    break;

                case "S":
                    specialPrompts();
                    break;

                default:
                    Console.WriteLine("Please choose a valid option!");
                    break;
                }

                void addAnimal()
                {
                    Console.WriteLine("Please tell me what animal you want to add!");
                    var _animalSpecies = Console.ReadLine();

                    //Create a new SeenAnimals instance called newSpecies and assign it a species.
                    var _newSpecies = new SeenAnimals {
                        Species = _animalSpecies
                    };

                    if (_animalSpecies != "")
                    {
                        //Add the new SeenAnimals instance to the SeenAnimalsTable in the SafariVacation database.
                        //Technically, we are just adding the newSpecies to a DbSet<SeenAnimals> in C#, which
                        //connects to the SQL server and adds a new row into the SeenAnimalsTable.
                        db.SeenAnimalsTable.Add(_newSpecies);

                        //commits changes from C# to the database.
                        db.SaveChanges();
                    }
                    else
                    {
                        //If no user input, display message below.
                        Console.WriteLine("Nothing to add");
                    }
                }

                void displayAnimals()
                {
                    //Display all of the animals that the user has seen
                    var _allSeenAnimals = db.SeenAnimalsTable;

                    foreach (var _animal in _allSeenAnimals)
                    {
                        Console.WriteLine($"Animal seen: {_animal.Species}");
                    }
                }

                void updateAnimal()
                {
                    Console.WriteLine("Please tell me what animal you want to update!");
                    var _animalSpecies = Console.ReadLine();

                    Console.WriteLine("How many times did you see this animal?");
                    var _animalSeenCount = Console.ReadLine();

                    Console.WriteLine("Where did you last see this animal?");
                    var _animalLocationSeen = Console.ReadLine();

                    //Update the CountOfTimesSeen and LocationOfLastSeen for an animal
                    var animalToUpdate = db.SeenAnimalsTable.FirstOrDefault(animal => animal.Species == _animalSpecies);

                    //Update properties of the selected animal.
                    animalToUpdate.CountOfTimesSeen   = int.Parse(_animalSeenCount);
                    animalToUpdate.LocationOfLastSeen = _animalLocationSeen;

                    //commits changes from C# to the database.
                    db.SaveChanges();
                }

                void displayAnimalsFromLocation()
                {
                    Console.WriteLine("From what location would you like to display all of the animals?");
                    var _location = Console.ReadLine();

                    var animalsToDisplay = db.SeenAnimalsTable.Where(animal => animal.LocationOfLastSeen == _location);

                    Console.WriteLine($"Animals from {_location}");
                    foreach (var _animalSpecies in animalsToDisplay)
                    {
                        Console.WriteLine($"Animal seen: {_animalSpecies.Species}");
                    }
                }

                void removeAnimalsFromLocation()
                {
                    Console.WriteLine("From what location would you like to remove all of the animals?");
                    var _location = Console.ReadLine();

                    var animalsToRemove = db.SeenAnimalsTable.Where(animal => animal.LocationOfLastSeen == _location);

                    foreach (var _animalSpecies in animalsToRemove)
                    {
                        db.SeenAnimalsTable.Remove(_animalSpecies);
                    }

                    db.SaveChanges();
                }

                void getTotalCountSeen()
                {
                    var _totalCount     = 0;
                    var _allSeenAnimals = db.SeenAnimalsTable;

                    foreach (var _animal in _allSeenAnimals)
                    {
                        _totalCount += _animal.CountOfTimesSeen;
                    }

                    Console.WriteLine($"You have seen a total of {_totalCount} animals!");
                }

                void getTotalCountSeenWizardsOfOzEdition()
                {
                    var _totalCount           = 0;
                    var _allWizardOfOzAnimals = db.SeenAnimalsTable.Where(animal => animal.Species == "Lion" || animal.Species == "Tiger" || animal.Species == "Bear");

                    foreach (var _animal in _allWizardOfOzAnimals)
                    {
                        _totalCount += _animal.CountOfTimesSeen;
                    }

                    Console.WriteLine($"You have seen a total of {_totalCount} animals of Wizard type!");
                }

                void specialPrompts()
                {
                    Console.WriteLine("(D)isplay all animals from a specific location.");
                    Console.WriteLine("(R)emove all animals from a specific location.");
                    Console.WriteLine("(G)et total count of all animals you have seen.");
                    Console.WriteLine("(W)izards of Oz edition: total count of lions, tigers, and bears seen.");

                    var specialUserAction = Console.ReadLine();

                    specialUserAction = specialUserAction.ToUpper();

                    switch (specialUserAction)
                    {
                    case "D":
                        displayAnimalsFromLocation();
                        break;

                    case "R":
                        removeAnimalsFromLocation();
                        break;

                    case "G":
                        getTotalCountSeen();
                        break;

                    case "W":
                        getTotalCountSeenWizardsOfOzEdition();
                        break;

                    default:
                        Console.WriteLine("Please choose a valid option!");
                        break;
                    }
                }
            }
        }
Ejemplo n.º 12
0
 public ActionResult <SeenAnimals> AddAnimal([FromBody] SeenAnimals animalToAdd)
 {
     db.SeenAnimals.Add(animalToAdd);
     db.SaveChanges();
     return(animalToAdd);
 }