1 package org.imageconverter.controller;
2
3 import static org.imageconverter.util.controllers.imagetype.ImageTypeConst.REST_URL;
4 import static org.springframework.http.HttpStatus.CREATED;
5 import static org.springframework.http.HttpStatus.NO_CONTENT;
6 import static org.springframework.http.HttpStatus.OK;
7 import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
8 import static org.springframework.http.MediaType.TEXT_PLAIN_VALUE;
9
10 import java.util.List;
11
12 import javax.servlet.http.HttpServletResponse;
13 import javax.validation.Valid;
14
15 import org.imageconverter.application.ImageTypeService;
16 import org.imageconverter.domain.imagetype.ImageType;
17 import org.imageconverter.infra.exception.ElementNotFoundException;
18 import org.imageconverter.util.controllers.imagetype.CreateImageTypeRequest;
19 import org.imageconverter.util.controllers.imagetype.ImageTypeResponse;
20 import org.imageconverter.util.controllers.imagetype.UpdateImageTypeRequest;
21 import org.imageconverter.util.logging.Loggable;
22 import org.imageconverter.util.openapi.imagetype.CreateImageTypeRequestBody;
23 import org.imageconverter.util.openapi.imagetype.ImageTypeRestDeleteOpenApi;
24 import org.imageconverter.util.openapi.imagetype.ImageTypeRestGet;
25 import org.imageconverter.util.openapi.imagetype.ImageTypeRestGetByIdOpenApi;
26 import org.imageconverter.util.openapi.imagetype.ImageTypeRestPostOpenApi;
27 import org.imageconverter.util.openapi.imagetype.ImageTypeRestPutOpenApi;
28 import org.imageconverter.util.openapi.imagetype.UpdateImageTypeRequestBody;
29 import org.springdoc.core.converters.models.PageableAsQueryParam;
30 import org.springframework.context.annotation.Description;
31 import org.springframework.data.domain.Page;
32 import org.springframework.data.domain.Pageable;
33 import org.springframework.data.jpa.domain.Specification;
34 import org.springframework.data.web.PageableDefault;
35 import org.springframework.web.bind.annotation.DeleteMapping;
36 import org.springframework.web.bind.annotation.GetMapping;
37 import org.springframework.web.bind.annotation.PathVariable;
38 import org.springframework.web.bind.annotation.PostMapping;
39 import org.springframework.web.bind.annotation.PutMapping;
40 import org.springframework.web.bind.annotation.RequestBody;
41 import org.springframework.web.bind.annotation.RequestMapping;
42 import org.springframework.web.bind.annotation.ResponseStatus;
43 import org.springframework.web.bind.annotation.RestController;
44
45 import com.turkraft.springfilter.boot.Filter;
46
47 import io.swagger.v3.oas.annotations.Parameter;
48 import io.swagger.v3.oas.annotations.security.SecurityRequirement;
49 import io.swagger.v3.oas.annotations.tags.Tag;
50
51
52
53
54
55
56 @SecurityRequirement(name = "BASIC")
57 @Tag(
58 name = "Image Type",
59 description = """
60 Image Type API - Rest CRUD for image type :) .
61 If something went wrong, please put 'trace=true' (for all Http methods) at the end of the request to receive the stackStrace.
62 Ex: http:
63 """
64 )
65
66 @Loggable
67 @RestController
68 @Description("Controller for image type API")
69 @RequestMapping(REST_URL)
70 public class ImageTypeRestController {
71
72 private final ImageTypeService imageTypeService;
73
74
75
76
77
78
79 ImageTypeRestController(final ImageTypeService imageTypeService) {
80 super();
81 this.imageTypeService = imageTypeService;
82 }
83
84
85
86
87
88
89
90
91 @ImageTypeRestGetByIdOpenApi
92
93 @ResponseStatus(OK)
94 @GetMapping(value = "/{id:[\\d]*}", produces = APPLICATION_JSON_VALUE)
95 public ImageTypeResponse getById(
96 @Parameter(description = "The image type id's", example = "1000")
97 @PathVariable(name = "id", required = true)
98 final Long id) {
99
100 return imageTypeService.findById(id);
101 }
102
103
104
105
106
107
108
109
110 @PageableAsQueryParam
111 @ImageTypeRestGet
112
113 @ResponseStatus(OK)
114 @GetMapping(produces = APPLICATION_JSON_VALUE)
115 public Page<ImageTypeResponse> getByFilter(
116 @Parameter(name = "filter", description = "Search's filter", required = true, example = "?filter=extension:'png'")
117 @Filter
118 final Specification<ImageType> filter,
119
120 @PageableDefault(value = 10, page = 0)
121 final Pageable page) {
122
123 return imageTypeService.findBySpecification(filter, page);
124 }
125
126
127
128
129
130
131
132 @ImageTypeRestPostOpenApi
133
134 @ResponseStatus(CREATED)
135 @PostMapping(consumes = APPLICATION_JSON_VALUE, produces = { TEXT_PLAIN_VALUE, APPLICATION_JSON_VALUE })
136 public void create(
137
138 @CreateImageTypeRequestBody
139 @Valid
140 @RequestBody
141 final CreateImageTypeRequest request,
142
143 final HttpServletResponse response) {
144
145 final var result = imageTypeService.createImageType(request);
146
147 response.addHeader("Location", REST_URL + "/" + result.id());
148 }
149
150
151
152
153
154
155 @ImageTypeRestPutOpenApi
156
157 @ResponseStatus(NO_CONTENT)
158 @PutMapping(value = "/{id:[\\d]*}", consumes = APPLICATION_JSON_VALUE)
159 public void update(
160
161 @Parameter(description = "The image type id's", example = "1000")
162 @PathVariable(name = "id", required = true)
163 final Long id,
164
165 @UpdateImageTypeRequestBody
166 @Valid
167 @RequestBody
168 final UpdateImageTypeRequest request) {
169
170 imageTypeService.updateImageType(id, request);
171 }
172
173
174
175
176
177
178 @ImageTypeRestDeleteOpenApi
179
180 @ResponseStatus(NO_CONTENT)
181 @DeleteMapping("/{id:[\\d]*}")
182 public void delete(
183
184 @Parameter(description = "The image type id's", example = "1000")
185 @PathVariable(name = "id", required = true)
186 final Long id) {
187
188 imageTypeService.deleteImageType(id);
189 }
190 }