/* * Retrieve owners who own cat. * Extract data requiered for view to display. */ public PetOwners GetPetOwnerNamesByPetCat() { PetOwners petOwnersByGender = null; //Catch the exception if any and create error response. For now throw it and handle in main IList <Person> petOwners = null; try { petOwners = ServiceProvider.GetPetOwnersByPetCat(); }catch (AppException e) { throw; } if (petOwners != null && petOwners.Count > 0) { petOwnersByGender = new PetOwners(); //List of female owner names petOwnersByGender.FemaleOwners = petOwners.Where(p => p.Gender == GenderEnum.Female) .Select(p => p.Name) .OrderBy(p => p) .ToList(); //List of male owner names petOwnersByGender.MaleOwners = petOwners.Where(p => p.Gender == GenderEnum.Male) .Select(p => p.Name) .OrderBy(p => p) .ToList(); } return(petOwnersByGender); }
static void Main(string[] args) { //Init controller with repository. Data provider as service IPersonController personController = new PersonController(new PersonWebServiceRepository()); //Retrieve and list sorted female pet owners who has cat PetOwners petOwners = null; try { petOwners = personController.GetPetOwnerNamesByPetCat(); int count = 1; System.Console.WriteLine("\nList of female owners."); if (petOwners == null || petOwners.FemaleOwners == null || petOwners.FemaleOwners.Count == 0) { System.Console.WriteLine("NONE."); } else { foreach (var ownerName in petOwners.FemaleOwners) { System.Console.WriteLine(string.Format("{0}. {1}", count++, ownerName)); } } //Retrieve and list sorted male pet owners who has cat System.Console.WriteLine("\nList of male owners."); if (petOwners == null || petOwners.MaleOwners == null || petOwners.MaleOwners.Count == 0) { System.Console.WriteLine("NONE."); } else { count = 1; foreach (var ownerName in petOwners.MaleOwners) { System.Console.WriteLine(string.Format("{0}. {1}", count++, ownerName)); } } } catch (AppException e) { System.Console.WriteLine(string.Format("Error: {0}", e.Message)); System.Console.WriteLine(e.ToString()); } catch (Exception e) { System.Console.WriteLine("Something went wrong unexpected."); System.Console.WriteLine(e.ToString()); } }
public async Task <ActionResult> Index() { var peopleAndPets = await peopleApiClient.GetPeopleAndTheirPets(); var model = new PetOwners { PetsOwnedByMale = peopleAndPets.GetAllCatsOwnedByMales(), PetsOwnedByFemale = peopleAndPets.GetAllCatsOwnedByFemales() }; // if no cats if (model.PetsOwnedByFemale.Count == 0 && model.PetsOwnedByMale.Count == 0) { return(Content("No Cats Found")); } return(View(model)); }