Skip to content

Commit 7edca47

Browse files
committed
Added some updates for controller
1 parent 7c28acc commit 7edca47

File tree

3 files changed

+104
-56
lines changed

3 files changed

+104
-56
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.kaluzny.demo.web;
2+
3+
import com.kaluzny.demo.domain.Automobile;
4+
import io.swagger.v3.oas.annotations.Operation;
5+
import io.swagger.v3.oas.annotations.Parameter;
6+
import io.swagger.v3.oas.annotations.media.ArraySchema;
7+
import io.swagger.v3.oas.annotations.media.Content;
8+
import io.swagger.v3.oas.annotations.media.Schema;
9+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
10+
import io.swagger.v3.oas.annotations.responses.ApiResponses;
11+
import io.swagger.v3.oas.annotations.tags.Tag;
12+
import org.springframework.web.bind.annotation.PathVariable;
13+
14+
import java.util.Collection;
15+
16+
@Tag(name = "Automobile", description = "the Automobile API")
17+
public interface AutomobileOpenApi extends AutomobileResource {
18+
19+
@Operation(summary = "Add a new Automobile", description = "endpoint for creating an entity", tags = {"Automobile"})
20+
@ApiResponses(value = {
21+
@ApiResponse(responseCode = "201", description = "Automobile created"),
22+
@ApiResponse(responseCode = "400", description = "Invalid input"),
23+
@ApiResponse(responseCode = "409", description = "Automobile already exists")})
24+
Automobile saveAutomobile(@Parameter(description = "Automobile", required = true) Automobile automobile);
25+
26+
@Operation(summary = "Find all Automobiles", description = " ", tags = {"Automobile"})
27+
@ApiResponses(value = {
28+
@ApiResponse(responseCode = "200", description = "successful operation",
29+
content = @Content(array = @ArraySchema(schema = @Schema(implementation = Automobile.class))))})
30+
Collection<Automobile> getAllAutomobiles();
31+
32+
@Operation(summary = "Find automobile by ID", description = "Returns a single automobile", tags = {"Automobile"})
33+
@ApiResponses(value = {
34+
@ApiResponse(responseCode = "200", description = "successful operation",
35+
content = @Content(schema = @Schema(implementation = Automobile.class))),
36+
@ApiResponse(responseCode = "404", description = "There is no such automobile")})
37+
Automobile getAutomobileById(
38+
@Parameter(description = "Id of the Automobile to be obtained. Cannot be empty.", required = true)
39+
@PathVariable Long id);
40+
41+
@Operation(summary = "Find automobile by name", description = " ", tags = {"Automobile"})
42+
@ApiResponses(value = {
43+
@ApiResponse(responseCode = "200", description = "successful operation",
44+
content = @Content(array = @ArraySchema(schema = @Schema(implementation = Automobile.class))))})
45+
Collection<Automobile> findAutomobileByName(
46+
@Parameter(description = "Name of the Automobile to be obtained. Cannot be empty.", required = true) String name);
47+
48+
@Operation(summary = "Update an existing Automobile", description = "need to fill", tags = {"Automobile"})
49+
@ApiResponses(value = {
50+
@ApiResponse(responseCode = "200", description = "successful operation"),
51+
@ApiResponse(responseCode = "400", description = "Invalid ID supplied"),
52+
@ApiResponse(responseCode = "404", description = "Automobile not found"),
53+
@ApiResponse(responseCode = "405", description = "Validation exception")})
54+
Automobile refreshAutomobile(
55+
@Parameter(description = "Id of the Automobile to be update. Cannot be empty.", required = true) Long id,
56+
@Parameter(description = "Automobile to update.", required = true) Automobile automobile);
57+
58+
@Operation(summary = "Deletes a Automobile", description = "need to fill", tags = {"Automobile"})
59+
@ApiResponses(value = {
60+
@ApiResponse(responseCode = "200", description = "successful operation"),
61+
@ApiResponse(responseCode = "404", description = "Automobile not found")})
62+
String removeAutomobileById(
63+
@Parameter(description = "Id of the Automobile to be delete. Cannot be empty.", required = true) Long id);
64+
65+
66+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.kaluzny.demo.web;
2+
3+
import com.kaluzny.demo.domain.Automobile;
4+
5+
import java.util.Collection;
6+
import java.util.List;
7+
8+
public interface AutomobileResource {
9+
10+
Automobile saveAutomobile(Automobile automobile);
11+
12+
Collection<Automobile> getAllAutomobiles();
13+
14+
Automobile getAutomobileById(Long id);
15+
16+
Collection<Automobile> findAutomobileByName(String name);
17+
18+
Automobile refreshAutomobile(Long id, Automobile automobile);
19+
20+
String removeAutomobileById(Long id);
21+
22+
void removeAllAutomobiles();
23+
24+
Collection<Automobile> findAutomobileByColor(String color);
25+
26+
Collection<Automobile> findAutomobileByNameAndColor(String name, String color);
27+
28+
Collection<Automobile> findAutomobileByColorStartsWith(String colorStartsWith, int page, int size);
29+
30+
List<String> getAllAutomobilesByName();
31+
}

src/main/java/com/kaluzny/demo/web/AutomobileRestController.java

Lines changed: 7 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,12 @@
55
import com.kaluzny.demo.exception.AutoWasDeletedException;
66
import com.kaluzny.demo.exception.ThereIsNoSuchAutoException;
77
import io.swagger.v3.oas.annotations.Hidden;
8-
import io.swagger.v3.oas.annotations.Operation;
98
import io.swagger.v3.oas.annotations.Parameter;
10-
import io.swagger.v3.oas.annotations.media.ArraySchema;
11-
import io.swagger.v3.oas.annotations.media.Content;
12-
import io.swagger.v3.oas.annotations.media.Schema;
13-
import io.swagger.v3.oas.annotations.responses.ApiResponse;
14-
import io.swagger.v3.oas.annotations.responses.ApiResponses;
15-
import io.swagger.v3.oas.annotations.tags.Tag;
169
import jakarta.annotation.PostConstruct;
17-
import jakarta.annotation.security.RolesAllowed;
18-
import jakarta.validation.constraints.NotNull;
10+
import jakarta.validation.Valid;
1911
import lombok.RequiredArgsConstructor;
2012
import lombok.extern.slf4j.Slf4j;
2113
import org.springframework.cache.annotation.CacheEvict;
22-
import org.springframework.cache.annotation.Cacheable;
2314
import org.springframework.data.domain.PageRequest;
2415
import org.springframework.data.domain.Sort;
2516
import org.springframework.http.HttpStatus;
@@ -38,8 +29,7 @@
3829
@RequestMapping(value = "/api", produces = MediaType.APPLICATION_JSON_VALUE)
3930
@RequiredArgsConstructor
4031
@Slf4j
41-
@Tag(name = "Automobile", description = "the Automobile API")
42-
public class AutomobileRestController {
32+
public class AutomobileRestController implements AutomobileResource, AutomobileOpenApi {
4333

4434
private final AutomobileRepository repository;
4535

@@ -53,27 +43,17 @@ public void init() {
5343
repository.save(new Automobile(1L, "Ford", "Green", Instant.now(), Instant.now(), true, false));
5444
}
5545

56-
/*@Operation(summary = "Add a new Automobile", description = "endpoint for creating an entity", tags = {"Automobile"})
57-
@ApiResponses(value = {
58-
@ApiResponse(responseCode = "201", description = "Automobile created"),
59-
@ApiResponse(responseCode = "400", description = "Invalid input"),
60-
@ApiResponse(responseCode = "409", description = "Automobile already exists")})*/
6146
@PostMapping("/automobiles")
6247
@ResponseStatus(HttpStatus.CREATED)
6348
@PreAuthorize("hasRole('ADMIN')")
6449
//@RolesAllowed("ADMIN")
65-
public Automobile saveAutomobile(
66-
@Parameter(description = "Automobile", required = true) @NotNull @RequestBody Automobile automobile) {
50+
public Automobile saveAutomobile(@Valid @RequestBody Automobile automobile) {
6751
log.info("saveAutomobile() - start: automobile = {}", automobile);
6852
Automobile savedAutomobile = repository.save(automobile);
6953
log.info("saveAutomobile() - end: savedAutomobile = {}", savedAutomobile.getId());
7054
return savedAutomobile;
7155
}
7256

73-
@Operation(summary = "Find all Automobiles", description = " ", tags = {"Automobile"})
74-
@ApiResponses(value = {
75-
@ApiResponse(responseCode = "200", description = "successful operation",
76-
content = @Content(array = @ArraySchema(schema = @Schema(implementation = Automobile.class))))})
7757
@GetMapping("/automobiles")
7858
@ResponseStatus(HttpStatus.OK)
7959
//@Cacheable(value = "automobile", sync = true)
@@ -85,17 +65,10 @@ public Collection<Automobile> getAllAutomobiles() {
8565
return collection;
8666
}
8767

88-
@Operation(summary = "Find automobile by ID", description = "Returns a single automobile", tags = {"Automobile"})
89-
@ApiResponses(value = {
90-
@ApiResponse(responseCode = "200", description = "successful operation",
91-
content = @Content(schema = @Schema(implementation = Automobile.class))),
92-
@ApiResponse(responseCode = "404", description = "There is no such automobile")})
9368
@GetMapping("/automobiles/{id}")
9469
@ResponseStatus(HttpStatus.OK)
9570
//@Cacheable(value = "automobile", sync = true)
96-
public Automobile getAutomobileById(
97-
@Parameter(description = "Id of the Automobile to be obtained. Cannot be empty.", required = true)
98-
@PathVariable Long id) {
71+
public Automobile getAutomobileById(@PathVariable Long id) {
9972
log.info("getAutomobileById() - start: id = {}", id);
10073
Automobile receivedAutomobile = repository.findById(id)
10174
//.orElseThrow(() -> new EntityNotFoundException("Automobile not found with id = " + id));
@@ -108,35 +81,19 @@ public Automobile getAutomobileById(
10881
}
10982

11083
@Hidden
111-
@Operation(summary = "Find automobile by name", description = " ", tags = {"Automobile"})
112-
@ApiResponses(value = {
113-
@ApiResponse(responseCode = "200", description = "successful operation",
114-
content = @Content(array = @ArraySchema(schema = @Schema(implementation = Automobile.class))))})
11584
@GetMapping(value = "/automobiles", params = {"name"})
11685
@ResponseStatus(HttpStatus.OK)
117-
public Collection<Automobile> findAutomobileByName(
118-
@Parameter(description = "Name of the Automobile to be obtained. Cannot be empty.", required = true)
119-
@RequestParam(value = "name") String name) {
86+
public Collection<Automobile> findAutomobileByName(@RequestParam(value = "name") String name) {
12087
log.info("findAutomobileByName() - start: name = {}", name);
12188
Collection<Automobile> collection = repository.findByName(name);
12289
log.info("findAutomobileByName() - end: collection = {}", collection);
12390
return collection;
12491
}
12592

126-
@Operation(summary = "Update an existing Automobile", description = "need to fill", tags = {"Automobile"})
127-
@ApiResponses(value = {
128-
@ApiResponse(responseCode = "200", description = "successful operation"),
129-
@ApiResponse(responseCode = "400", description = "Invalid ID supplied"),
130-
@ApiResponse(responseCode = "404", description = "Automobile not found"),
131-
@ApiResponse(responseCode = "405", description = "Validation exception")})
13293
@PutMapping("/automobiles/{id}")
13394
@ResponseStatus(HttpStatus.OK)
13495
//@CachePut(value = "automobile", key = "#id")
135-
public Automobile refreshAutomobile(
136-
@Parameter(description = "Id of the Automobile to be update. Cannot be empty.", required = true)
137-
@PathVariable Long id,
138-
@Parameter(description = "Automobile to update.", required = true)
139-
@RequestBody Automobile automobile) {
96+
public Automobile refreshAutomobile(@PathVariable Long id, @RequestBody Automobile automobile) {
14097
log.info("refreshAutomobile() - start: id = {}, automobile = {}", id, automobile);
14198
Automobile updatedAutomobile = repository.findById(id)
14299
.map(entity -> {
@@ -155,16 +112,10 @@ public Automobile refreshAutomobile(
155112
return updatedAutomobile;
156113
}
157114

158-
@Operation(summary = "Deletes a Automobile", description = "need to fill", tags = {"Automobile"})
159-
@ApiResponses(value = {
160-
@ApiResponse(responseCode = "200", description = "successful operation"),
161-
@ApiResponse(responseCode = "404", description = "Automobile not found")})
162115
@DeleteMapping("/automobiles/{id}")
163116
@ResponseStatus(HttpStatus.NO_CONTENT)
164117
@CacheEvict(value = "automobile", key = "#id")
165-
public String removeAutomobileById(
166-
@Parameter(description = "Id of the Automobile to be delete. Cannot be empty.", required = true)
167-
@PathVariable Long id) {
118+
public String removeAutomobileById(@PathVariable Long id) {
168119
log.info("removeAutomobileById() - start: id = {}", id);
169120
Automobile deletedAutomobile = repository.findById(id)
170121
.orElseThrow(ThereIsNoSuchAutoException::new);

0 commit comments

Comments
 (0)