Skip to content

Commit

Permalink
Update controller & templates
Browse files Browse the repository at this point in the history
  • Loading branch information
garamb1 committed Apr 9, 2024
1 parent aad4c81 commit bea06bc
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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";
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,8 +19,13 @@ public class InMemoryNewsRepository implements NewsRepository {
private Date updatedAt;

@Override
public Map<String, List<Article>> getAllArticles() {
return articles;
public List<Article> getArticlesByCountry(String country) {
return articles.get(country);
}

@Override
public boolean isCountrySupported(String country) {
return !isNull(articles) && articles.containsKey(country);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

public interface NewsRepository {

Map<String, List<Article>> getAllArticles();
List<Article> getArticlesByCountry(String country);

void updateAll(Map<String, List<Article>> newArticles);

Date getUpdatedAt();

boolean isCountrySupported(String country);
}
4 changes: 2 additions & 2 deletions src/main/resources/templates/fragments/footer.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<br>
<p>
<a href="/about"><b>RetroSearch</b></a> is an open source project built by <a href="/browse?url=https://garambo.it/about">@garambo</a>
| Search results from <a href="https://duckduckgo.com">DuckDuckGo</a>
| Open a <a href="https://github.com/garamb1/retrosearch/issues">GitHub Issue</a>
</p>
<p><b>Search</b> and <b>Browse</b> the WWW <em>like it's 1997</em> </p>
<p><b>Search</b> and <b>Browse</b> the WWW <em>like it's 1997</em> | Search results from <a href="https://duckduckgo.com">DuckDuckGo</a> </p>
<code>
HTML:
<th:block th:utext="${__${@applicationSettings.htmlVersion.id}__}"></th:block>
Expand Down
7 changes: 4 additions & 3 deletions src/main/resources/templates/fragments/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
<th:block th:if="${@environment.getProperty('retrosearch.news.enable') or
@environment.getProperty('retrosearch.sports.football.enable')}">
<h4>
<a href="/">Home</a>
<th:block th:if="${@environment.getProperty('retrosearch.news.enable')}">
News (
- News (
<th:block th:each="locale,iteration: ${@newsSettings.getLocales()}">
<a th:href="@{ /news(country=${locale.getDisplayCountry()}) }" th:text="${locale.getDisplayCountry()}"></a>
<th:block th:text="${!iteration.last ? '|' : ''}"></th:block>
</th:block>
) -
)
</th:block>

<th:block th:if="${@environment.getProperty('retrosearch.sports.football.enable')}">
<a href="/football">Football scores</a>
- <a href="/football">Football scores</a>
</th:block>
</h4>
</th:block>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/news.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<body>
<th:block th:insert="fragments/header.html"></th:block>

<h2>Latest news</h2>
<h2>Latest news | <em th:text="${country}"></em></h2>
<p>Updated: <em th:text="${updatedAt}"></em></p>
<ul>
<li th:each="element: ${articles}">
Expand Down

0 comments on commit bea06bc

Please sign in to comment.