Skip to content

Commit a261e76

Browse files
committed
improve(sitemap.xml): add a list of countries to make them indexable by search engines
Part of #1605
1 parent 3082909 commit a261e76

9 files changed

+54
-0
lines changed

src/main/java/ru/mystamps/web/feature/country/ApiCountryService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.http.ResponseEntity;
2525
import org.springframework.web.client.RestTemplate;
2626
import ru.mystamps.web.common.LinkEntityDto;
27+
import ru.mystamps.web.common.SitemapInfoDto;
2728

2829
import java.util.Date;
2930
import java.util.List;
@@ -72,6 +73,11 @@ public List<LinkEntityDto> findAllAsLinkEntities(String lang) {
7273
throw new UnsupportedOperationException();
7374
}
7475

76+
@Override
77+
public List<SitemapInfoDto> findAllForSitemap() {
78+
throw new UnsupportedOperationException();
79+
}
80+
7581
@Override
7682
public LinkEntityDto findOneAsLinkEntity(String slug, String lang) {
7783
throw new UnsupportedOperationException();

src/main/java/ru/mystamps/web/feature/country/CountryDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package ru.mystamps.web.feature.country;
1919

2020
import ru.mystamps.web.common.LinkEntityDto;
21+
import ru.mystamps.web.common.SitemapInfoDto;
2122

2223
import java.util.Date;
2324
import java.util.List;
@@ -37,6 +38,7 @@ public interface CountryDao {
3738
List<Integer> findIdsByNames(List<String> names);
3839
List<Integer> findIdsByNamePattern(String pattern);
3940
List<LinkEntityDto> findAllAsLinkEntities(String lang);
41+
List<SitemapInfoDto> findAllForSitemap();
4042
LinkEntityDto findOneAsLinkEntity(String slug, String lang);
4143
String findCountryOfLastCreatedSeriesByUser(Integer userId);
4244
String findPopularCountryInCollection(Integer userId);

src/main/java/ru/mystamps/web/feature/country/CountryService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package ru.mystamps.web.feature.country;
1919

2020
import ru.mystamps.web.common.LinkEntityDto;
21+
import ru.mystamps.web.common.SitemapInfoDto;
2122

2223
import java.util.Date;
2324
import java.util.List;
@@ -29,7 +30,9 @@ public interface CountryService {
2930
List<Integer> findIdsByNames(List<String> names);
3031
List<Integer> findIdsWhenNameStartsWith(String name);
3132
List<LinkEntityDto> findAllAsLinkEntities(String lang);
33+
List<SitemapInfoDto> findAllForSitemap();
3234
LinkEntityDto findOneAsLinkEntity(String slug, String lang);
35+
3336
long countAll();
3437
long countCountriesOf(Integer collectionId);
3538
long countBySlug(String slug);

src/main/java/ru/mystamps/web/feature/country/CountryServiceImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.transaction.annotation.Transactional;
2626
import ru.mystamps.web.common.LinkEntityDto;
2727
import ru.mystamps.web.common.LocaleUtils;
28+
import ru.mystamps.web.common.SitemapInfoDto;
2829
import ru.mystamps.web.common.SlugUtils;
2930
import ru.mystamps.web.support.spring.security.HasAuthority;
3031

@@ -116,6 +117,13 @@ public List<LinkEntityDto> findAllAsLinkEntities(String lang) {
116117
return countryDao.findAllAsLinkEntities(lang);
117118
}
118119

120+
// @todo #1605 CountryService.findAllForSitemap(): add unit test
121+
@Override
122+
@Transactional(readOnly = true)
123+
public List<SitemapInfoDto> findAllForSitemap() {
124+
return countryDao.findAllForSitemap();
125+
}
126+
119127
@Override
120128
@Transactional(readOnly = true)
121129
public LinkEntityDto findOneAsLinkEntity(String slug, String lang) {

src/main/java/ru/mystamps/web/feature/country/JdbcCountryDao.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import ru.mystamps.web.common.JdbcUtils;
2929
import ru.mystamps.web.common.LinkEntityDto;
3030
import ru.mystamps.web.common.RowMappers;
31+
import ru.mystamps.web.common.SitemapInfoDto;
3132
import ru.mystamps.web.support.spring.jdbc.MapStringIntegerResultSetExtractor;
3233

3334
import java.util.Collections;
@@ -55,6 +56,7 @@ public class JdbcCountryDao implements CountryDao {
5556
private final String findIdsByNamesSql;
5657
private final String findIdsByNamePatternSql;
5758
private final String findCountriesNamesWithSlugSql;
59+
private final String findAllForSitemapSql;
5860
private final String findCountryLinkEntityBySlugSql;
5961
@SuppressWarnings("PMD.LongVariable")
6062
private final String findFromLastCreatedSeriesByUserSql;
@@ -78,6 +80,7 @@ public JdbcCountryDao(Environment env, NamedParameterJdbcTemplate jdbcTemplate)
7880
this.findIdsByNamesSql = env.getRequiredProperty("country.find_ids_by_names");
7981
this.findIdsByNamePatternSql = env.getRequiredProperty("country.find_ids_by_name_pattern");
8082
this.findCountriesNamesWithSlugSql = env.getRequiredProperty("country.find_all_countries_names_with_slug");
83+
this.findAllForSitemapSql = env.getRequiredProperty("country.find_all_for_sitemap");
8184
this.findCountryLinkEntityBySlugSql = env.getRequiredProperty("country.find_country_link_info_by_slug");
8285
this.findFromLastCreatedSeriesByUserSql = env.getRequiredProperty("country.find_from_last_created_series_by_user");
8386
this.findPopularCountryInCollectionSql = env.getRequiredProperty("country.find_popular_country_from_user_collection");
@@ -212,6 +215,15 @@ public List<LinkEntityDto> findAllAsLinkEntities(String lang) {
212215
);
213216
}
214217

218+
@Override
219+
public List<SitemapInfoDto> findAllForSitemap() {
220+
return jdbcTemplate.query(
221+
findAllForSitemapSql,
222+
Collections.emptyMap(),
223+
RowMappers::forSitemapInfoDto
224+
);
225+
}
226+
215227
@Override
216228
public LinkEntityDto findOneAsLinkEntity(String slug, String lang) {
217229
Map<String, Object> params = new HashMap<>();

src/main/java/ru/mystamps/web/feature/country/TogglzWithFallbackCountryService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.slf4j.Logger;
2222
import org.slf4j.LoggerFactory;
2323
import ru.mystamps.web.common.LinkEntityDto;
24+
import ru.mystamps.web.common.SitemapInfoDto;
2425
import ru.mystamps.web.support.togglz.Features;
2526

2627
import java.util.Date;
@@ -79,6 +80,14 @@ public List<LinkEntityDto> findAllAsLinkEntities(String lang) {
7980
);
8081
}
8182

83+
@Override
84+
public List<SitemapInfoDto> findAllForSitemap() {
85+
return executeOneOf(
86+
apiService::findAllForSitemap,
87+
fallbackService::findAllForSitemap
88+
);
89+
}
90+
8291
@Override
8392
public LinkEntityDto findOneAsLinkEntity(String slug, String lang) {
8493
return executeOneOf(

src/main/java/ru/mystamps/web/feature/site/SiteConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public SitemapController sitemapController() {
8484
return new SitemapController(
8585
categoryService,
8686
collectionService,
87+
countryService,
8788
seriesService
8889
);
8990
}

src/main/java/ru/mystamps/web/feature/site/SitemapController.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import ru.mystamps.web.feature.category.CategoryService;
2828
import ru.mystamps.web.feature.collection.CollectionService;
2929
import ru.mystamps.web.feature.collection.CollectionUrl;
30+
import ru.mystamps.web.feature.country.CountryService;
3031
import ru.mystamps.web.feature.series.SeriesService;
3132
import ru.mystamps.web.feature.series.SeriesUrl;
3233

@@ -53,6 +54,7 @@ public class SitemapController {
5354

5455
private final CategoryService categoryService;
5556
private final CollectionService collectionService;
57+
private final CountryService countryService;
5658
private final SeriesService seriesService;
5759

5860
@GetMapping(SiteUrl.SITEMAP_XML)
@@ -85,6 +87,11 @@ public void generateSitemapXml(HttpServletResponse response) {
8587
// CheckStyle: ignore LineLength for next 1 line
8688
writer.print(createUrlEntry(dateFormatter, item, SeriesUrl.INFO_CATEGORY_PAGE, "{slug}"));
8789
}
90+
91+
for (SitemapInfoDto item : countryService.findAllForSitemap()) {
92+
// CheckStyle: ignore LineLength for next 1 line
93+
writer.print(createUrlEntry(dateFormatter, item, SeriesUrl.INFO_COUNTRY_PAGE, "{slug}"));
94+
}
8895

8996
writer.print("</urlset>\n");
9097
} catch (IOException ex) {

src/main/resources/sql/country_dao_queries.properties

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ country.find_all_countries_names_with_slug = \
9090
FROM countries c \
9191
ORDER BY CASE WHEN 'ru' = :lang THEN COALESCE(c.name_ru, c.name) ELSE c.name END
9292

93+
country.find_all_for_sitemap = \
94+
SELECT slug AS id \
95+
, updated_at \
96+
FROM countries \
97+
ORDER BY updated_at DESC
98+
9399
country.find_country_link_info_by_slug = \
94100
SELECT CASE WHEN 'ru' = :lang THEN COALESCE(c.name_ru, c.name) ELSE c.name END AS name \
95101
, c.slug \

0 commit comments

Comments
 (0)