37
37
import org .springframework .web .bind .annotation .RequestMapping ;
38
38
import org .springframework .web .bind .annotation .RequestMethod ;
39
39
import org .springframework .web .bind .WebDataBinder ;
40
+ import org .springframework .web .servlet .mvc .support .RedirectAttributes ;
40
41
import org .springframework .web .util .UriComponentsBuilder ;
41
42
42
43
import lombok .RequiredArgsConstructor ;
51
52
import ru .mystamps .web .model .AddSeriesForm .YvertCatalogChecks ;
52
53
import ru .mystamps .web .entity .Series ;
53
54
import ru .mystamps .web .service .CategoryService ;
55
+ import ru .mystamps .web .service .CollectionService ;
54
56
import ru .mystamps .web .service .CountryService ;
55
57
import ru .mystamps .web .service .SeriesService ;
56
58
import ru .mystamps .web .service .dto .EntityInfoDto ;
@@ -68,6 +70,7 @@ public class SeriesController {
68
70
private static final Map <Integer , Integer > YEARS ;
69
71
70
72
private final CategoryService categoryService ;
73
+ private final CollectionService collectionService ;
71
74
private final CountryService countryService ;
72
75
private final SeriesService seriesService ;
73
76
@@ -142,7 +145,7 @@ public String processInput(
142
145
}
143
146
144
147
@ RequestMapping (value = Url .INFO_SERIES_PAGE , method = RequestMethod .GET )
145
- public String showInfo (@ PathVariable ("id" ) Series series , Model model ) {
148
+ public String showInfo (@ PathVariable ("id" ) Series series , Model model , User currentUser ) {
146
149
147
150
if (series == null ) {
148
151
throw new NotFoundException ();
@@ -154,8 +157,63 @@ public String showInfo(@PathVariable("id") Series series, Model model) {
154
157
model .addAttribute ("yvertNumbers" , CatalogUtils .toShortForm (series .getYvert ()));
155
158
model .addAttribute ("gibbonsNumbers" , CatalogUtils .toShortForm (series .getGibbons ()));
156
159
160
+ model .addAttribute (
161
+ "isSeriesInCollection" ,
162
+ collectionService .isSeriesInCollection (currentUser , series )
163
+ );
164
+
157
165
return "series/info" ;
158
166
}
159
167
168
+ @ RequestMapping (
169
+ value = Url .INFO_SERIES_PAGE ,
170
+ method = RequestMethod .POST ,
171
+ params = "action=ADD"
172
+ )
173
+ public String addToCollection (
174
+ @ PathVariable ("id" ) Series series ,
175
+ User currentUser ,
176
+ RedirectAttributes redirectAttributes ) {
177
+
178
+ if (series == null ) {
179
+ throw new NotFoundException ();
180
+ }
181
+
182
+ Integer collectionId = collectionService .addToCollection (currentUser , series );
183
+
184
+ String dstUrl = UriComponentsBuilder .fromUriString (Url .INFO_COLLECTION_PAGE )
185
+ .buildAndExpand (collectionId )
186
+ .toString ();
187
+
188
+ redirectAttributes .addFlashAttribute ("justAddedSeries" , true );
189
+
190
+ return "redirect:" + dstUrl ;
191
+ }
192
+
193
+ @ RequestMapping (
194
+ value = Url .INFO_SERIES_PAGE ,
195
+ method = RequestMethod .POST ,
196
+ params = "action=REMOVE"
197
+ )
198
+ public String removeFromCollection (
199
+ @ PathVariable ("id" ) Series series ,
200
+ User currentUser ,
201
+ RedirectAttributes redirectAttributes ) {
202
+
203
+ if (series == null ) {
204
+ throw new NotFoundException ();
205
+ }
206
+
207
+ Integer collectionId = collectionService .removeFromCollection (currentUser , series );
208
+
209
+ String dstUrl = UriComponentsBuilder .fromUriString (Url .INFO_COLLECTION_PAGE )
210
+ .buildAndExpand (collectionId )
211
+ .toString ();
212
+
213
+ redirectAttributes .addFlashAttribute ("justRemovedSeries" , true );
214
+
215
+ return "redirect:" + dstUrl ;
216
+ }
217
+
160
218
}
161
219
0 commit comments