Skip to content

Commit fc7e116

Browse files
committed
User can specify a price that he/she paid for a series.
Fix #663
1 parent 09fc76c commit fc7e116

File tree

13 files changed

+130
-3
lines changed

13 files changed

+130
-3
lines changed

NEWS.txt

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- (functionality) add support for specifying Solovyov catalog numbers
2222
- (functionality) add support for specifying Zagorski catalog numbers
2323
- (functionality) user may specify how many stamps from a series in his/her collection
24+
- (functionality) user may specify a price that he/she paid for a series
2425

2526
0.3
2627
- (functionality) implemented possibility to user to add series to his collection

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

+10
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,24 @@
1717
*/
1818
package ru.mystamps.web.controller.dto;
1919

20+
import java.math.BigDecimal;
21+
2022
import javax.validation.constraints.Min;
2123
import javax.validation.constraints.NotNull;
2224

2325
import lombok.Getter;
2426
import lombok.Setter;
2527

28+
import ru.mystamps.web.dao.dto.Currency;
2629
import ru.mystamps.web.service.dto.AddToCollectionDto;
2730

2831
import static ru.mystamps.web.validation.ValidationRules.MIN_STAMPS_IN_SERIES;
2932

3033
// @todo #477 Add to collection: integration test for invisible quantity for a series with 1 stamp
3134
// @todo #477 Add to collection: series quantity should be specified by default
3235
// @todo #477 Add to collection: add integration test for custom number of stamps
36+
// @todo #663 Add to collection: add integration test for specifying a price
37+
// @todo #663 Add a page with a list of series, their prices and total cost
3338
@Getter
3439
@Setter
3540
public class AddToCollectionForm implements AddToCollectionDto {
@@ -39,4 +44,9 @@ public class AddToCollectionForm implements AddToCollectionDto {
3944
@Min(MIN_STAMPS_IN_SERIES)
4045
private Integer numberOfStamps;
4146

47+
// @todo #663 /series/{id}(price): must be greater than zero
48+
private BigDecimal price;
49+
50+
// @todo #663 /series/{id}(currency): must be required when price is specified
51+
private Currency currency;
4252
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
package ru.mystamps.web.dao.dto;
1919

20+
import java.math.BigDecimal;
21+
2022
import lombok.Getter;
2123
import lombok.Setter;
2224

@@ -26,4 +28,6 @@ public class AddToCollectionDbDto {
2628
private Integer ownerId;
2729
private Integer seriesId;
2830
private Integer numberOfStamps;
31+
private BigDecimal price;
32+
private String currency;
2933
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ public void addSeriesToUserCollection(AddToCollectionDbDto dto) {
171171
params.put("user_id", dto.getOwnerId());
172172
params.put("series_id", dto.getSeriesId());
173173
params.put("number_of_stamps", dto.getNumberOfStamps());
174+
params.put("price", dto.getPrice());
175+
params.put("currency", dto.getCurrency());
174176

175177
int affected = jdbcTemplate.update(addSeriesToCollectionSql, params);
176178

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

+32-2
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,24 @@ public void addToCollection(Integer userId, Integer seriesId, AddToCollectionDto
8282
collectionDto.setSeriesId(seriesId);
8383
collectionDto.setNumberOfStamps(dto.getNumberOfStamps());
8484

85+
if (dto.getPrice() != null) {
86+
Validate.validState(
87+
dto.getCurrency() != null,
88+
"Currency must be non null when price is specified"
89+
);
90+
collectionDto.setPrice(dto.getPrice());
91+
collectionDto.setCurrency(dto.getCurrency().toString());
92+
}
93+
8594
collectionDao.addSeriesToUserCollection(collectionDto);
8695
collectionDao.markAsModified(userId, new Date());
8796

88-
// TODO: it would be good to include number of stamps in series vs in collection
89-
log.info("Series #{} has been added to collection of user #{}", seriesId, userId);
97+
log.info(
98+
"Series #{} ({}) has been added to collection of user #{}",
99+
seriesId,
100+
formatSeriesInfo(collectionDto),
101+
userId
102+
);
90103
}
91104

92105
@Override
@@ -145,4 +158,21 @@ public CollectionInfoDto findBySlug(String slug) {
145158
return collectionDao.findCollectionInfoBySlug(slug);
146159
}
147160

161+
private static String formatSeriesInfo(AddToCollectionDbDto collectionDto) {
162+
StringBuilder sb = new StringBuilder();
163+
164+
// TODO: it would be good to include number of stamps in series vs in collection
165+
sb.append("stamps=")
166+
.append(collectionDto.getNumberOfStamps());
167+
168+
if (collectionDto.getPrice() != null) {
169+
sb.append(", price=")
170+
.append(collectionDto.getPrice())
171+
.append(' ')
172+
.append(collectionDto.getCurrency());
173+
}
174+
175+
return sb.toString();
176+
}
177+
148178
}

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

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
*/
1818
package ru.mystamps.web.service.dto;
1919

20+
import java.math.BigDecimal;
21+
22+
import ru.mystamps.web.dao.dto.Currency;
23+
2024
public interface AddToCollectionDto {
2125
Integer getNumberOfStamps();
26+
BigDecimal getPrice();
27+
Currency getCurrency();
2228
}

src/main/resources/liquibase/version/0.4.xml

+1
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,6 @@
5050
<include file="0.4/2018-02-09--add_seller_info_to_parsed_data.xml" relativeToChangelogFile="true" />
5151
<include file="0.4/2018-03-11--series_remove_currency_fields.xml" relativeToChangelogFile="true" />
5252
<include file="0.4/2018-06-09--add_number_of_stamps_to_collections_series.xml" relativeToChangelogFile="true" />
53+
<include file="0.4/2018-06-15--add_price_to_collections_series.xml" relativeToChangelogFile="true" />
5354

5455
</databaseChangeLog>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<databaseChangeLog
3+
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
6+
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
7+
8+
<changeSet id="add-price-and-currency-fields-to-collections_series" author="php-coder" context="scheme">
9+
10+
<addColumn tableName="collections_series">
11+
<column name="price" type="DECIMAL(19,2)" />
12+
<column name="currency" type="CHAR(3)" />
13+
</addColumn>
14+
15+
</changeSet>
16+
17+
</databaseChangeLog>

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

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ t_yes = Yes
136136
t_no = No
137137
t_series_not_in_collection = Series isn't part of your collection
138138
t_i_have = I have
139+
t_i_bought_for = I bought for
139140
t_out_of_n_stamps = out of {0} stamps
140141
t_series_in_collection = Series is part of your collection
141142
t_add_to_collection = Add to collection

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

+1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ t_yes = Да
135135
t_no = Нет
136136
t_series_not_in_collection = Этой серии нет в вашей коллекции
137137
t_i_have = У меня есть
138+
t_i_bought_for = Я купил за
138139
t_out_of_n_stamps = из {0} марок
139140
t_series_in_collection = Эта серия есть в вашей коллекции
140141
t_add_to_collection = Добавить в коллекцию

src/main/resources/sql/collection_dao_queries.properties

+9-1
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,18 @@ SELECT COUNT(*) \
5555

5656
collection.add_series_to_collection = \
5757
INSERT \
58-
INTO collections_series(collection_id, series_id, number_of_stamps) \
58+
INTO collections_series \
59+
( collection_id \
60+
, series_id \
61+
, number_of_stamps \
62+
, price \
63+
, currency \
64+
) \
5965
SELECT c.id AS collection_id \
6066
, :series_id AS series_id \
6167
, :number_of_stamps AS number_of_stamps \
68+
, :price as price \
69+
, :currency as currency \
6270
FROM collections c \
6371
WHERE c.user_id = :user_id
6472

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

+29
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,35 @@
369369
th:text="${error}"></span>
370370
</p>
371371
/*/-->
372+
<p th:with="price_msg=#{t_price}">
373+
<span th:text="#{t_i_bought_for}" th:remove="tag">I bought for</span>
374+
<input id="paid-price"
375+
type="text"
376+
size="5"
377+
placeholder="price"
378+
th:placeholder="${#strings.unCapitalize(price_msg)}"
379+
th:field="*{price}"
380+
/>
381+
<select id="paid-currency" th:field="*{currency}">
382+
<option value=""></option>
383+
<option value="USD">USD</option>
384+
<option value="EUR">EUR</option>
385+
<option value="RUB">RUB</option>
386+
<option value="CZK">CZK</option>
387+
</select>
388+
</p>
389+
<!--/*/
390+
<p th:if="${#fields.hasErrors('price') or #fields.hasErrors('currency')}" th:classappend="has-error">
391+
<span id="paid-price.errors"
392+
class="help-block"
393+
th:each="error : ${#fields.errors('price')}"
394+
th:text="${error}"></span>
395+
<span id="paid-currency.errors"
396+
class="help-block"
397+
th:each="error : ${#fields.errors('currency')}"
398+
th:text="${error}"></span>
399+
</p>
400+
/*/-->
372401
<p>
373402
<input type="hidden" name="action" value="ADD" />
374403
<input type="hidden" name="numberOfStamps" value="1" th:if="${series.quantity == 1}" />

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

+17
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import ru.mystamps.web.dao.CollectionDao
3030
import ru.mystamps.web.dao.dto.AddCollectionDbDto
3131
import ru.mystamps.web.dao.dto.AddToCollectionDbDto
3232
import ru.mystamps.web.dao.dto.CollectionInfoDto
33+
import ru.mystamps.web.dao.dto.Currency
3334
import ru.mystamps.web.tests.DateUtils
3435
import ru.mystamps.web.tests.Random
3536
import ru.mystamps.web.util.SlugUtils
@@ -147,15 +148,31 @@ class CollectionServiceImplTest extends Specification {
147148
thrown IllegalArgumentException
148149
}
149150

151+
def 'addToCollection() should throw exception when price is specified without currency'() {
152+
given:
153+
AddToCollectionForm dto = new AddToCollectionForm()
154+
dto.setPrice(Random.price())
155+
dto.setNumberOfStamps(Random.quantity())
156+
dto.setCurrency(null)
157+
when:
158+
service.addToCollection(Random.userId(), Random.id(), dto)
159+
then:
160+
thrown IllegalStateException
161+
}
162+
150163
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
151164
def 'addToCollection() should add series to collection and mark it as modified'() {
152165
given:
153166
Integer expectedUserId = Random.userId()
154167
Integer expectedSeriesId = Random.id()
155168
Integer expectedNumberOfStamps = Random.quantity()
169+
BigDecimal expectedPrice = Random.price()
170+
Currency expectedCurrency = Random.currency()
156171
and:
157172
AddToCollectionForm form = new AddToCollectionForm()
158173
form.setNumberOfStamps(expectedNumberOfStamps)
174+
form.setPrice(expectedPrice)
175+
form.setCurrency(expectedCurrency)
159176
when:
160177
service.addToCollection(expectedUserId, expectedSeriesId, form)
161178
then:

0 commit comments

Comments
 (0)