public void GetterSetterTestPopularTrip() { PopularTrip popularTrip = new PopularTrip { ID = 100, CityName = "Crabs", VacationName = "lobsters", AnswerCode = "shwoop", Popularity = 13, InUSA = true, LikesHot = true, Price = 1, HasChildren = false, LikesOutdoor = true }; Assert.Equal(100, popularTrip.ID); Assert.Equal("Crabs", popularTrip.CityName); Assert.Equal("lobsters", popularTrip.VacationName); Assert.Equal("shwoop", popularTrip.AnswerCode); Assert.Equal(13, popularTrip.Popularity); Assert.True(popularTrip.InUSA); Assert.True(popularTrip.LikesHot); Assert.Equal(1, popularTrip.Price); Assert.False(popularTrip.HasChildren); Assert.True(popularTrip.LikesOutdoor); }
/// <summary> /// Calls the GetPopularTrips method of the ITripController and sorts the result based on the Popularity property. Send the data to the trip/Popular view. /// </summary> /// <returns>Returns the trip/popular view with the sorted data.</returns> public async Task <IActionResult> Popular() { var popular = await _trips.GetPopularTrips(); PopularTrip[] popArr = popular.ToArray(); bool sorted = false; while (sorted == false) { int count = 0; for (int i = 0; i < popArr.Length; i++) { if (i < popArr.Length - 1) { if (popArr[i].Popularity > popArr[i + 1].Popularity) { PopularTrip sort = popArr[i]; popArr[i] = popArr[i + 1]; popArr[i + 1] = sort; count++; } } } if (count == 0) { sorted = true; } } return(View(popArr)); }
public async Task <IActionResult> Details(string AnswerCode, string CityName, string VacationName, int UserID) { SavedTrip trip = new SavedTrip { AnswerCode = AnswerCode, CityName = CityName, VacationName = VacationName, UserID = UserID, InUSA = (AnswerCode[0] == '1' ? true : false), LikesHot = (AnswerCode[2] == '1' ? true : false), HasChildren = (AnswerCode[6] == '1' ? true : false), LikesOutdoor = (AnswerCode[8] == '1' ? true : false) }; PopularTrip popTrip = new PopularTrip { AnswerCode = AnswerCode, CityName = CityName, VacationName = VacationName, Popularity = 0, InUSA = (AnswerCode[0] == '1' ? true : false), LikesHot = (AnswerCode[2] == '1' ? true : false), HasChildren = (AnswerCode[6] == '1' ? true : false), LikesOutdoor = (AnswerCode[8] == '1' ? true : false) }; switch (AnswerCode[4]) { case '1': trip.Price = 1; popTrip.Price = 1; break; case '2': trip.Price = 2; popTrip.Price = 2; break; case '3': trip.Price = 3; popTrip.Price = 3; break; } await _trips.SaveAsPopularTrip(popTrip); try { await _trips.SaveTrip(trip); } catch (Exception) { return(Ok("You already saved a vacation just like that! Try again.")); } return(RedirectToAction("MyVacations", UserID)); }
/// <summary> /// Saved the newly recommended trip, and the /// </summary> /// <param name="trip"></param> /// <returns></returns> public async Task SaveAsPopularTrip(PopularTrip trip) { var popularTrips = await _context.PopularTrips.ToListAsync(); bool found = false; foreach (var popTrip in popularTrips) { if (popTrip.AnswerCode == trip.AnswerCode) { popTrip.Popularity++; _context.Update(popTrip); await _context.SaveChangesAsync(); found = true; } } if (found == false) { await _context.AddAsync(trip); await _context.SaveChangesAsync(); } }