Skip to content

Commit 7a1e0b4

Browse files
committed
Show statistics about amount of countries/series in database.
1 parent b9aa23b commit 7a1e0b4

File tree

12 files changed

+132
-14
lines changed

12 files changed

+132
-14
lines changed

src/main/java/ru/mystamps/web/config/ControllersConfig.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,12 @@ public SeriesController getSeriesController() {
5858
);
5959
}
6060

61+
@Bean
62+
public SiteController getSiteController() {
63+
return new SiteController(
64+
servicesConfig.getCountryService(),
65+
servicesConfig.getSeriesService()
66+
);
67+
}
68+
6169
}

src/main/java/ru/mystamps/web/config/MvcConfig.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public void configureDefaultServletHandling(DefaultServletHandlerConfigurer conf
6262
@Override
6363
public void addViewControllers(ViewControllerRegistry registry) {
6464
registry.addViewController(Url.AUTHENTICATION_PAGE);
65-
registry.addViewController(Url.INDEX_PAGE).setViewName("site/index");
6665
registry.addViewController(Url.UNAUTHORIZED_PAGE);
6766
}
6867

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (C) 2009-2014 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.controller;
19+
20+
import org.springframework.stereotype.Controller;
21+
import org.springframework.ui.Model;
22+
import org.springframework.web.bind.annotation.RequestMapping;
23+
import org.springframework.web.bind.annotation.RequestMethod;
24+
25+
import ru.mystamps.web.Url;
26+
import ru.mystamps.web.service.CountryService;
27+
import ru.mystamps.web.service.SeriesService;
28+
29+
@Controller
30+
public class SiteController {
31+
32+
private final CountryService countryService;
33+
private final SeriesService seriesService;
34+
35+
public SiteController(CountryService countryService, SeriesService seriesService) {
36+
this.countryService = countryService;
37+
this.seriesService = seriesService;
38+
}
39+
40+
@RequestMapping(value = Url.INDEX_PAGE, method = RequestMethod.GET)
41+
public String showIndexPage(Model model) {
42+
model.addAttribute("countryCounter", countryService.countAll());
43+
model.addAttribute("seriesCounter", seriesService.countAll());
44+
return "site/index";
45+
}
46+
47+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
public interface CountryService {
2525
Country add(AddCountryDto dto, User user);
2626
Iterable<Country> findAll();
27+
long countAll();
2728
int countByName(String name);
2829
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ public Iterable<Country> findAll() {
7474
return countryDao.findAll();
7575
}
7676

77+
@Override
78+
@Transactional(readOnly = true)
79+
public long countAll() {
80+
return countryDao.count();
81+
}
82+
7783
@Override
7884
@Transactional(readOnly = true)
7985
public int countByName(String name) {

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

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

2424
public interface SeriesService {
2525
Series add(AddSeriesDto dto, User user, boolean userCanAddComments);
26+
long countAll();
2627
int countByMichelNumber(String michelNumberCode);
2728
int countByScottNumber(String scottNumberCode);
2829
int countByYvertNumber(String yvertNumberCode);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ public Series add(AddSeriesDto dto, User user, boolean userCanAddComments) {
126126
return entity;
127127
}
128128

129+
@Override
130+
@Transactional(readOnly = true)
131+
public long countAll() {
132+
return seriesDao.count();
133+
}
134+
129135
@Override
130136
@Transactional(readOnly = true)
131137
public int countByMichelNumber(String michelNumberCode) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ t_add_country = add country
2222
# site/index.jsp
2323
t_index_title = create your own virtual collection!
2424
t_you_may = You may
25+
t_in_db = In our database
26+
t_countries_amount = Amount of countries
27+
t_series_amount = Amount of series
2528

2629
# account/register.jsp
2730
t_registration_on_site = Register on site

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ t_add_country = добавить страну
2222
# site/index.jsp
2323
t_index_title = создай свою виртуальную коллекцию!
2424
t_you_may = Вы можете
25+
t_in_db = В нашей базе
26+
t_countries_amount = Стран
27+
t_series_amount = Серий
2528

2629
# account/register.jsp
2730
t_registration_on_site = Регистрация на сайте

src/main/webapp/WEB-INF/tiles/body/site/index.jsp

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,30 @@
44
<spring:url var="addSeriesUrl" value="<%= Url.ADD_SERIES_PAGE %>" />
55
<spring:url var="addCountryUrl" value="<%= Url.ADD_COUNTRY_PAGE %>" />
66

7-
<sec:authorize access="hasAnyAuthority('CREATE_COUNTRY', 'CREATE_SERIES')">
8-
<spring:message code="t_you_may" />:
9-
<nav>
10-
<ul>
11-
<sec:authorize access="hasAuthority('CREATE_SERIES')">
12-
<li><a href="${addSeriesUrl}"><spring:message code="t_add_series" /></a></li>
13-
</sec:authorize>
14-
<sec:authorize access="hasAuthority('CREATE_COUNTRY')">
15-
<li><a href="${addCountryUrl}"><spring:message code="t_add_country" /></a></li>
16-
</sec:authorize>
17-
</ul>
18-
</nav>
19-
</sec:authorize>
7+
<div class="span10">
8+
<sec:authorize access="hasAnyAuthority('CREATE_COUNTRY', 'CREATE_SERIES')">
9+
<spring:message code="t_you_may" />:
10+
<nav>
11+
<ul>
12+
<sec:authorize access="hasAuthority('CREATE_SERIES')">
13+
<li><a href="${addSeriesUrl}"><spring:message code="t_add_series" /></a></li>
14+
</sec:authorize>
15+
<sec:authorize access="hasAuthority('CREATE_COUNTRY')">
16+
<li><a href="${addCountryUrl}"><spring:message code="t_add_country" /></a></li>
17+
</sec:authorize>
18+
</ul>
19+
</nav>
20+
</sec:authorize>
21+
</div>
22+
23+
<div class="span2">
24+
<p>
25+
<strong><spring:message code="t_in_db" />:</strong>
26+
</p>
27+
<p>
28+
<spring:message code="t_countries_amount" />: ${countryCounter}
29+
</p>
30+
<p>
31+
<spring:message code="t_series_amount" />: ${seriesCounter}
32+
</p>
33+
</div>

src/test/groovy/ru/mystamps/web/service/CountryServiceTest.groovy

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,21 @@ class CountryServiceTest extends Specification {
151151
resultCountries == expectedCountries
152152
}
153153

154+
//
155+
// Tests for countAll()
156+
//
157+
158+
def "countAll() should call dao and returns result"() {
159+
given:
160+
long expectedResult = 20
161+
when:
162+
long result = service.countAll()
163+
then:
164+
1 * countryDao.count() >> expectedResult
165+
and:
166+
result == expectedResult
167+
}
168+
154169
//
155170
// Tests for countByName()
156171
//

src/test/groovy/ru/mystamps/web/service/SeriesServiceTest.groovy

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,21 @@ class SeriesServiceTest extends Specification {
553553
})
554554
}
555555

556+
//
557+
// Tests for countAll()
558+
//
559+
560+
def "countAll() should call dao and returns result"() {
561+
given:
562+
long expectedResult = 20
563+
when:
564+
long result = service.countAll()
565+
then:
566+
1 * seriesDao.count() >> expectedResult
567+
and:
568+
result == expectedResult
569+
}
570+
556571
//
557572
// Tests for countByMichelNumber()
558573
//

0 commit comments

Comments
 (0)