Skip to content

Commit bc1288a

Browse files
committed
feat: admin can add a release year to a series.
Implementation details: a release year can be added only if it isn't set yet. This is a protection against possible race or other unforeseeable circumstances that might lead to data loss. A full implementation of the editing can be added later so this can be considered as the first iteration. Fix #1343
1 parent b80811b commit bc1288a

File tree

12 files changed

+108
-7
lines changed

12 files changed

+108
-7
lines changed

NEWS.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- (feature) series images can be replaced
44
- (feature) add preliminary support for hidden images
55
- (feature) admin can add a comment to a series
6+
- (feature) admin can add a release year to a series
67
- (improvement) on a country info page show the series with an image
78

89
0.4.3

src/main/frontend/src/components/AddReleaseYearForm.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ class AddReleaseYearForm extends React.PureComponent {
5555
const data = response.data;
5656
if (data.hasOwnProperty('fieldErrors')) {
5757
const fieldErrors = [];
58-
if (data.fieldErrors.year) {
59-
fieldErrors.push(...data.fieldErrors.year);
58+
if (data.fieldErrors.value) {
59+
fieldErrors.push(...data.fieldErrors.value);
6060
}
6161
this.setState({
6262
isDisabled: false,
@@ -114,7 +114,7 @@ class AddReleaseYearFormView extends React.PureComponent {
114114
</div>
115115
<div className="form-group form-group-sm">
116116
<label className="control-label col-sm-3">
117-
{ l10n['t_release_year'] || 'Release year' }
117+
{ l10n['t_year'] || 'Year' }
118118
</label>
119119
<div className="col-sm-6">
120120
<select
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (C) 2009-2020 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+
19+
package ru.mystamps.web.feature.series;
20+
21+
import lombok.Getter;
22+
import lombok.Setter;
23+
24+
import java.util.Date;
25+
26+
@Getter
27+
@Setter
28+
public class AddReleaseYearDbDto {
29+
private Integer seriesId;
30+
private Integer releaseYear;
31+
private Date updatedAt;
32+
private Integer updatedBy;
33+
}

src/main/java/ru/mystamps/web/feature/series/JdbcSeriesDao.java

+21
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public class JdbcSeriesDao implements SeriesDao {
5151
@Value("${series.add_comment}")
5252
private String addCommentSql;
5353

54+
@Value("${series.add_release_year}")
55+
private String addReleaseYearSql;
56+
5457
@Value("${series.mark_as_modified}")
5558
private String markAsModifiedSql;
5659

@@ -155,6 +158,24 @@ public void addComment(Integer seriesId, String comment) {
155158
);
156159
}
157160

161+
@Override
162+
public void addReleaseYear(AddReleaseYearDbDto dto) {
163+
Map<String, Object> params = new HashMap<>();
164+
params.put("series_id", dto.getSeriesId());
165+
params.put("release_year", dto.getReleaseYear());
166+
params.put("updated_at", dto.getUpdatedAt());
167+
params.put("updated_by", dto.getUpdatedBy());
168+
169+
int affected = jdbcTemplate.update(addReleaseYearSql, params);
170+
171+
// @todo #1343 Update series: handle refuse to update an existing release year gracefully
172+
Validate.validState(
173+
affected == 1,
174+
"Unexpected number of affected rows after updating series: %d",
175+
affected
176+
);
177+
}
178+
158179
/**
159180
* @author Sergey Chechenev
160181
*/

src/main/java/ru/mystamps/web/feature/series/RestSeriesController.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.web.bind.annotation.RestController;
2727
import ru.mystamps.web.support.spring.mvc.PatchRequest;
2828
import ru.mystamps.web.support.spring.mvc.PatchRequest.Operation;
29+
import ru.mystamps.web.support.spring.security.CurrentUser;
2930

3031
import javax.servlet.http.HttpServletResponse;
3132
import javax.validation.Valid;
@@ -42,11 +43,12 @@ class RestSeriesController {
4243

4344
// @todo #785 Update series: add integration test
4445
// @todo #785 Update series: add validation for a comment
46+
// @todo #1343 Update series: add validation for a release year
4547
@PatchMapping(SeriesUrl.INFO_SERIES_PAGE)
46-
@SuppressWarnings("PMD.TooFewBranchesForASwitchStatement")
4748
public ResponseEntity<Void> updateSeries(
4849
@PathVariable("id") Integer seriesId,
4950
@RequestBody @Valid @NotEmpty List<@Valid PatchRequest> patches,
51+
@CurrentUser Integer currentUserId,
5052
HttpServletResponse response) throws IOException {
5153

5254
if (seriesId == null) {
@@ -69,6 +71,12 @@ public ResponseEntity<Void> updateSeries(
6971
case "/comment":
7072
seriesService.addComment(seriesId, patch.getValue());
7173
break;
74+
case "/release_year":
75+
seriesService.addReleaseYear(
76+
seriesId,
77+
Integer.valueOf(patch.getValue()),
78+
currentUserId);
79+
break;
7280
default:
7381
// @todo #785 Update series: properly fail on invalid path
7482
break;

src/main/java/ru/mystamps/web/feature/series/SeriesDao.java

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
public interface SeriesDao {
2626
Integer add(AddSeriesDbDto series);
2727
void addComment(Integer seriesId, String comment);
28+
void addReleaseYear(AddReleaseYearDbDto dto);
2829
void markAsModified(Integer seriesId, Date updateAt, Integer updatedBy);
2930
List<SitemapInfoDto> findAllForSitemap();
3031
List<SeriesLinkDto> findSimilarSeries(Integer seriesId, String lang);

src/main/java/ru/mystamps/web/feature/series/SeriesService.java

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
public interface SeriesService {
2626
Integer add(AddSeriesDto dto, Integer userId, boolean userCanAddComments);
2727
void addComment(Integer seriesId, String comment);
28+
void addReleaseYear(Integer seriesId, Integer year, Integer userId);
2829
void addImageToSeries(AddImageDto dto, Integer seriesId, Integer userId);
2930
void replaceImage(ReplaceImageDto dto, Integer seriesId, Integer userId);
3031
long countAll();

src/main/java/ru/mystamps/web/feature/series/SeriesServiceImpl.java

+22
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,28 @@ public void addComment(Integer seriesId, String comment) {
135135
log.info("Series #{}: a comment has been added", seriesId);
136136
}
137137

138+
// @todo #1343 SeriesServiceImpl.addReleaseYear(): add unit tests
139+
@Override
140+
@Transactional
141+
@PreAuthorize(HasAuthority.CREATE_SERIES)
142+
public void addReleaseYear(Integer seriesId, Integer year, Integer userId) {
143+
Validate.isTrue(seriesId != null, "Series id must be non null");
144+
Validate.isTrue(year != null, "Release year must be non null");
145+
Validate.isTrue(userId != null, "User id must be non null");
146+
147+
AddReleaseYearDbDto dto = new AddReleaseYearDbDto();
148+
dto.setSeriesId(seriesId);
149+
dto.setReleaseYear(year);
150+
dto.setUpdatedBy(userId);
151+
152+
Date now = new Date();
153+
dto.setUpdatedAt(now);
154+
155+
seriesDao.addReleaseYear(dto);
156+
157+
log.info("Series #{}: release year set to {}", seriesId, year);
158+
}
159+
138160
@Override
139161
@Transactional
140162
@PreAuthorize("isAuthenticated()")

src/main/java/ru/mystamps/web/feature/site/ResourceUrl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public final class ResourceUrl {
3232
public static final String STATIC_RESOURCES_URL = "https://stamps.filezz.ru";
3333

3434
// MUST be updated when any of our resources were modified
35-
public static final String RESOURCES_VERSION = "v0.4.3.10";
35+
public static final String RESOURCES_VERSION = "v0.4.3.11";
3636

3737
// CheckStyle: ignore LineLength for next 15 lines
3838
private static final String CATALOG_UTILS_JS = "/public/js/" + RESOURCES_VERSION + "/CatalogUtils.min.js";

src/main/java/ru/mystamps/web/support/spring/security/SecurityConfig.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ protected void configure(HttpSecurity http) throws Exception {
109109
.mvcMatchers(CountryUrl.ADD_COUNTRY_PAGE).hasAuthority(StringAuthority.CREATE_COUNTRY)
110110
.mvcMatchers(ParticipantUrl.ADD_PARTICIPANT_PAGE).hasAuthority(StringAuthority.ADD_PARTICIPANT)
111111
.mvcMatchers(SeriesUrl.ADD_SERIES_PAGE).hasAuthority(StringAuthority.CREATE_SERIES)
112-
.mvcMatchers(HttpMethod.PATCH, SeriesUrl.INFO_SERIES_PAGE).hasAuthority(StringAuthority.ADD_COMMENTS_TO_SERIES)
112+
.mvcMatchers(HttpMethod.PATCH, SeriesUrl.INFO_SERIES_PAGE)
113+
.hasAnyAuthority(StringAuthority.CREATE_SERIES, StringAuthority.ADD_COMMENTS_TO_SERIES)
113114
.mvcMatchers(SeriesImportUrl.REQUEST_IMPORT_SERIES_PAGE).hasAuthority(StringAuthority.IMPORT_SERIES)
114115
.mvcMatchers(SiteUrl.SITE_EVENTS_PAGE).hasAuthority(StringAuthority.VIEW_SITE_EVENTS)
115116
.mvcMatchers(CategoryUrl.SUGGEST_SERIES_CATEGORY).hasAuthority(StringAuthority.CREATE_SERIES)

src/main/resources/sql/series_dao_queries.properties

+8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ UPDATE series \
4747
WHERE id = :series_id \
4848
AND comment IS NULL
4949

50+
series.add_release_year = \
51+
UPDATE series \
52+
SET release_year = :release_year \
53+
, updated_at = :updated_at \
54+
, updated_by = :updated_by \
55+
WHERE id = :series_id \
56+
AND release_year IS NULL
57+
5058
series.mark_as_modified = \
5159
UPDATE series \
5260
SET updated_at = :updated_at \

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -1049,9 +1049,14 @@ <h5 th:text="#{t_add_info_who_selling_series}">Add info about selling/buying thi
10491049
/*[# th:if="${series.releaseYear == null}"]*/
10501050
/*[+
10511051
var addReleaseYearProps = {
1052+
'url': [[ '__@{${INFO_SERIES_PAGE}(id=${series.id})}__' ]],
10521053
'csrfHeaderName': [[ ${_csrf.headerName} ]],
10531054
'csrfTokenValue': [[ ${_csrf.token} ]],
1054-
'l10n': {},
1055+
'l10n': {
1056+
't_server_error': [[ #{t_server_error} ]],
1057+
't_year': [[ #{t_year} ]],
1058+
't_add': [[ #{t_add} ]]
1059+
},
10551060
'sinceYear': [[ ${T(ru.mystamps.web.feature.series.SeriesValidation).MIN_RELEASE_YEAR} ]],
10561061
'tillYear': new Date().getFullYear()
10571062
};

0 commit comments

Comments
 (0)