Example #1
0
        public void RemovePlant(int plantId)
        {
            var db      = new PlantContext();
            var validId = db.Plants.Any(plant => plant.Id == plantId);

            while (validId != true)
            {
                Console.WriteLine("That is not a valid Id number please try again");
                plantId = int.Parse(Console.ReadLine());
                validId = db.Plants.Any(plant => plant.Id == plantId);
            }
            if (validId == true)
            {
                var idToRemove = db.Plants.FirstOrDefault(plant => plant.Id == plantId);
                Console.WriteLine($"Are you sure you wand to remove {idToRemove.Species} with an Id of {idToRemove.Id}? (Y)/(N)");
                var confirmRemoval = Console.ReadLine().ToLower();
                while (confirmRemoval != "y" && confirmRemoval != "n")
                {
                    Console.WriteLine("Please take this seriously we're about to delete a plant. Input (Y) or (N) ");
                    confirmRemoval = Console.ReadLine().ToLower();
                }
                if (confirmRemoval == "y")
                {
                    Console.WriteLine($"{idToRemove.Id} has been removed.");
                    db.Remove(idToRemove);
                    db.SaveChanges();
                }
                else
                {
                    Console.WriteLine("Nothing has been removed.");
                }
            }
        }