|
| 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 | +package ru.mystamps.web.feature.series; |
| 19 | + |
| 20 | +import lombok.RequiredArgsConstructor; |
| 21 | +import org.springframework.http.ResponseEntity; |
| 22 | +import org.springframework.validation.annotation.Validated; |
| 23 | +import org.springframework.web.bind.annotation.PatchMapping; |
| 24 | +import org.springframework.web.bind.annotation.PathVariable; |
| 25 | +import org.springframework.web.bind.annotation.RequestBody; |
| 26 | +import org.springframework.web.bind.annotation.RestController; |
| 27 | +import ru.mystamps.web.support.spring.mvc.PatchRequest; |
| 28 | +import ru.mystamps.web.support.spring.mvc.PatchRequest.Operation; |
| 29 | + |
| 30 | +import javax.servlet.http.HttpServletResponse; |
| 31 | +import javax.validation.Valid; |
| 32 | +import javax.validation.constraints.NotEmpty; |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.List; |
| 35 | + |
| 36 | +@Validated |
| 37 | +@RestController |
| 38 | +@RequiredArgsConstructor |
| 39 | +class RestSeriesController { |
| 40 | + |
| 41 | + private final SeriesService seriesService; |
| 42 | + |
| 43 | + // @todo #785 Update series: add integration test |
| 44 | + // @todo #785 Update series: add validation for a comment |
| 45 | + @PatchMapping(SeriesUrl.INFO_SERIES_PAGE) |
| 46 | + @SuppressWarnings("PMD.TooFewBranchesForASwitchStatement") |
| 47 | + public ResponseEntity<Void> updateSeries( |
| 48 | + @PathVariable("id") Integer seriesId, |
| 49 | + @RequestBody @Valid @NotEmpty List<@Valid PatchRequest> patches, |
| 50 | + HttpServletResponse response) throws IOException { |
| 51 | + |
| 52 | + if (seriesId == null) { |
| 53 | + response.sendError(HttpServletResponse.SC_NOT_FOUND); |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + if (!seriesService.isSeriesExist(seriesId)) { |
| 58 | + response.sendError(HttpServletResponse.SC_NOT_FOUND); |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + for (PatchRequest patch : patches) { |
| 63 | + if (patch.op != Operation.add) { |
| 64 | + // @todo #785 Update series: properly fail on non-supported operations |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + switch (patch.path) { |
| 69 | + case "/comment": |
| 70 | + seriesService.addComment(seriesId, patch.value); |
| 71 | + break; |
| 72 | + default: |
| 73 | + // @todo #785 Update series: properly fail on invalid path |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return ResponseEntity.noContent().build(); |
| 79 | + } |
| 80 | + |
| 81 | +} |
| 82 | + |
0 commit comments