/// <summary> /// Make an Http GET request for one way flights corresponding to a search form. /// <param name="searchForm">The view model containing the expected input into the system.</param> /// <returns> /// The task corresponding to the ViewResult for the one-way flight prices. /// </returns> /// <remarks> /// Asynchronous because it makes an HTTP Get request via an HttpClient instance to get the list of /// one-way flight prices. /// </remarks> /// </summary> private async Task <IActionResult> GetOneWayFlights(HomeSearchViewModel searchForm) { // Construct the url based on the form string url = $"https://localhost:44320/browsequotes/oneWay?" + $"origin={searchForm.OriginAirport}" + $"&destination={searchForm.DestinationAirport}" + $"&departureDate={searchForm.TakeOffDate.Value.ToString("yyyy-MM-dd")}"; // Make the HTTP Get request var quotesResponse = await MakeHTTPGetRequest <BrowseQuotesPayload>(url); // Build the view model for the Flights view var viewModel = new HomePageQuotesViewModel { Quotes = quotesResponse.Quotes, IsRoundTrip = false, DepartureDate = searchForm.TakeOffDate.Value, DestinationAirport = searchForm.DestinationAirport, OriginAirport = searchForm.OriginAirport }; return(View("Flights", viewModel)); }
public async Task <ActionResult> Search([Bind(Include = "MaDanhMuc,MaDonViQLThucHien,SearchValue")] HomeSearchViewModel home) { ViewBag.SearchValue = home.SearchValue; int Size_Of_Page = 6; int No_Of_Page = 1; switch (home.MaDanhMuc) { case "1": ViewBag.MaCapDeTai = new SelectList(db.CapDeTais, "MaCapDeTai", "TenCapDeTai"); ViewBag.MaDonViQLThucHien = new SelectList(db.DonViQLs, "MaDonVi", "TenDonVI"); ViewBag.MaLinhVuc = new SelectList(QLKHrepo.GetListMenuLinhVuc(), "Id", "TenLinhVuc"); var detais = db.DeTais.Include(d => d.CapDeTai).Include(d => d.LoaiHinhDeTai).Include(d => d.DonViChuTri).Include(d => d.DonViQL).Include(d => d.LinhVuc).Include(d => d.XepLoai).Include(d => d.TinhTrangDeTai).Include(d => d.PhanLoaiSP).ToList(); if (!String.IsNullOrEmpty(home.MaDonViQLThucHien) && home.MaDonViQLThucHien != "0") { detais = detais.Where(p => p.MaDonViQLThucHien.ToString() == home.MaDonViQLThucHien).ToList(); } if (!String.IsNullOrEmpty(home.SearchValue)) { detais = detais.Where(p => p.TenDeTai.ToLower().Contains(home.SearchValue.ToLower())).ToList(); } int totalPage1 = (int)Math.Ceiling((decimal)detais.Count() / 6); ViewBag.TotalItem = detais.Count(); IPagedList <DeTai> pageOrders1 = new StaticPagedList <DeTai>(detais, No_Of_Page, 1, totalPage1); return(View("~/Views/DeTais/Index.cshtml", pageOrders1)); case "2": ViewBag.MaLinhVuc = new SelectList(QLKHrepo.GetListMenuLinhVuc(), "Id", "TenLinhVuc"); ViewBag.MaCapTapChi = new SelectList(db.CapTapChis, "MaCapTapChi", "TenCapTapChi"); ViewBag.MaPhanLoaiTapChi = new SelectList(db.PhanLoaiTapChis, "MaLoaiTapChi", "TenLoaiTapChi"); ViewBag.MaLoaiTapChi = new List <SelectListItem> { new SelectListItem { Text = "Trong Nước", Value = "1" }, new SelectListItem { Text = "Ngoài Nước", Value = "0" }, }; var baibaos = db.BaiBaos.Include(b => b.CapTapChi).Include(b => b.PhanLoaiTapChi).ToList(); if (!String.IsNullOrEmpty(home.SearchValue)) { baibaos = baibaos.Where(p => p.TenBaiBao.ToLower().Contains(home.SearchValue.ToLower())).ToList(); } int totalPage2 = (int)Math.Ceiling((decimal)baibaos.Count() / 6); ViewBag.TotalItem = baibaos.Count(); IPagedList <BaiBao> pageOrders2 = new StaticPagedList <BaiBao>(baibaos, No_Of_Page, 1, totalPage2); return(View("~/Views/BaiBaos/Index.cshtml", pageOrders2)); case "3": ViewBag.MaLinhVuc = new SelectList(QLKHrepo.GetListMenuLinhVuc(), "Id", "TenLinhVuc"); ViewBag.MaNXB = new SelectList(db.NhaXuatBans, "MaNXB", "TenNXB"); ViewBag.MaLoai = new SelectList(db.PhanLoaiSaches, "MaLoai", "TenLoai"); var sachGiaoTrinhs = db.SachGiaoTrinhs.Include(s => s.LinhVuc).Include(s => s.NhaXuatBan).Include(s => s.PhanLoaiSach).ToList(); if (!String.IsNullOrEmpty(home.SearchValue)) { sachGiaoTrinhs = sachGiaoTrinhs.Where(p => p.TenSach.ToLower().Contains(home.SearchValue.ToLower())).ToList(); } int totalPage3 = (int)Math.Ceiling((decimal)sachGiaoTrinhs.Count() / 6); ViewBag.TotalItem = sachGiaoTrinhs.Count(); IPagedList <SachGiaoTrinh> pageOrders3 = new StaticPagedList <SachGiaoTrinh>(sachGiaoTrinhs, No_Of_Page, 1, totalPage3); return(View("~/Views/SachGiaoTrinhs/Index.cshtml", pageOrders3)); default: ViewBag.MaCNDaoTao = new SelectList(db.ChuyenNganhs.ToList(), "MaChuyenNganh", "TenChuyenNganh"); ViewBag.MaHocHam = new SelectList(db.HocHams.ToList(), "MaHocHam", "TenHocHam"); ViewBag.MaHocVi = new SelectList(db.HocVis.ToList(), "MaHocVi", "TenHocVi"); ViewBag.MaDonViQL = new SelectList(db.DonViQLs.ToList(), "MaDonVi", "TenDonVI"); ViewBag.MaNgachVienChuc = new SelectList(db.NgachVienChucs.ToList(), "MaNgach", "TenNgach"); var nhaKhoaHocs = db.NhaKhoaHocs.Include(n => n.ChuyenNganh).Include(n => n.DonViQL).Include(n => n.HocHam).Include(n => n.HocVi).Include(n => n.NgachVienChuc).ToList(); if (!String.IsNullOrEmpty(home.MaDonViQLThucHien) && home.MaDonViQLThucHien != "0") { nhaKhoaHocs = nhaKhoaHocs.Where(p => p.MaDonViQL.ToString() == home.MaDonViQLThucHien).ToList(); } if (!String.IsNullOrEmpty(home.SearchValue)) { nhaKhoaHocs = nhaKhoaHocs.Where(p => (p.HoNKH + " " + p.TenNKH).ToLower().Contains(home.SearchValue.ToLower())).ToList(); } var lstNKH = new List <NhaKhoaHocViewModel>(); for (int i = 0; i < nhaKhoaHocs.Count; i++) { NhaKhoaHocViewModel nkh = NhaKhoaHocViewModel.Mapping(nhaKhoaHocs[i]); lstNKH.Add(nkh); } int totalPage = (int)Math.Ceiling((decimal)lstNKH.Count() / 6); ViewBag.TotalItem = lstNKH.Count(); IPagedList <NhaKhoaHocViewModel> pageOrders = new StaticPagedList <NhaKhoaHocViewModel>(lstNKH, No_Of_Page, 1, totalPage); return(View("~/Views/NhaKhoaHocs/Index.cshtml", pageOrders)); } }
public async Task <IActionResult> Search(HomeSearchViewModel searchForm) { _logger.LogInformation($"Received form from the user:"******"Destination Airport: {searchForm.DestinationAirport}" + Environment.NewLine + $"Origin Airport: {searchForm.OriginAirport}" + Environment.NewLine + $"Return Date: {searchForm.ReturnDate}" + Environment.NewLine + $"Takeoff Date: {searchForm.TakeOffDate}" + Environment.NewLine + $"Round Trip: {searchForm.IsRoundTrip}"); _logger.LogInformation("WebApp requesting Airport data from WebAPI"); // Get the list of airports for validation var airports = await GetAirports(); _logger.LogInformation($"WebAPI responded with data for {airports.Count} airports."); // Make sure the Origin Airport name is valid if (!airports.Select(a => a.AirportName).Contains(searchForm.OriginAirport)) { ModelState.AddModelError("OriginAirport", "Invalid airport name"); } // Make sure the Destination Airport name is valid if (!airports.Select(a => a.AirportName).Contains(searchForm.DestinationAirport)) { ModelState.AddModelError("DestinationAirport", "Invalid airport name"); } // Perform other validation. See the data annotations on the HomeSearchViewModel class for more. // If the validation fails, return ViewResult for the Home page with error information if (!ModelState.IsValid) { _logger.LogWarning("Form validation failed."); searchForm.Airports = airports; return(View("Index", searchForm)); } // Send the appropriate HTTP Get request or return to the home page if the form is in an invalid state. if (!searchForm.IsRoundTrip) { _logger.LogInformation("WebApp querying WebAPI for one-way flight prices."); try { return(await GetOneWayFlights(searchForm)); } catch (FlightQuoteApiException ex) // This indicates a failure on the WebAPI side { var viewModel = new HttpErrorPageViewModel(ex.StatusCode); return(View("HttpErrorPage", viewModel)); } catch (HttpRequestException) // This indicates a failure such as internet loss { var viewModel = new HttpErrorPageViewModel(System.Net.HttpStatusCode.ServiceUnavailable); return(View("HttpErrorPage", viewModel)); } } _logger.LogInformation("WebApp querying WebAPI for round-trip flight prices."); try { // We've passed all validation and the only option remaining is roundtrip flights return(await GetRoundTripFlights(searchForm)); } catch (FlightQuoteApiException ex) // This indicates a failure on the WebAPI side { var viewModel = new HttpErrorPageViewModel(ex.StatusCode); return(View("HttpErrorPage", viewModel)); } catch (HttpRequestException) // This indicates a failure such as internet loss { var viewModel = new HttpErrorPageViewModel(System.Net.HttpStatusCode.ServiceUnavailable); return(View("HttpErrorPage", viewModel)); } }
public ActionResult Search() { var vm = new HomeSearchViewModel(); return(View(vm)); }
public IActionResult Search(HomeSearchViewModel homeSearchViewModel) { homeSearchViewModel.TypeList = _outdoorPowerRepository.GetInventoryTypes().Select( pt => new SelectListItem { Value = pt.Id.ToString(), Text = pt.Name }).ToList(); homeSearchViewModel.EngineBrandList = _outdoorPowerRepository.GetEngineBrands().Select( pt => new SelectListItem { Value = pt, Text = pt } ).ToList(); homeSearchViewModel.MakeList = _outdoorPowerRepository.GetInventoryMakes().Select( pt => new SelectListItem { Value = pt.Id.ToString(), Text = pt.Name }).ToList(); if (homeSearchViewModel.QMake > 0) { homeSearchViewModel.ModelList = _outdoorPowerRepository.GetInventoryModels( typeId: homeSearchViewModel.QType, inventoryMakeId: homeSearchViewModel.QMake ).Select( pt => new SelectListItem { Value = pt.Id.ToString(), Text = pt.Name }).ToList(); } if (homeSearchViewModel.QModel > 0) { homeSearchViewModel.QModelOptionList = _outdoorPowerRepository.GetInventoryModelOptions( inventoryModelId: homeSearchViewModel.QModel ).Select( pt => new SelectListItem { Value = pt.Id.ToString(), Text = pt.Name }).ToList(); } homeSearchViewModel.SearchResults = _outdoorPowerRepository.SearchInventory(homeSearchViewModel); homeSearchViewModel.NavStart = homeSearchViewModel.PageNum; homeSearchViewModel.NavEnd = homeSearchViewModel.TotalResults / homeSearchViewModel.ResultsPerPage; if (homeSearchViewModel.TotalResults % homeSearchViewModel.ResultsPerPage > 0) { homeSearchViewModel.NavEnd += 1; } if (homeSearchViewModel.NavStart <= 7) { homeSearchViewModel.NavStart = 1; } else if (homeSearchViewModel.NavStart > 7) { homeSearchViewModel.NavEnd -= 2; } if (homeSearchViewModel.NavEnd > homeSearchViewModel.NavStart + 7) { homeSearchViewModel.NavEnd = homeSearchViewModel.NavStart + 7; } return(View(homeSearchViewModel)); }