composite-build-usage-example

Форк
0
123 строки · 4.8 Кб
1
package by.andd3dfx.templateapp.controllers;
2

3
import by.andd3dfx.templateapp.dto.ArticleDto;
4
import by.andd3dfx.templateapp.dto.ArticleUpdateDto;
5
import by.andd3dfx.templateapp.services.IArticleService;
6
import io.swagger.annotations.ApiImplicitParam;
7
import io.swagger.annotations.ApiImplicitParams;
8
import io.swagger.annotations.ApiOperation;
9
import io.swagger.annotations.ApiParam;
10
import io.swagger.annotations.ApiResponse;
11
import io.swagger.annotations.ApiResponses;
12
import javax.validation.constraints.NotNull;
13
import lombok.RequiredArgsConstructor;
14
import org.springframework.data.domain.Pageable;
15
import org.springframework.data.domain.Slice;
16
import org.springframework.data.domain.Sort;
17
import org.springframework.data.web.PageableDefault;
18
import org.springframework.data.web.SortDefault;
19
import org.springframework.http.HttpStatus;
20
import org.springframework.validation.annotation.Validated;
21
import org.springframework.web.bind.annotation.DeleteMapping;
22
import org.springframework.web.bind.annotation.GetMapping;
23
import org.springframework.web.bind.annotation.PatchMapping;
24
import org.springframework.web.bind.annotation.PathVariable;
25
import org.springframework.web.bind.annotation.PostMapping;
26
import org.springframework.web.bind.annotation.RequestBody;
27
import org.springframework.web.bind.annotation.RequestMapping;
28
import org.springframework.web.bind.annotation.ResponseStatus;
29
import org.springframework.web.bind.annotation.RestController;
30

31
@RestController
32
@RequiredArgsConstructor
33
@RequestMapping("/api/v1/articles")
34
public class ArticleController {
35

36
    private final IArticleService articleService;
37

38
    @ApiOperation(value = "Create new article", response = ArticleDto.class)
39
    @ApiResponses(value = {
40
        @ApiResponse(code = 201, message = "Article successfully created"),
41
    })
42
    @PostMapping
43
    @ResponseStatus(HttpStatus.CREATED)
44
    public ArticleDto createArticle(
45
        @ApiParam("New article's data")
46
        @Validated
47
        @RequestBody ArticleDto newArticleDto
48
    ) {
49
        return articleService.create(newArticleDto);
50
    }
51

52
    @ApiOperation(value = "Get article by id", response = ArticleDto.class)
53
    @ApiResponses(value = {
54
        @ApiResponse(code = 200, message = "Article successfully retrieved"),
55
        @ApiResponse(code = 404, message = "Article not found"),
56
    })
57
    @GetMapping("/{id}")
58
    public ArticleDto readArticle(
59
        @ApiParam("Article's id")
60
        @NotNull
61
        @PathVariable Long id
62
    ) {
63
        return articleService.get(id);
64
    }
65

66
    @ApiOperation("Update article")
67
    @ApiResponses(value = {
68
        @ApiResponse(code = 200, message = "Article successfully updated"),
69
        @ApiResponse(code = 404, message = "Article not found"),
70
    })
71
    @PatchMapping("/{id}")
72
    public void updateArticle(
73
        @ApiParam("Article's id")
74
        @NotNull
75
        @PathVariable Long id,
76
        @ApiParam("Updated fields of article")
77
        @Validated
78
        @RequestBody ArticleUpdateDto articleUpdateDto
79
    ) {
80
        articleService.update(id, articleUpdateDto);
81
    }
82

83
    @ApiOperation("Delete article by id")
84
    @ApiResponses(value = {
85
        @ApiResponse(code = 204, message = "Article successfully deleted"),
86
        @ApiResponse(code = 404, message = "Article not found"),
87
    })
88
    @DeleteMapping("/{id}")
89
    @ResponseStatus(HttpStatus.NO_CONTENT)
90
    public void deleteArticle(
91
        @ApiParam("Article's id")
92
        @NotNull
93
        @PathVariable Long id
94
    ) {
95
        articleService.delete(id);
96
    }
97

98
    @ApiOperation(value = "Read articles paged", response = Slice.class)
99
    @ApiResponses(value = {
100
        @ApiResponse(code = 200, message = "Articles successfully retrieved"),
101
    })
102
    @GetMapping
103
    // Workaround for Swagger bug, according to https://github.com/springfox/springfox/issues/2623#issuecomment-414297583
104
    @ApiImplicitParams({
105
        @ApiImplicitParam(name = "page", dataType = "integer", paramType = "query",
106
            value = "Results page you want to retrieve (0..N)", defaultValue = "0"),
107
        @ApiImplicitParam(name = "size", dataType = "integer", paramType = "query",
108
            value = "Number of records per page.", defaultValue = "50"),
109
        @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
110
            value = "Sorting criteria in the format: property,asc(|desc). " +
111
                "Default sort order is ascending. " +
112
                "Multiple sort criteria are supported.",
113
            defaultValue = "title,ASC")
114
    })
115
    public Slice<ArticleDto> readArticlesPaged(
116
        @PageableDefault(page = 0, size = 50)
117
        @SortDefault.SortDefaults({
118
            @SortDefault(sort = "title", direction = Sort.Direction.ASC)
119
        }) Pageable pageable
120
    ) {
121
        return articleService.getAll(pageable);
122
    }
123
}
124

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.