1
package by.andd3dfx.templateapp.controllers;
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;
32
@RequiredArgsConstructor
33
@RequestMapping("/api/v1/articles")
34
public class ArticleController {
36
private final IArticleService articleService;
38
@ApiOperation(value = "Create new article", response = ArticleDto.class)
39
@ApiResponses(value = {
40
@ApiResponse(code = 201, message = "Article successfully created"),
43
@ResponseStatus(HttpStatus.CREATED)
44
public ArticleDto createArticle(
45
@ApiParam("New article's data")
47
@RequestBody ArticleDto newArticleDto
49
return articleService.create(newArticleDto);
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"),
58
public ArticleDto readArticle(
59
@ApiParam("Article's id")
63
return articleService.get(id);
66
@ApiOperation("Update article")
67
@ApiResponses(value = {
68
@ApiResponse(code = 200, message = "Article successfully updated"),
69
@ApiResponse(code = 404, message = "Article not found"),
71
@PatchMapping("/{id}")
72
public void updateArticle(
73
@ApiParam("Article's id")
75
@PathVariable Long id,
76
@ApiParam("Updated fields of article")
78
@RequestBody ArticleUpdateDto articleUpdateDto
80
articleService.update(id, articleUpdateDto);
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"),
88
@DeleteMapping("/{id}")
89
@ResponseStatus(HttpStatus.NO_CONTENT)
90
public void deleteArticle(
91
@ApiParam("Article's id")
95
articleService.delete(id);
98
@ApiOperation(value = "Read articles paged", response = Slice.class)
99
@ApiResponses(value = {
100
@ApiResponse(code = 200, message = "Articles successfully retrieved"),
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")
115
public Slice<ArticleDto> readArticlesPaged(
116
@PageableDefault(page = 0, size = 50)
117
@SortDefault.SortDefaults({
118
@SortDefault(sort = "title", direction = Sort.Direction.ASC)
121
return articleService.getAll(pageable);