Skip to content

Commit ab95ef6

Browse files
committed
Port country creation from JPA to JDBC.
Addressed to #120 No functional changes.
1 parent 59ba2ee commit ab95ef6

File tree

7 files changed

+136
-40
lines changed

7 files changed

+136
-40
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@
3535
@EnableAsync
3636
public class ServicesConfig {
3737

38-
@Inject
39-
private CountryDao countryDao;
40-
4138
@Inject
4239
private CategoryDao categoryDao;
4340

@@ -76,7 +73,7 @@ public class ServicesConfig {
7673

7774
@Bean
7875
public CountryService getCountryService() {
79-
return new CountryServiceImpl(countryDao, daoConfig.getJdbcCountryDao());
76+
return new CountryServiceImpl(daoConfig.getJdbcCountryDao());
8077
}
8178

8279
@Bean

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919

2020
import java.util.Map;
2121

22+
import ru.mystamps.web.dao.dto.AddCountryDbDto;
2223
import ru.mystamps.web.service.dto.LinkEntityDto;
2324
import ru.mystamps.web.service.dto.SelectEntityDto;
2425

2526
public interface JdbcCountryDao {
27+
Integer add(AddCountryDbDto country);
2628
long countAll();
2729
long countByName(String name);
2830
long countByNameRu(String name);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (C) 2009-2015 Slava Semushin <[email protected]>
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.dao.dto;
19+
20+
import lombok.Getter;
21+
import lombok.Setter;
22+
import lombok.ToString;
23+
24+
import java.util.Date;
25+
26+
@Getter
27+
@Setter
28+
@ToString(of = { "name", "nameRu"})
29+
public class AddCountryDbDto {
30+
private String name;
31+
private String nameRu;
32+
private String slug;
33+
private Date createdAt;
34+
private Integer createdBy;
35+
private Date updatedAt;
36+
private Integer updatedBy;
37+
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,18 @@
2222
import java.util.List;
2323
import java.util.Map;
2424

25+
import org.apache.commons.lang3.Validate;
26+
2527
import org.springframework.beans.factory.annotation.Value;
28+
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
2629
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
30+
import org.springframework.jdbc.support.GeneratedKeyHolder;
31+
import org.springframework.jdbc.support.KeyHolder;
2732

2833
import lombok.RequiredArgsConstructor;
2934

3035
import ru.mystamps.web.dao.JdbcCountryDao;
36+
import ru.mystamps.web.dao.dto.AddCountryDbDto;
3137
import ru.mystamps.web.service.dto.LinkEntityDto;
3238
import ru.mystamps.web.service.dto.SelectEntityDto;
3339

@@ -36,6 +42,9 @@ public class JdbcCountryDaoImpl implements JdbcCountryDao {
3642

3743
private final NamedParameterJdbcTemplate jdbcTemplate;
3844

45+
@Value("${country.create}")
46+
private String addCountrySql;
47+
3948
@Value("${country.count_all_countries}")
4049
private String countAllSql;
4150

@@ -57,6 +66,34 @@ public class JdbcCountryDaoImpl implements JdbcCountryDao {
5766
@Value("${country.find_all_countries_names_with_slug}")
5867
private String findCountriesNamesWithSlugSql;
5968

69+
@Override
70+
public Integer add(AddCountryDbDto country) {
71+
Map<String, Object> params = new HashMap<>();
72+
params.put("name", country.getName());
73+
params.put("name_ru", country.getNameRu());
74+
params.put("slug", country.getSlug());
75+
params.put("created_at", country.getCreatedAt());
76+
params.put("created_by", country.getCreatedBy());
77+
params.put("updated_at", country.getUpdatedAt());
78+
params.put("updated_by", country.getUpdatedBy());
79+
80+
KeyHolder holder = new GeneratedKeyHolder();
81+
82+
int affected = jdbcTemplate.update(
83+
addCountrySql,
84+
new MapSqlParameterSource(params),
85+
holder
86+
);
87+
88+
Validate.validState(
89+
affected == 1,
90+
"Unexpected number of affected rows after creation of country: %d",
91+
affected
92+
);
93+
94+
return Integer.valueOf(holder.getKey().intValue());
95+
}
96+
6097
@Override
6198
public long countAll() {
6299
return jdbcTemplate.queryForObject(

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@
3232
import lombok.RequiredArgsConstructor;
3333

3434
import ru.mystamps.web.dao.JdbcCountryDao;
35+
import ru.mystamps.web.dao.dto.AddCountryDbDto;
3536
import ru.mystamps.web.entity.Collection;
36-
import ru.mystamps.web.entity.Country;
3737
import ru.mystamps.web.entity.User;
38-
import ru.mystamps.web.dao.CountryDao;
3938
import ru.mystamps.web.service.dto.AddCountryDto;
4039
import ru.mystamps.web.service.dto.LinkEntityDto;
4140
import ru.mystamps.web.service.dto.SelectEntityDto;
@@ -46,7 +45,6 @@
4645
public class CountryServiceImpl implements CountryService {
4746
private static final Logger LOG = LoggerFactory.getLogger(CountryServiceImpl.class);
4847

49-
private final CountryDao countryDao;
5048
private final JdbcCountryDao jdbcCountryDao;
5149

5250
@Override
@@ -58,7 +56,7 @@ public UrlEntityDto add(AddCountryDto dto, User user) {
5856
Validate.isTrue(dto.getNameRu() != null, "Country name on Russian should be non null");
5957
Validate.isTrue(user != null, "Current user must be non null");
6058

61-
Country country = new Country();
59+
AddCountryDbDto country = new AddCountryDbDto();
6260
country.setName(dto.getName());
6361
country.setNameRu(dto.getNameRu());
6462

@@ -70,16 +68,17 @@ public UrlEntityDto add(AddCountryDto dto, User user) {
7068
country.setSlug(slug);
7169

7270
Date now = new Date();
73-
country.getMetaInfo().setCreatedAt(now);
74-
country.getMetaInfo().setUpdatedAt(now);
7571

76-
country.getMetaInfo().setCreatedBy(user);
77-
country.getMetaInfo().setUpdatedBy(user);
78-
79-
Country entity = countryDao.save(country);
80-
LOG.info("Country has been created ({})", entity.toLongString());
72+
country.setCreatedAt(now);
73+
country.setCreatedBy(user.getId());
74+
country.setUpdatedAt(now);
75+
country.setUpdatedBy(user.getId());
76+
77+
Integer id = jdbcCountryDao.add(country);
78+
79+
LOG.info("Country #{} has been created ({})", id, country);
8180

82-
return new UrlEntityDto(entity.getId(), entity.getSlug());
81+
return new UrlEntityDto(id, slug);
8382
}
8483

8584
@Override

src/main/resources/sql/country_dao_queries.properties

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
country.create = \
2+
INSERT \
3+
INTO countries \
4+
( name \
5+
, name_ru \
6+
, slug \
7+
, created_at \
8+
, created_by \
9+
, updated_at \
10+
, updated_by \
11+
) \
12+
VALUES \
13+
( :name \
14+
, :name_ru \
15+
, :slug \
16+
, :created_at \
17+
, :created_by \
18+
, :updated_at \
19+
, :updated_by \
20+
)
21+
122
country.count_all_countries = \
223
SELECT COUNT(*) \
324
FROM countries

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

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ package ru.mystamps.web.service
2020
import spock.lang.Specification
2121
import spock.lang.Unroll
2222

23-
import ru.mystamps.web.dao.CountryDao
2423
import ru.mystamps.web.dao.JdbcCountryDao
24+
import ru.mystamps.web.dao.dto.AddCountryDbDto
2525
import ru.mystamps.web.entity.Country
2626
import ru.mystamps.web.entity.Collection
2727
import ru.mystamps.web.entity.User
@@ -37,9 +37,8 @@ class CountryServiceImplTest extends Specification {
3737
private AddCountryForm form
3838
private User user
3939

40-
private CountryDao countryDao = Mock()
4140
private JdbcCountryDao jdbcCountryDao = Mock()
42-
private CountryService service = new CountryServiceImpl(countryDao, jdbcCountryDao)
41+
private CountryService service = new CountryServiceImpl(jdbcCountryDao)
4342

4443
def setup() {
4544
form = new AddCountryForm()
@@ -87,11 +86,15 @@ class CountryServiceImplTest extends Specification {
8786

8887
def "add() should call dao"() {
8988
given:
90-
Country country = TestObjects.createCountry()
89+
Integer expectedId = 7
9190
and:
92-
countryDao.save(_ as Country) >> country
91+
form.setName("Example Country")
9392
and:
94-
UrlEntityDto expected = new UrlEntityDto(country.getId(), country.getSlug())
93+
String expectedSlug = "example-country"
94+
and:
95+
jdbcCountryDao.add(_ as AddCountryDbDto) >> expectedId
96+
and:
97+
UrlEntityDto expected = new UrlEntityDto(expectedId, expectedSlug)
9598
when:
9699
UrlEntityDto actual = service.add(form, user)
97100
then:
@@ -105,10 +108,10 @@ class CountryServiceImplTest extends Specification {
105108
when:
106109
service.add(form, user)
107110
then:
108-
1 * countryDao.save({ Country country ->
111+
1 * jdbcCountryDao.add({ AddCountryDbDto country ->
109112
assert country?.name == expectedCountryName
110113
return true
111-
}) >> TestObjects.createCountry()
114+
}) >> 10
112115
}
113116

114117
def "add() should pass country name on Russian to dao"() {
@@ -118,10 +121,10 @@ class CountryServiceImplTest extends Specification {
118121
when:
119122
service.add(form, user)
120123
then:
121-
1 * countryDao.save({ Country country ->
124+
1 * jdbcCountryDao.add({ AddCountryDbDto country ->
122125
assert country?.nameRu == expectedCountryName
123126
return true
124-
}) >> TestObjects.createCountry()
127+
}) >> 20
125128
}
126129

127130
def "add() should throw exception when name can't be converted to slug"() {
@@ -143,50 +146,50 @@ class CountryServiceImplTest extends Specification {
143146
when:
144147
service.add(form, user)
145148
then:
146-
1 * countryDao.save({ Country country ->
149+
1 * jdbcCountryDao.add({ AddCountryDbDto country ->
147150
assert country?.slug == slug
148151
return true
149-
}) >> TestObjects.createCountry()
152+
}) >> 30
150153
}
151154

152155
def "add() should assign created at to current date"() {
153156
when:
154157
service.add(form, user)
155158
then:
156-
1 * countryDao.save({ Country country ->
157-
assert DateUtils.roughlyEqual(country?.metaInfo?.createdAt, new Date())
159+
1 * jdbcCountryDao.add({ AddCountryDbDto country ->
160+
assert DateUtils.roughlyEqual(country?.createdAt, new Date())
158161
return true
159-
}) >> TestObjects.createCountry()
162+
}) >> 40
160163
}
161164

162165
def "add() should assign updated at to current date"() {
163166
when:
164167
service.add(form, user)
165168
then:
166-
1 * countryDao.save({ Country country ->
167-
assert DateUtils.roughlyEqual(country?.metaInfo?.updatedAt, new Date())
169+
1 * jdbcCountryDao.add({ AddCountryDbDto country ->
170+
assert DateUtils.roughlyEqual(country?.updatedAt, new Date())
168171
return true
169-
}) >> TestObjects.createCountry()
172+
}) >> 50
170173
}
171174

172175
def "add() should assign created by to user"() {
173176
when:
174177
service.add(form, user)
175178
then:
176-
1 * countryDao.save({ Country country ->
177-
assert country?.metaInfo?.createdBy == user
179+
1 * jdbcCountryDao.add({ AddCountryDbDto country ->
180+
assert country?.createdBy == user.id
178181
return true
179-
}) >> TestObjects.createCountry()
182+
}) >> 60
180183
}
181184

182185
def "add() should assign updated by to user"() {
183186
when:
184187
service.add(form, user)
185188
then:
186-
1 * countryDao.save({ Country country ->
187-
assert country?.metaInfo?.updatedBy == user
189+
1 * jdbcCountryDao.add({ AddCountryDbDto country ->
190+
assert country?.updatedBy == user.id
188191
return true
189-
}) >> TestObjects.createCountry()
192+
}) >> 70
190193
}
191194

192195
//

0 commit comments

Comments
 (0)