diff --git a/src/main/java/it/garambo/retrosearch/controller/NewsController.java b/src/main/java/it/garambo/retrosearch/controller/NewsController.java index c63047e..adc44ea 100644 --- a/src/main/java/it/garambo/retrosearch/controller/NewsController.java +++ b/src/main/java/it/garambo/retrosearch/controller/NewsController.java @@ -6,6 +6,7 @@ import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; @Controller @ConditionalOnBean(NewsRepository.class) @@ -14,9 +15,14 @@ public class NewsController { @Autowired private NewsRepository newsRepository; @GetMapping("/news") - public String news(Model model) { + public String news(@RequestParam String country, Model model) { + if (country.isEmpty() || !newsRepository.isCountrySupported(country)) { + return "redirect:/"; + } + + model.addAttribute("country", country); model.addAttribute("updatedAt", newsRepository.getUpdatedAt()); - model.addAttribute("articles", newsRepository.getAllArticles()); + model.addAttribute("articles", newsRepository.getArticlesByCountry(country)); return "news"; } } diff --git a/src/main/java/it/garambo/retrosearch/news/repository/InMemoryNewsRepository.java b/src/main/java/it/garambo/retrosearch/news/repository/InMemoryNewsRepository.java index 03b8a25..f914d3f 100644 --- a/src/main/java/it/garambo/retrosearch/news/repository/InMemoryNewsRepository.java +++ b/src/main/java/it/garambo/retrosearch/news/repository/InMemoryNewsRepository.java @@ -1,5 +1,7 @@ package it.garambo.retrosearch.news.repository; +import static java.util.Objects.isNull; + import it.garambo.retrosearch.news.model.Article; import java.util.Date; import java.util.List; @@ -17,8 +19,13 @@ public class InMemoryNewsRepository implements NewsRepository { private Date updatedAt; @Override - public Map> getAllArticles() { - return articles; + public List
getArticlesByCountry(String country) { + return articles.get(country); + } + + @Override + public boolean isCountrySupported(String country) { + return !isNull(articles) && articles.containsKey(country); } @Override diff --git a/src/main/java/it/garambo/retrosearch/news/repository/NewsRepository.java b/src/main/java/it/garambo/retrosearch/news/repository/NewsRepository.java index e34f7f2..79c91ec 100644 --- a/src/main/java/it/garambo/retrosearch/news/repository/NewsRepository.java +++ b/src/main/java/it/garambo/retrosearch/news/repository/NewsRepository.java @@ -7,9 +7,11 @@ public interface NewsRepository { - Map> getAllArticles(); + List
getArticlesByCountry(String country); void updateAll(Map> newArticles); Date getUpdatedAt(); + + boolean isCountrySupported(String country); } diff --git a/src/main/resources/templates/fragments/footer.html b/src/main/resources/templates/fragments/footer.html index fa064f7..63acd05 100644 --- a/src/main/resources/templates/fragments/footer.html +++ b/src/main/resources/templates/fragments/footer.html @@ -1,9 +1,9 @@

RetroSearch is an open source project built by @garambo -| Search results from DuckDuckGo +| Open a GitHub Issue

-

Search and Browse the WWW like it's 1997

+

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: diff --git a/src/main/resources/templates/fragments/header.html b/src/main/resources/templates/fragments/header.html index b35f116..8dfb0f8 100644 --- a/src/main/resources/templates/fragments/header.html +++ b/src/main/resources/templates/fragments/header.html @@ -3,17 +3,18 @@

+ Home - News ( + - News ( - ) - + ) - Football scores + - Football scores

diff --git a/src/main/resources/templates/news.html b/src/main/resources/templates/news.html index 7c4e60f..efa5087 100644 --- a/src/main/resources/templates/news.html +++ b/src/main/resources/templates/news.html @@ -6,7 +6,7 @@ -

Latest news

+

Latest news |

Updated: