Skip to content

Commit 75c5589

Browse files
committed
feat: admin can hide an image
Part of #1383
1 parent 41d9d4d commit 75c5589

File tree

9 files changed

+172
-3
lines changed

9 files changed

+172
-3
lines changed

NEWS.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
0.x (upcoming release)
2-
- (feature) users can add a comment to a series
2+
- (feature) users can add a comment to a series
3+
- (feature) admin can hide an image
34
- (infrastructure) migrate to JUnit 5
45

56
0.4.5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (C) 2009-2021 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+
package ru.mystamps.web.feature.series;
19+
20+
import lombok.RequiredArgsConstructor;
21+
import org.apache.commons.lang3.Validate;
22+
import org.springframework.beans.factory.annotation.Value;
23+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
24+
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
28+
@RequiredArgsConstructor
29+
public class JdbcSeriesImageDao implements SeriesImageDao {
30+
31+
private final NamedParameterJdbcTemplate jdbcTemplate;
32+
33+
@Value("${series_images.mark_as_hidden}")
34+
private String hideImageSql;
35+
36+
@Override
37+
public void hideImage(Integer seriesId, Integer imageId) {
38+
Map<String, Object> params = new HashMap<>();
39+
params.put("series_id", seriesId);
40+
params.put("image_id", imageId);
41+
42+
int affected = jdbcTemplate.update(hideImageSql, params);
43+
44+
Validate.validState(
45+
affected == 1,
46+
"Unexpected number of affected rows after hiding image #%d in series #%d: %d",
47+
imageId,
48+
seriesId,
49+
affected
50+
);
51+
}
52+
53+
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
class RestSeriesController {
4444

4545
private final SeriesService seriesService;
46+
private final SeriesImageService seriesImageService;
4647

4748
// @todo #1339 Update series: add validation for catalog numbers
4849
// @todo #1340 Update series: add validation for a price
@@ -125,6 +126,8 @@ public ResponseEntity<Void> modifySeriesImage(
125126
return null;
126127
}
127128

129+
seriesImageService.hideImage(seriesId, form.getImageId());
130+
128131
return ResponseEntity.noContent().build();
129132
}
130133

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

+20-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public static class Controllers {
5252
private final CollectionService collectionService;
5353
private final CountryService countryService;
5454
private final SeriesService seriesService;
55+
private final SeriesImageService seriesImageService;
5556
private final SeriesImportService seriesImportService;
5657
private final SeriesSalesService seriesSalesService;
5758
private final ParticipantService participantService;
@@ -71,7 +72,7 @@ public SeriesController seriesController() {
7172

7273
@Bean
7374
public RestSeriesController restSeriesController() {
74-
return new RestSeriesController(seriesService);
75+
return new RestSeriesController(seriesService, seriesImageService);
7576
}
7677

7778
}
@@ -80,6 +81,7 @@ public RestSeriesController restSeriesController() {
8081
@RequiredArgsConstructor
8182
public static class Services {
8283

84+
private final SeriesImageDao seriesImageDao;
8385
private final ImageService imageService;
8486
private final Map<String, StampsCatalogDao> stampsCatalogDaos;
8587

@@ -105,6 +107,14 @@ public SeriesService seriesService(
105107
zagorskiCatalogService
106108
);
107109
}
110+
111+
@Bean
112+
public SeriesImageService seriesImageService() {
113+
return new SeriesImageServiceImpl(
114+
LoggerFactory.getLogger(SeriesImageServiceImpl.class),
115+
seriesImageDao
116+
);
117+
}
108118

109119
@Bean(name = "michelCatalog")
110120
public StampsCatalogService michelCatalogService() {
@@ -163,7 +173,10 @@ public StampsCatalogService zagorskiCatalogService() {
163173
}
164174

165175
@RequiredArgsConstructor
166-
@PropertySource("classpath:/sql/stamps_catalog_dao_queries.properties")
176+
@PropertySource({
177+
"classpath:sql/series_image_dao_queries.properties",
178+
"classpath:/sql/stamps_catalog_dao_queries.properties"
179+
})
167180
/* default */ static class Daos {
168181

169182
private final NamedParameterJdbcTemplate jdbcTemplate;
@@ -174,6 +187,11 @@ public SeriesDao seriesDao() {
174187
return new JdbcSeriesDao(jdbcTemplate);
175188
}
176189

190+
@Bean
191+
public SeriesImageDao seriesImageDao() {
192+
return new JdbcSeriesImageDao(jdbcTemplate);
193+
}
194+
177195
@Bean
178196
public StampsCatalogDao michelCatalogDao() {
179197
return new JdbcStampsCatalogDao(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (C) 2009-2021 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+
package ru.mystamps.web.feature.series;
19+
20+
public interface SeriesImageDao {
21+
void hideImage(Integer seriesId, Integer imageId);
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (C) 2009-2021 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+
package ru.mystamps.web.feature.series;
19+
20+
public interface SeriesImageService {
21+
void hideImage(Integer seriesId, Integer imageId);
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (C) 2009-2021 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+
package ru.mystamps.web.feature.series;
19+
20+
import lombok.RequiredArgsConstructor;
21+
import org.apache.commons.lang3.Validate;
22+
import org.slf4j.Logger;
23+
import org.springframework.security.access.prepost.PreAuthorize;
24+
import org.springframework.transaction.annotation.Transactional;
25+
import ru.mystamps.web.support.spring.security.HasAuthority;
26+
27+
@RequiredArgsConstructor
28+
public class SeriesImageServiceImpl implements SeriesImageService {
29+
30+
private final Logger log;
31+
private final SeriesImageDao seriesImageDao;
32+
33+
@Override
34+
@Transactional
35+
@PreAuthorize(HasAuthority.HIDE_IMAGE)
36+
public void hideImage(Integer seriesId, Integer imageId) {
37+
Validate.isTrue(seriesId != null, "Series id must be non null");
38+
Validate.isTrue(imageId != null, "Image id must be non null");
39+
40+
seriesImageDao.hideImage(seriesId, imageId);
41+
42+
log.info("Series #{}: image #{} has been hidden", seriesId, imageId);
43+
}
44+
45+
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public final class HasAuthority {
3636
public static final String CREATE_COUNTRY = "hasAuthority('" + StringAuthority.CREATE_COUNTRY + "')";
3737
public static final String CREATE_SERIES = "hasAuthority('" + StringAuthority.CREATE_SERIES + "')";
3838
public static final String DOWNLOAD_IMAGE = "hasAuthority('" + StringAuthority.DOWNLOAD_IMAGE + "')";
39+
public static final String HIDE_IMAGE = "hasAuthority('" + StringAuthority.HIDE_IMAGE + "')";
3940
public static final String IMPORT_SERIES = "hasAuthority('" + StringAuthority.IMPORT_SERIES + "')";
4041
public static final String MARK_SIMILAR_SERIES = "hasAuthority('" + StringAuthority.MARK_SIMILAR_SERIES + "')";
4142
public static final String REPLACE_IMAGE = "hasAuthority('" + StringAuthority.REPLACE_IMAGE + "')";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
series_images.mark_as_hidden = \
2+
UPDATE series_images \
3+
SET hidden = TRUE \
4+
WHERE series_id = :series_id AND image_id = :image_id

0 commit comments

Comments
 (0)