From a596c075472b94b13f8c03c2806b842121b9907f Mon Sep 17 00:00:00 2001 From: neon_arch Date: Sun, 6 Aug 2023 20:31:30 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20chore:=20add=20code=20to=20cache?= =?UTF-8?q?=20the=20previous=20&=20next=20pages=20(#160)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/routes.rs | 47 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/src/server/routes.rs b/src/server/routes.rs index 0b07115..c203e20 100644 --- a/src/server/routes.rs +++ b/src/server/routes.rs @@ -13,6 +13,7 @@ use crate::{ use actix_web::{get, web, HttpRequest, HttpResponse}; use handlebars::Handlebars; use serde::Deserialize; +use tokio::join; /// A named struct which deserializes all the user provided search parameters and stores them. /// @@ -96,15 +97,49 @@ pub async fn search( } let page = match ¶ms.page { Some(page) => *page, - None => 0, + None => 1, }; - let url = format!( - "http://{}:{}/search?q={}&page={}", - config.binding_ip, config.port, query, page + let (_, results, _) = join!( + results( + format!( + "http://{}:{}/search?q={}&page={}", + config.binding_ip, + config.port, + query, + page - 1 + ), + &config, + query.to_string(), + page - 1, + req.clone(), + ), + results( + format!( + "http://{}:{}/search?q={}&page={}", + config.binding_ip, config.port, query, page + ), + &config, + query.to_string(), + page, + req.clone(), + ), + results( + format!( + "http://{}:{}/search?q={}&page={}", + config.binding_ip, + config.port, + query, + page + 1 + ), + &config, + query.to_string(), + page + 1, + req.clone(), + ) ); - let results_json = results(url, &config, query.to_string(), page, req).await?; - let page_content: String = hbs.render("search", &results_json)?; + + let page_content: String = hbs.render("search", &results?)?; Ok(HttpResponse::Ok().body(page_content)) } None => Ok(HttpResponse::Found()