public void ViewZooById() { Console.Write("Enter Zoo ID: "); int userInput = int.Parse(Console.ReadLine()); _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token); var readTask = _client.GetAsync("https://localhost:44322/api/Zoo/"); var response = readTask.Result; if (response.IsSuccessStatusCode) { Console.Clear(); ZooListItems zoo = _client.GetAsync($"https://localhost:44322/api/Zoo/{userInput}").Result.Content.ReadAsAsync <ZooListItems>().Result; if (zoo != null) { Console.WriteLine($"Zoo ID: {zoo.ZooId}\n" + $"Name: {zoo.ZooName}\n" + $"Size: {zoo.ZooSize}\n" + $"Location: {zoo.Location}\n" + $"Admission: {zoo.Admission}\n" + $"AZA Accredited: {zoo.AZAAccredited}\n" + $"Average Rating: {zoo.AverageRating}\n" + $"{zoo.AttractionDetails}\n" + $"{zoo.AllZooReviews}"); } Console.ReadLine(); } }
// GET BY ID - READ public ZooListItems GetZooById(int id) { AttractionService attractionService = new AttractionService(); ReviewService reviewService = new ReviewService(); using (var ctx = new ApplicationDbContext()) { var entity = ctx .Zoos .SingleOrDefault(e => e.ZooId == id); foreach (Attraction attraction in entity.Attractions) { AttractionDetail attractionDetail = attractionService.GetAttractionById(attraction.AttId); var detail = new ZooListItems { ZooId = entity.ZooId, ZooName = entity.ZooName, Location = entity.Location, ZooSize = entity.ZooSize, AZAAccredited = entity.AZAAccredited, Admission = entity.Admission, AverageRating = entity.AverageRating, AttractionDetails = attractionDetail, AllZooReviews = new List <ReviewDetail>() //entity.AllZooReviews.Select(e=>reviewService.GetReviewById(e.ReviewId)).ToList() }; foreach (Review review in entity.AllZooReviews) { ReviewDetail reviewDetail = reviewService.GetReviewById(review.ReviewId); detail.AllZooReviews.Add(reviewDetail); } return(detail); } var zooDetail = new ZooListItems { ZooId = entity.ZooId, ZooName = entity.ZooName, Location = entity.Location, ZooSize = entity.ZooSize, AZAAccredited = entity.AZAAccredited, Admission = entity.Admission, AverageRating = entity.AverageRating, AllZooReviews = new List <ReviewDetail>() }; foreach (Review review in entity.AllZooReviews) { ReviewDetail reviewDetail = reviewService.GetReviewById(review.ReviewId); zooDetail.AllZooReviews.Add(reviewDetail); } return(zooDetail); } }
public void UpdateZoo() { Console.Clear(); Console.Write("Enter the Zoo ID for the Zoo You'd like to update: "); int id = int.Parse(Console.ReadLine()); _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token); var getTask = _client.GetAsync("https://localhost:44322/api/Zoo/"); var response = getTask.Result; ZooListItems oldZoo = new ZooListItems(); if (response.IsSuccessStatusCode) { Console.Clear(); oldZoo = _client.GetAsync("https://localhost:44322/api/Zoo/{id}").Result.Content.ReadAsAsync <ZooListItems>().Result; if (oldZoo != null) { Console.WriteLine($"Zoo ID: {oldZoo.ZooId}\n" + $"Name: {oldZoo.ZooName}\n" + $"Size: {oldZoo.ZooSize}\n" + $"Location: {oldZoo.Location}\n" + $"Admission: {oldZoo.Admission}\n" + $"AZA Accredited: {oldZoo.AZAAccredited}\n" + $"Average Rating: {oldZoo.AverageRating}\n" + $"{oldZoo.AttractionDetails}\n" + $"{oldZoo.AllZooReviews}"); } else { Console.WriteLine("The animals must have been hungry and ate that ID, please enter a valid Zoo ID."); } } Dictionary <string, string> newZoo = new Dictionary <string, string>(); //Console.Write("Zoo ID: "); //string zooId = Console.ReadLine(); // Need to figure out how to make it automatically use the key method for ZooId //newZoo.Add("ZooId", zooId); Console.Write("Name: "); string zooName = Console.ReadLine(); newZoo.Add("Name", zooName); Console.Write("Size: "); int zooSize = int.Parse(Console.ReadLine()); newZoo.Add("Size", zooSize.ToString()); Console.Write("Location: "); string zooLocation = Console.ReadLine(); newZoo.Add("Location", zooLocation); Console.Write("Admission: "); double zooAdmission = double.Parse(Console.ReadLine()); newZoo.Add("Admission", zooAdmission.ToString()); Console.Write("AZA Accredited: "); bool zooAZA = bool.Parse(Console.ReadLine()); newZoo.Add("AZA Accredited", zooAZA.ToString()); HttpContent newZooHTTP = new FormUrlEncodedContent(newZoo); var putResponse = _client.PostAsync("https://localhost:44322/api/Zoo/", newZooHTTP); if (putResponse.Result.IsSuccessStatusCode) { Console.WriteLine("Zoo Successfully Create"); } else { Console.WriteLine("Failed to create Zoo."); } Console.ReadKey(); }