Skip to content

Commit 0065390

Browse files
committed
/participant/add: allow to specify group.
Fix #704
1 parent a0fea08 commit 0065390

File tree

16 files changed

+120
-7
lines changed

16 files changed

+120
-7
lines changed

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
*/
1818
package ru.mystamps.web.controller;
1919

20+
import java.util.List;
21+
2022
import javax.validation.Valid;
2123

2224
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
2325
import org.springframework.stereotype.Controller;
26+
import org.springframework.ui.Model;
2427
import org.springframework.validation.BindingResult;
2528
import org.springframework.web.bind.WebDataBinder;
2629
import org.springframework.web.bind.annotation.GetMapping;
@@ -32,6 +35,7 @@
3235

3336
import ru.mystamps.web.Url;
3437
import ru.mystamps.web.controller.dto.AddParticipantForm;
38+
import ru.mystamps.web.dao.dto.EntityWithIdDto;
3539
import ru.mystamps.web.service.TransactionParticipantService;
3640

3741
import static ru.mystamps.web.controller.ControllerUtils.redirectTo;
@@ -51,19 +55,25 @@ protected void initBinder(WebDataBinder binder) {
5155

5256
@GetMapping(Url.ADD_PARTICIPANT_PAGE)
5357
public AddParticipantForm showForm(
58+
Model model,
5459
@RequestParam(name = "seller", required = false) Boolean seller,
5560
@RequestParam(name = "buyer", required = false) Boolean buyer) {
5661

5762
AddParticipantForm form = new AddParticipantForm();
5863
form.setSeller(seller);
5964
form.setBuyer(buyer);
6065

66+
List<EntityWithIdDto> groups = participantService.findAllGroups();
67+
model.addAttribute("groups", groups);
68+
6169
return form;
6270
}
6371

6472
@PostMapping(Url.ADD_PARTICIPANT_PAGE)
65-
public String processInput(@Valid AddParticipantForm form, BindingResult result) {
73+
public String processInput(Model model, @Valid AddParticipantForm form, BindingResult result) {
6674
if (result.hasErrors()) {
75+
List<EntityWithIdDto> groups = participantService.findAllGroups();
76+
model.addAttribute("groups", groups);
6777
return null;
6878
}
6979

src/main/java/ru/mystamps/web/controller/dto/AddParticipantForm.java

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public class AddParticipantForm implements AddParticipantDto {
4747
@Size(max = PARTICIPANT_URL_MAX_LENGTH, message = "{value.too-long}")
4848
private String url;
4949

50+
// TODO: must be positive
51+
// TODO: must be existing group
52+
private Integer groupId;
53+
5054
@NotNull
5155
private Boolean buyer;
5256

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

+2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
import java.util.List;
2121

2222
import ru.mystamps.web.dao.dto.AddParticipantDbDto;
23+
import ru.mystamps.web.dao.dto.EntityWithIdDto;
2324
import ru.mystamps.web.dao.dto.TransactionParticipantDto;
2425

2526
public interface TransactionParticipantDao {
2627
void add(AddParticipantDbDto participant);
2728
List<TransactionParticipantDto> findBuyersWithParents();
2829
List<TransactionParticipantDto> findSellersWithParents();
30+
List<EntityWithIdDto> findAllGroups();
2931
}

src/main/java/ru/mystamps/web/dao/dto/AddParticipantDbDto.java

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
public class AddParticipantDbDto {
2828
private String name;
2929
private String url;
30+
private Integer groupId;
3031
private Boolean buyer;
3132
private Boolean seller;
3233
}

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

+10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import ru.mystamps.web.dao.TransactionParticipantDao;
3232
import ru.mystamps.web.dao.dto.AddParticipantDbDto;
33+
import ru.mystamps.web.dao.dto.EntityWithIdDto;
3334
import ru.mystamps.web.dao.dto.TransactionParticipantDto;
3435

3536
@RequiredArgsConstructor
@@ -46,11 +47,15 @@ public class JdbcTransactionParticipantDao implements TransactionParticipantDao
4647
@Value("${transaction_participant.find_sellers_with_parent_names}")
4748
private String findSellersWithParentNamesSql;
4849

50+
@Value("${transaction_participant_group.find_all}")
51+
private String findAllGroupsSql;
52+
4953
@Override
5054
public void add(AddParticipantDbDto participant) {
5155
Map<String, Object> params = new HashMap<>();
5256
params.put("name", participant.getName());
5357
params.put("url", participant.getUrl());
58+
params.put("group_id", participant.getGroupId());
5459
params.put("buyer", participant.getBuyer());
5560
params.put("seller", participant.getSeller());
5661

@@ -79,4 +84,9 @@ public List<TransactionParticipantDto> findSellersWithParents() {
7984
);
8085
}
8186

87+
@Override
88+
public List<EntityWithIdDto> findAllGroups() {
89+
return jdbcTemplate.query(findAllGroupsSql, RowMappers::forEntityWithIdDto);
90+
}
91+
8292
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919

2020
import java.util.List;
2121

22+
import ru.mystamps.web.dao.dto.EntityWithIdDto;
2223
import ru.mystamps.web.service.dto.AddParticipantDto;
2324
import ru.mystamps.web.service.dto.GroupedTransactionParticipantDto;
2425

2526
public interface TransactionParticipantService {
2627
void add(AddParticipantDto dto);
2728
List<GroupedTransactionParticipantDto> findAllBuyers();
2829
List<GroupedTransactionParticipantDto> findAllSellers();
30+
List<EntityWithIdDto> findAllGroups();
2931
}

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

+9
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import ru.mystamps.web.dao.TransactionParticipantDao;
3535
import ru.mystamps.web.dao.dto.AddParticipantDbDto;
36+
import ru.mystamps.web.dao.dto.EntityWithIdDto;
3637
import ru.mystamps.web.dao.dto.TransactionParticipantDto;
3738
import ru.mystamps.web.service.dto.AddParticipantDto;
3839
import ru.mystamps.web.service.dto.GroupedTransactionParticipantDto;
@@ -56,6 +57,7 @@ public void add(AddParticipantDto dto) {
5657
AddParticipantDbDto participant = new AddParticipantDbDto();
5758
participant.setName(dto.getName());
5859
participant.setUrl(dto.getUrl());
60+
participant.setGroupId(dto.getGroupId());
5961
participant.setBuyer(dto.getBuyer());
6062
participant.setSeller(dto.getSeller());
6163

@@ -148,4 +150,11 @@ public List<GroupedTransactionParticipantDto> findAllSellers() {
148150
return items;
149151
}
150152

153+
@Override
154+
@Transactional(readOnly = true)
155+
@PreAuthorize(HasAuthority.ADD_PARTICIPANT)
156+
public List<EntityWithIdDto> findAllGroups() {
157+
return transactionParticipantDao.findAllGroups();
158+
}
159+
151160
}

src/main/java/ru/mystamps/web/service/dto/AddParticipantDto.java

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
public interface AddParticipantDto {
2121
String getName();
2222
String getUrl();
23+
Integer getGroupId();
2324
Boolean getBuyer();
2425
Boolean getSeller();
2526
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ t_stamps_by_categories = Stamps by categories
181181
t_stamps_by_countries = Stamps by countries
182182
t_unspecified = Unspecified
183183

184+
# participant/add.html
185+
t_group = Group
186+
184187
# series/search_result.html
185188
t_search_results = search results
186189
t_no_series_found = No series found

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

+3
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ t_stamps_by_categories = Марки по категориям
181181
t_stamps_by_countries = Марки по странам
182182
t_unspecified = Не указана
183183

184+
# participant/add.html
185+
t_group = Группа
186+
184187
# series/search_result.html
185188
t_search_results = результаты поиска
186189
t_no_series_found = Серии не найдены

src/main/resources/sql/transaction_participants_dao_queries.properties

+8
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ INSERT \
55
, url \
66
, is_buyer \
77
, is_seller \
8+
, group_id \
89
) \
910
VALUES \
1011
( :name \
1112
, :url \
1213
, :buyer \
1314
, :seller \
15+
, :group_id \
1416
)
1517

1618
transaction_participant.find_buyers_with_parent_names = \
@@ -32,3 +34,9 @@ LEFT JOIN transaction_participant_groups g \
3234
ON p.group_id = g.id \
3335
WHERE is_seller = TRUE \
3436
ORDER BY CONCAT(g.name, p.name)
37+
38+
transaction_participant_group.find_all = \
39+
SELECT id \
40+
, name \
41+
FROM transaction_participant_groups \
42+
ORDER BY name

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

+24
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,30 @@ <h3 th:text="${#strings.capitalize(header)}">
103103
</div>
104104
</div>
105105

106+
<div class="form-group form-group-sm" th:classappend="${#fields.hasErrors('groupId') ? 'has-error' : ''}">
107+
<label for="group" class="control-label col-sm-4" th:text="#{t_group}">
108+
Group
109+
</label>
110+
<div class="col-sm-5">
111+
<select id="group" class="form-control" th:field="*{groupId}">
112+
<option value="" th:text="#{t_not_chosen}">Not chosen</option>
113+
<!--/*/
114+
<option th:each="group : ${groups}"
115+
th:value="${group.id}"
116+
th:text="${group.name}"
117+
th:selected="${addParticipantForm.groupId == group.id}">
118+
</option>
119+
/*/-->
120+
<!--/*-->
121+
<option value="1">Musicians</option>
122+
<!--*/-->
123+
</select>
124+
<!--/*/
125+
<span id="group.errors" class="help-block" th:if="${#fields.hasErrors('groupId')}" th:each="error : ${#fields.errors('groupId')}" th:text="${error}"></span>
126+
/*/-->
127+
</div>
128+
</div>
129+
106130
<div class="form-group" th:classappend="${#fields.hasErrors('buyer') ? 'has-error' : ''}">
107131
<label for="buyer" class="control-label col-sm-4" th:text="#{t_buyer}">
108132
Buyer

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

+24-4
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
package ru.mystamps.web.service
1919

2020
import static io.qala.datagen.RandomShortApi.bool
21+
import static io.qala.datagen.RandomShortApi.nullOr
2122

2223
import org.slf4j.helpers.NOPLogger
2324

2425
import spock.lang.Specification
2526

2627
import ru.mystamps.web.dao.TransactionParticipantDao
2728
import ru.mystamps.web.dao.dto.AddParticipantDbDto
29+
import ru.mystamps.web.dao.dto.EntityWithIdDto
2830
import ru.mystamps.web.controller.dto.AddParticipantForm
2931
import ru.mystamps.web.tests.Random
3032

@@ -87,14 +89,16 @@ class TransactionParticipantServiceImplTest extends Specification {
8789
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
8890
def 'add() should create participant'() {
8991
given:
90-
String expectedName = Random.participantName()
91-
String expectedUrl = Random.url()
92-
Boolean expectedBuyer = bool()
93-
Boolean expectedSeller = bool()
92+
String expectedName = Random.participantName()
93+
String expectedUrl = Random.url()
94+
Integer expectedGroupId = nullOr(Random.id())
95+
Boolean expectedBuyer = bool()
96+
Boolean expectedSeller = bool()
9497
and:
9598
AddParticipantForm form = new AddParticipantForm()
9699
form.setName(expectedName)
97100
form.setUrl(expectedUrl)
101+
form.setGroupId(expectedGroupId)
98102
form.setBuyer(expectedBuyer)
99103
form.setSeller(expectedSeller)
100104
when:
@@ -103,10 +107,26 @@ class TransactionParticipantServiceImplTest extends Specification {
103107
1 * transactionParticipantDao.add({ AddParticipantDbDto participant ->
104108
assert participant?.name == expectedName
105109
assert participant?.url == expectedUrl
110+
assert participant?.groupId == expectedGroupId
106111
assert participant?.buyer == expectedBuyer
107112
assert participant?.seller == expectedSeller
108113
return true
109114
})
110115
}
111116

117+
//
118+
// Tests for findAllGroups()
119+
//
120+
121+
def 'findAllGroups() should invoke dao and return its result'() {
122+
given:
123+
List<EntityWithIdDto> expectedResult = Random.listOfEntityWithIdDto()
124+
when:
125+
List<EntityWithIdDto> result = service.findAllGroups()
126+
then:
127+
1 * transactionParticipantDao.findAllGroups() >> expectedResult
128+
and:
129+
result == expectedResult
130+
}
131+
112132
}

src/test/java/ru/mystamps/web/service/TestObjects.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import ru.mystamps.web.tests.Random;
2727
import ru.mystamps.web.util.SlugUtils;
2828

29-
final class TestObjects {
29+
public final class TestObjects {
3030
public static final String TEST_ACTIVITY_TYPE = "EventType";
3131
public static final String TEST_ACTIVITY_PAGE = "http://example.org/some/page";
3232
public static final String TEST_ACTIVITY_METHOD = "GET";

src/test/java/ru/mystamps/web/tests/Random.java

+14
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
import io.qala.datagen.RandomShortApi;
2626

27+
import ru.mystamps.web.dao.dto.EntityWithIdDto;
28+
import ru.mystamps.web.service.TestObjects;
2729
import ru.mystamps.web.validation.ValidationRules;
2830

2931
import static io.qala.datagen.RandomShortApi.integer;
@@ -118,4 +120,16 @@ public static List<Integer> listOfIntegers() {
118120
return sampleMultiple(size, integer(), integer(), integer());
119121
}
120122

123+
public static List<EntityWithIdDto> listOfEntityWithIdDto() {
124+
final int minSize = 1;
125+
final int maxSize = 3;
126+
int size = integer(minSize, maxSize);
127+
return sampleMultiple(
128+
size,
129+
TestObjects.createEntityWithIdDto(),
130+
TestObjects.createEntityWithIdDto(),
131+
TestObjects.createEntityWithIdDto()
132+
);
133+
}
134+
121135
}

src/test/robotframework/participant/creation/logic.robot

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@ Create participant with name only
2121
List Should Contain Value ${availableSellers} participant1
2222
List Should Not Contain Value ${availableBuyers} participant1
2323

24-
Create participant with name and url
24+
Create participant by filling all fields
2525
[Documentation] Verify creation of participant by filling all fields
2626
Input Text id=name participant2
27+
Select From List By Label id=group Movies characters
2728
Select Checkbox id=buyer
2829
Select Checkbox id=seller
2930
Input Text id=url http://participant2.example.org
3031
Submit Form id=add-participant-form
3132
Location Should Be ${SITE_URL}/
3233
Go To ${SITE_URL}/series/1
34+
# TODO: check that buyer and seller listed in the "Movies characters" group
3335
${availableSellers}= Get List Items id=seller
3436
${availableBuyers}= Get List Items id=buyer
3537
List Should Contain Value ${availableSellers} participant2

0 commit comments

Comments
 (0)