Skip to content

Commit ae0b00b

Browse files
committed
Guess Country
Guess Country gh571_prediction_country add guess country fixes after merge fix query cleaning and return String
1 parent 0f1a51f commit ae0b00b

File tree

17 files changed

+131
-0
lines changed

17 files changed

+131
-0
lines changed

src/main/java/ru/mystamps/web/Url.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public final class Url {
5151
public static final String INFO_SERIES_PAGE = "/series/{id}";
5252
public static final String ADD_IMAGE_SERIES_PAGE = "/series/{id}/image";
5353
public static final String SEARCH_SERIES_BY_CATALOG = "/series/search/by_catalog";
54+
public static final String INFO_COUNTRY_SERIES_PAGE = "/series/country_slug";
5455

5556
public static final String ADD_CATEGORY_PAGE = "/category/add";
5657
public static final String LIST_CATEGORIES_PAGE = "/category/list";
@@ -125,6 +126,7 @@ public static Map<String, String> asMap(boolean serveContentFromSingleHost) {
125126
map.put("INFO_SERIES_PAGE", INFO_SERIES_PAGE);
126127
map.put("ADD_IMAGE_SERIES_PAGE", ADD_IMAGE_SERIES_PAGE);
127128
map.put("SEARCH_SERIES_BY_CATALOG", SEARCH_SERIES_BY_CATALOG);
129+
map.put("INFO_COUNTRY_SERIES_PAGE", INFO_COUNTRY_SERIES_PAGE);
128130
map.put("ADD_CATEGORY_PAGE", ADD_CATEGORY_PAGE);
129131
map.put("INFO_CATEGORY_PAGE", INFO_CATEGORY_PAGE);
130132
map.put("LIST_CATEGORIES_PAGE", LIST_CATEGORIES_PAGE);

src/main/java/ru/mystamps/web/controller/SeriesController.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.springframework.web.bind.annotation.PathVariable;
4848
import org.springframework.web.bind.annotation.PostMapping;
4949
import org.springframework.web.bind.annotation.RequestParam;
50+
import org.springframework.web.bind.annotation.ResponseBody;
5051
import org.springframework.web.servlet.View;
5152
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
5253
import org.springframework.web.servlet.view.RedirectView;
@@ -421,6 +422,13 @@ public String searchSeriesByCatalog(
421422
return "series/search_result";
422423
}
423424

425+
@ResponseBody
426+
@GetMapping(Url.INFO_COUNTRY_SERIES_PAGE)
427+
public String guessCountryBy(@CurrentUser Integer currentUserId) {
428+
String result = seriesService.guessCountryBy(currentUserId);
429+
return "{\"country\":\"" + result + "\"}";
430+
}
431+
424432
// CheckStyle: ignore LineLength for next 1 line
425433
private Map<String, ?> prepareCommonAttrsForSeriesInfo(SeriesDto series, Integer currentUserId) {
426434
Map<String, Object> model = new HashMap<>();

src/main/java/ru/mystamps/web/dao/SeriesDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
@SuppressWarnings("PMD.TooManyMethods")
3131
public interface SeriesDao {
3232
Integer add(AddSeriesDbDto series);
33+
String guessCountryBy(Integer createdBy);
34+
3335
void markAsModified(Integer seriesId, Date updateAt, Integer updatedBy);
3436
List<SitemapInfoDto> findAllForSitemap();
3537
List<SeriesInfoDto> findLastAdded(int quantity, String lang);

src/main/java/ru/mystamps/web/dao/impl/JdbcSeriesDao.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ public class JdbcSeriesDao implements SeriesDao {
101101
@Value("${series.count_series_added_since}")
102102
private String countSeriesAddedSinceSql;
103103

104+
@Value("${series.find_last_country_by_id}")
105+
private String findLastCountryByIdSql;
106+
107+
@Value("${series.find_popular_country}")
108+
private String findPopularCountrySql;
109+
104110
@Value("${series.count_series_updated_since}")
105111
private String countSeriesUpdatedSinceSql;
106112

@@ -318,6 +324,19 @@ public long countAddedSince(Date date) {
318324
Long.class
319325
);
320326
}
327+
328+
@Override
329+
public String guessCountryBy(Integer createdBy)
330+
{
331+
Map<String, Object> params = new HashMap<>();
332+
params.put("created_by", createdBy);
333+
334+
String result = jdbcTemplate.queryForObject(findLastCountryByIdSql, params, String.class);
335+
if (result != null)
336+
return result;
337+
338+
return jdbcTemplate.queryForObject(findPopularCountrySql, Collections.<String, Object>emptyMap(), String.class);
339+
}
321340

322341
@Override
323342
public long countUpdatedSince(Date date) {

src/main/java/ru/mystamps/web/service/CountryService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
public interface CountryService {
2727
String add(AddCountryDto dto, Integer userId);
28+
2829
List<LinkEntityDto> findAllAsLinkEntities(String lang);
2930
LinkEntityDto findOneAsLinkEntity(String slug, String lang);
3031
long countAll();

src/main/java/ru/mystamps/web/service/SeriesService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
@SuppressWarnings("PMD.TooManyMethods")
3232
public interface SeriesService {
3333
Integer add(AddSeriesDto dto, Integer userId, boolean userCanAddComments);
34+
String guessCountryBy(Integer createdBy);
35+
3436
void addImageToSeries(AddImageDto dto, Integer seriesId, Integer userId);
3537
long countAll();
3638
long countAllStamps();

src/main/java/ru/mystamps/web/service/SeriesServiceImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,14 @@ public long countUpdatedSince(Date date) {
221221
return seriesDao.countUpdatedSince(date);
222222
}
223223

224+
@Override
225+
@Transactional(readOnly = true)
226+
public String guessCountryBy(Integer createdBy){
227+
Validate.isTrue(createdBy != null, "CreatedBy must be non null");
228+
229+
return seriesDao.guessCountryBy(createdBy);
230+
}
231+
224232
@Override
225233
@Transactional(readOnly = true)
226234
public boolean isSeriesExist(Integer seriesId) {

src/main/javascript/series/add.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,23 @@ function initPage() {
2121
$('.js-with-tooltip').tooltip({
2222
'placement': 'right'
2323
});
24+
25+
$.ajax({
26+
url : "/series/country_slug",
27+
success : function(data) {
28+
var slug = jQuery.parseJSON(data).country;
29+
if (slug == null)
30+
return;
31+
32+
var country = $("#guess_country");
33+
country[0].style.display = "block";
34+
country.click(function() {
35+
$(this)[0].style.display = "none";
36+
37+
var select_country = $("#country").selectize();
38+
var selectize = select_country[0].selectize;
39+
selectize.setValue(slug);
40+
});
41+
}
42+
});
2443
}

src/main/resources/liquibase/changelog.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
<include file="initial-state.xml" relativeToChangelogFile="true" />
1212
<include file="version/0.3.xml" relativeToChangelogFile="true" />
1313
<include file="version/0.4.xml" relativeToChangelogFile="true" />
14+
<include file="initial-test.xml" relativeToChangelogFile="true" />
1415

1516
</databaseChangeLog>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<databaseChangeLog
3+
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
6+
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd">
7+
8+
<changeSet id="add-countries" author="php-coder" context="test-data">
9+
<sqlFile path="sql/test-add-countries.sql" relativeToChangelogFile="true" />
10+
</changeSet>
11+
12+
<changeSet id="add-series" author="php-coder" context="test-data">
13+
<sqlFile path="sql/test-add-series.sql" relativeToChangelogFile="true" />
14+
</changeSet>
15+
16+
</databaseChangeLog>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--
2+
-- Auto-generated by Maven, based on values from src/main/resources/test/spring/test-data.properties
3+
--
4+
5+
INSERT INTO countries(name, created_at, created_by, updated_at, updated_by, name_ru, slug) VALUES
6+
('Russia', NOW(), 3, NOW(), 3, 'Россия', 'russia');
7+
8+
INSERT INTO countries(name, created_at, created_by, updated_at, updated_by, name_ru, slug) VALUES
9+
('Germany', NOW(), 3, NOW(), 3, 'Германия', 'germany');
10+
11+
INSERT INTO countries(name, created_at, created_by, updated_at, updated_by, name_ru, slug) VALUES
12+
('France', NOW(), 3, NOW(), 3, 'Франция', 'france');
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--
2+
-- Auto-generated by Maven, based on values from src/main/resources/test/spring/test-data.properties
3+
--
4+
5+
INSERT INTO series(id, quantity, perforated, country_id, created_at, created_by, updated_at, updated_by, category_id) VALUES
6+
(2, 1, 1, 2, NOW() + 1, 1, NOW(), 4, 1);
7+
8+
INSERT INTO series(id, quantity, perforated, country_id, created_at, created_by, updated_at, updated_by, category_id) VALUES
9+
(3, 1, 1, 2, NOW() + 2, 1, NOW(), 4, 1);
10+
11+
INSERT INTO series(id, quantity, perforated, country_id, created_at, created_by, updated_at, updated_by, category_id) VALUES
12+
(4, 1, 1, 2, NOW() + 3, 1, NOW(), 4, 1);
13+
14+
INSERT INTO series(id, quantity, perforated, country_id, created_at, created_by, updated_at, updated_by, category_id) VALUES
15+
(5, 1, 1, 2, NOW() + 4, 1, NOW(), 4, 1);
16+
17+
INSERT INTO series(id, quantity, perforated, country_id, created_at, created_by, updated_at, updated_by, category_id) VALUES
18+
(6, 1, 1, 4, NOW() + 5, 1, NOW(), 4, 1);

src/main/resources/ru/mystamps/i18n/Messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ t_add_more_images_hint = Later you will be able to add additional images
121121
t_not_chosen = Not chosen
122122
t_create_category_hint = You can also <a tabindex="-1" href="{0}">add a new category</a>
123123
t_create_country_hint = You can also <a tabindex="-1" href="{0}">add a new country</a>
124+
t_guess_country = Guess the country
124125

125126
# series/info.html
126127
t_series_info = Info about series

src/main/resources/ru/mystamps/i18n/Messages_ru.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ t_add_more_images_hint = Вы сможете добавить дополните
121121
t_not_chosen = Не выбрана
122122
t_create_category_hint = Вы также можете <a tabindex="-1" href="{0}">добавить новую категорию</a>
123123
t_create_country_hint = Вы также можете <a tabindex="-1" href="{0}">добавить новую страну</a>
124+
t_guess_country = Угадать страну
124125

125126
# series/info.html
126127
t_series_info = Информация о серии

src/main/resources/sql/series_dao_queries.properties

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,19 @@ SELECT COUNT(*) \
242242
FROM series \
243243
WHERE updated_at >= :date
244244

245+
series.find_last_country_by_id = \
246+
SELECT slug \
247+
FROM series LEFT JOIN countries ON countries.id = series.country_id \
248+
WHERE series.created_at IN (SELECT MAX(series.created_at) \
249+
FROM series \
250+
WHERE series.created_by = :created_by)
251+
252+
series.find_popular_country = \
253+
SELECT slug \
254+
FROM series LEFT JOIN countries ON countries.id = series.country_id \
255+
GROUP BY country_id \
256+
ORDER BY COUNT(*) DESC LIMIT 1
257+
245258
series.find_series_ids_by_michel_number = \
246259
SELECT smc.series_id AS series_id \
247260
FROM series_michel_catalog smc \

src/main/webapp/WEB-INF/static/styles/main.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,7 @@ label {
107107
.goog-tooltip {
108108
color: white;
109109
}
110+
111+
#guess_country {
112+
display: none;
113+
}

src/main/webapp/WEB-INF/views/series/add.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ <h3 th:text="${#strings.capitalize(add_series)}">
202202
<span id="country.errors" class="help-block" th:if="${#fields.hasErrors('country')}" th:each="error : ${#fields.errors('country')}" th:text="${error}"></span>
203203
/*/-->
204204
</div>
205+
206+
<span id="guess_country">
207+
<a tabindex="-1" th:text="#{t_guess_country}" href="javascript:void(0)">Guess the country</a>
208+
</span>
205209
</div>
206210

207211
<div class="form-group form-group-sm" th:classappend="${#fields.hasErrors('quantity') ? 'has-error' : ''}">

0 commit comments

Comments
 (0)