Skip to content

Commit 8d45632

Browse files
committed
/participant/add: add ability to specify a type of participant (seller, buyer, or both).
Addressed to #499
1 parent 655ff33 commit 8d45632

File tree

8 files changed

+94
-12
lines changed

8 files changed

+94
-12
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.web.bind.annotation.GetMapping;
2727
import org.springframework.web.bind.annotation.InitBinder;
2828
import org.springframework.web.bind.annotation.PostMapping;
29+
import org.springframework.web.bind.annotation.RequestParam;
2930

3031
import lombok.RequiredArgsConstructor;
3132

@@ -49,8 +50,15 @@ protected void initBinder(WebDataBinder binder) {
4950
}
5051

5152
@GetMapping(Url.ADD_PARTICIPANT_PAGE)
52-
public AddParticipantForm showForm() {
53-
return new AddParticipantForm();
53+
public AddParticipantForm showForm(
54+
@RequestParam(name = "seller", required = false) Boolean seller,
55+
@RequestParam(name = "buyer", required = false) Boolean buyer) {
56+
57+
AddParticipantForm form = new AddParticipantForm();
58+
form.setSeller(seller);
59+
form.setBuyer(buyer);
60+
61+
return form;
5462
}
5563

5664
@PostMapping(Url.ADD_PARTICIPANT_PAGE)

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package ru.mystamps.web.controller.dto;
1919

20+
import javax.validation.constraints.NotNull;
2021
import javax.validation.constraints.Size;
2122

2223
import org.hibernate.validator.constraints.NotEmpty;
@@ -46,4 +47,9 @@ public class AddParticipantForm implements AddParticipantDto {
4647
@Size(max = PARTICIPANT_URL_MAX_LENGTH, message = "{value.too-long}")
4748
private String url;
4849

50+
@NotNull
51+
private Boolean buyer;
52+
53+
@NotNull
54+
private Boolean seller;
4955
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ public class TransactionParticipantServiceImpl implements TransactionParticipant
4747
public void add(AddParticipantDto dto) {
4848
Validate.isTrue(dto != null, "DTO must be non null");
4949
Validate.isTrue(dto.getName() != null, "Name must be non null");
50+
Validate.isTrue(dto.getBuyer() != null, "Buyer flag must be non null");
51+
Validate.isTrue(dto.getSeller() != null, "Seller flag must be non null");
5052

5153
AddParticipantDbDto participant = new AddParticipantDbDto();
5254
participant.setName(dto.getName());
5355
participant.setUrl(dto.getUrl());
54-
participant.setBuyer(Boolean.TRUE);
55-
participant.setSeller(Boolean.TRUE);
56+
participant.setBuyer(dto.getBuyer());
57+
participant.setSeller(dto.getSeller());
5658

5759
transactionParticipantDao.add(participant);
5860

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@
2020
public interface AddParticipantDto {
2121
String getName();
2222
String getUrl();
23+
Boolean getBuyer();
24+
Boolean getSeller();
2325
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,30 @@ <h3 th:text="${#strings.capitalize(header)}">
105105
</div>
106106
</div>
107107

108+
<div class="form-group" th:classappend="${#fields.hasErrors('buyer') ? 'has-error' : ''}">
109+
<label for="buyer" class="control-label col-sm-4" th:text="#{t_buyer}">
110+
Buyer
111+
</label>
112+
<div class="col-sm-1">
113+
<input id="buyer" type="checkbox" class="form-control" th:field="*{buyer}" />
114+
<!--/*/
115+
<span id="buyer.errors" class="help-block" th:if="${#fields.hasErrors('buyer')}" th:each="error : ${#fields.errors('buyer')}" th:text="${error}"></span>
116+
/*/-->
117+
</div>
118+
</div>
119+
120+
<div class="form-group" th:classappend="${#fields.hasErrors('seller') ? 'has-error' : ''}">
121+
<label for="seller" class="control-label col-sm-4" th:text="#{t_seller}">
122+
Seller
123+
</label>
124+
<div class="col-sm-1">
125+
<input id="seller" type="checkbox" checked="checked" class="form-control" th:field="*{seller}" />
126+
<!--/*/
127+
<span id="seller.errors" class="help-block" th:if="${#fields.hasErrors('seller')}" th:each="error : ${#fields.errors('seller')}" th:text="${error}"></span>
128+
/*/-->
129+
</div>
130+
</div>
131+
108132
<div class="form-group">
109133
<div class="col-sm-offset-4 col-sm-5">
110134
<input type="submit" class="btn btn-primary" value="Add" th:value="#{t_add}" />

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,8 @@ <h5 th:text="|#{t_add_info_who_selling_series}:|">Add info about selling/buying
416416
<!--*/-->
417417
</select>
418418
<small sec:authorize="hasAuthority('ADD_PARTICIPANT')">
419-
<span class="hint-block" th:utext="#{t_add_seller_hint(@{${ADD_PARTICIPANT_PAGE}})}">
420-
You can also <a tabindex="-1" href="../participant/add.html">add a new seller</a>
419+
<span class="hint-block" th:utext="#{t_add_seller_hint(@{${ADD_PARTICIPANT_PAGE}(seller=true)})}">
420+
You can also <a tabindex="-1" href="../participant/add.html?seller=true">add a new seller</a>
421421
</span>
422422
</small>
423423
<!--/*/
@@ -517,8 +517,8 @@ <h5 th:text="|#{t_add_info_who_selling_series}:|">Add info about selling/buying
517517
<!--*/-->
518518
</select>
519519
<small sec:authorize="hasAuthority('ADD_PARTICIPANT')">
520-
<span class="hint-block" th:utext="#{t_add_buyer_hint(@{${ADD_PARTICIPANT_PAGE}})}">
521-
You can also <a tabindex="-1" href="../participant/add.html">add a new buyer</a>
520+
<span class="hint-block" th:utext="#{t_add_buyer_hint(@{${ADD_PARTICIPANT_PAGE}(buyer=true)})}">
521+
You can also <a tabindex="-1" href="../participant/add.html?buyer=true">add a new buyer</a>
522522
</span>
523523
</small>
524524
<!--/*/

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
package ru.mystamps.web.service
1919

20+
import static io.qala.datagen.RandomShortApi.bool
21+
2022
import org.slf4j.helpers.NOPLogger
2123

2224
import spock.lang.Specification
@@ -25,6 +27,7 @@ import ru.mystamps.web.dao.TransactionParticipantDao
2527
import ru.mystamps.web.dao.dto.AddParticipantDbDto
2628
import ru.mystamps.web.dao.dto.EntityWithIdDto
2729
import ru.mystamps.web.controller.dto.AddParticipantForm
30+
import ru.mystamps.web.tests.Random
2831

2932
@SuppressWarnings(['ClassJavadoc', 'MethodName', 'NoDef', 'NoTabCharacter', 'TrailingWhitespace'])
3033
class TransactionParticipantServiceImplTest extends Specification {
@@ -50,6 +53,32 @@ class TransactionParticipantServiceImplTest extends Specification {
5053
given:
5154
AddParticipantForm form = new AddParticipantForm()
5255
form.setName(null)
56+
form.setBuyer(bool())
57+
form.setSeller(bool())
58+
when:
59+
service.add(form)
60+
then:
61+
thrown IllegalArgumentException
62+
}
63+
64+
def 'add() should throw exception when buyer flag is null'() {
65+
given:
66+
AddParticipantForm form = new AddParticipantForm()
67+
form.setName(Random.participantName())
68+
form.setBuyer(null)
69+
form.setSeller(bool())
70+
when:
71+
service.add(form)
72+
then:
73+
thrown IllegalArgumentException
74+
}
75+
76+
def 'add() should throw exception when seller flag is null'() {
77+
given:
78+
AddParticipantForm form = new AddParticipantForm()
79+
form.setName(Random.participantName())
80+
form.setBuyer(bool())
81+
form.setSeller(null)
5382
when:
5483
service.add(form)
5584
then:
@@ -59,20 +88,24 @@ class TransactionParticipantServiceImplTest extends Specification {
5988
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
6089
def 'add() should create participant'() {
6190
given:
62-
String expectedName = 'test'
63-
String expectedUrl = 'http://example.org'
91+
String expectedName = Random.participantName()
92+
String expectedUrl = Random.url()
93+
Boolean expectedBuyer = bool()
94+
Boolean expectedSeller = bool()
6495
and:
6596
AddParticipantForm form = new AddParticipantForm()
6697
form.setName(expectedName)
6798
form.setUrl(expectedUrl)
99+
form.setBuyer(expectedBuyer)
100+
form.setSeller(expectedSeller)
68101
when:
69102
service.add(form)
70103
then:
71104
1 * transactionParticipantDao.add({ AddParticipantDbDto participant ->
72105
assert participant?.name == expectedName
73106
assert participant?.url == expectedUrl
74-
assert participant?.buyer
75-
assert participant?.seller
107+
assert participant?.buyer == expectedBuyer
108+
assert participant?.seller == expectedSeller
76109
return true
77110
})
78111
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ public static String countryName() {
8585
.english();
8686
}
8787

88+
public static String participantName() {
89+
return between(
90+
ValidationRules.PARTICIPANT_NAME_MIN_LENGTH,
91+
ValidationRules.PARTICIPANT_NAME_MAX_LENGTH
92+
).english();
93+
}
94+
8895
public static Integer issueYear() {
8996
return between(ValidationRules.MIN_RELEASE_YEAR, Year.now().getValue()).integer();
9097
}

0 commit comments

Comments
 (0)