Skip to content

Commit 9360f6f

Browse files
committed
TransactionParticipantService.add(): return id after creation.
Prerequisite for #695 No functional changes.
1 parent e07b1c9 commit 9360f6f

File tree

5 files changed

+25
-9
lines changed

5 files changed

+25
-9
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import ru.mystamps.web.dao.dto.EntityWithParentDto;
2525

2626
public interface TransactionParticipantDao {
27-
void add(AddParticipantDbDto participant);
27+
Integer add(AddParticipantDbDto participant);
2828
List<EntityWithParentDto> findBuyersWithParents();
2929
List<EntityWithParentDto> findSellersWithParents();
3030
Integer findSellerId(String name, String url);

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525

2626
import org.springframework.beans.factory.annotation.Value;
2727
import org.springframework.dao.EmptyResultDataAccessException;
28+
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
2829
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
30+
import org.springframework.jdbc.support.GeneratedKeyHolder;
31+
import org.springframework.jdbc.support.KeyHolder;
2932

3033
import lombok.RequiredArgsConstructor;
3134

@@ -55,21 +58,29 @@ public class JdbcTransactionParticipantDao implements TransactionParticipantDao
5558
private String findAllGroupsSql;
5659

5760
@Override
58-
public void add(AddParticipantDbDto participant) {
61+
public Integer add(AddParticipantDbDto participant) {
5962
Map<String, Object> params = new HashMap<>();
6063
params.put("name", participant.getName());
6164
params.put("url", participant.getUrl());
6265
params.put("group_id", participant.getGroupId());
6366
params.put("buyer", participant.getBuyer());
6467
params.put("seller", participant.getSeller());
6568

66-
int affected = jdbcTemplate.update(addParticipantSql, params);
69+
KeyHolder holder = new GeneratedKeyHolder();
70+
71+
int affected = jdbcTemplate.update(
72+
addParticipantSql,
73+
new MapSqlParameterSource(params),
74+
holder
75+
);
6776

6877
Validate.validState(
6978
affected == 1,
7079
"Unexpected number of affected rows after creation of participant: %d",
7180
affected
7281
);
82+
83+
return Integer.valueOf(holder.getKey().intValue());
7384
}
7485

7586
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import ru.mystamps.web.service.dto.AddParticipantDto;
2525

2626
public interface TransactionParticipantService {
27-
void add(AddParticipantDto dto);
27+
Integer add(AddParticipantDto dto);
2828
List<EntityWithParentDto> findBuyersWithParents();
2929
List<EntityWithParentDto> findSellersWithParents();
3030
Integer findSellerId(String name, String url);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class TransactionParticipantServiceImpl implements TransactionParticipant
4646
@Override
4747
@Transactional
4848
@PreAuthorize(HasAuthority.ADD_PARTICIPANT)
49-
public void add(AddParticipantDto dto) {
49+
public Integer add(AddParticipantDto dto) {
5050
Validate.isTrue(dto != null, "DTO must be non null");
5151
Validate.isTrue(dto.getName() != null, "Name must be non null");
5252
Validate.isTrue(dto.getBuyer() != null, "Buyer flag must be non null");
@@ -59,9 +59,11 @@ public void add(AddParticipantDto dto) {
5959
participant.setBuyer(dto.getBuyer());
6060
participant.setSeller(dto.getSeller());
6161

62-
transactionParticipantDao.add(participant);
62+
Integer id = transactionParticipantDao.add(participant);
6363

64-
log.info("Participant has been created ({})", participant);
64+
log.info("Participant #{} has been created ({})", id, participant);
65+
66+
return id;
6567
}
6668

6769
@Override

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class TransactionParticipantServiceImplTest extends Specification {
9696
Integer expectedGroupId = nullOr(Random.id())
9797
Boolean expectedBuyer = bool()
9898
Boolean expectedSeller = bool()
99+
Integer expectedResult = Random.id()
99100
and:
100101
AddParticipantForm form = new AddParticipantForm()
101102
form.setName(expectedName)
@@ -104,7 +105,7 @@ class TransactionParticipantServiceImplTest extends Specification {
104105
form.setBuyer(expectedBuyer)
105106
form.setSeller(expectedSeller)
106107
when:
107-
service.add(form)
108+
Integer result = service.add(form)
108109
then:
109110
1 * transactionParticipantDao.add({ AddParticipantDbDto participant ->
110111
assert participant?.name == expectedName
@@ -113,7 +114,9 @@ class TransactionParticipantServiceImplTest extends Specification {
113114
assert participant?.buyer == expectedBuyer
114115
assert participant?.seller == expectedSeller
115116
return true
116-
})
117+
}) >> expectedResult
118+
and:
119+
result == expectedResult
117120
}
118121

119122
//

0 commit comments

Comments
 (0)