Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swagger implementation #7

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.0.2</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/packt/mygamelist/MyGameListApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
public class MyGameListApp {
Expand Down
34 changes: 33 additions & 1 deletion src/main/java/org/packt/mygamelist/web/GameController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.packt.mygamelist.web;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.packt.mygamelist.domain.Game;
import org.packt.mygamelist.services.GameService;
import org.springframework.http.ResponseEntity;
Expand All @@ -10,7 +14,7 @@
import java.util.List;
import java.util.Optional;


@Tag(name = "Game Controller", description = "Controller for managing games")
@RestController
@RequestMapping("api/games")
public class GameController {
Expand All @@ -21,6 +25,10 @@ public GameController(GameService gameService) {
}

@GetMapping
@Operation(summary = "Get all available or unavailable games")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "List of games returned successfully")
})
public ResponseEntity<List<Game>> getGames(@RequestParam(value = "gameAvail", required = false) Boolean gameAvail) {
if (gameAvail == null) {
List<Game> games = gameService.findAll();
Expand All @@ -32,30 +40,50 @@ public ResponseEntity<List<Game>> getGames(@RequestParam(value = "gameAvail", re
}

@GetMapping("/game/{name}")
@Operation(summary = "Get games by name")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Game returned successfully")
})
public ResponseEntity<Optional<Game>> getGameByName(@PathVariable("name") String name) {
Optional<Game> game = gameService.findByName(name);
return ResponseEntity.ok().body(game);
}

@GetMapping("/byPrice/{price}")
@Operation(summary = "Get games by price")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "List of game/s returned successfully")
})
public ResponseEntity<List<Game>> getGamesByPrice(@PathVariable("price") int price) {
List<Game> games = gameService.findByPrice(price);
return ResponseEntity.ok().body(games);
}

@DeleteMapping("/{id}")
@Operation(summary = "Delete game by id")
@ApiResponses(value = {
@ApiResponse(responseCode = "204", description = "Game deleted successfully")
})
public ResponseEntity<Game> deleteGameById(@PathVariable("id") Long id) {
gameService.delete(id);
return ResponseEntity.noContent().build();
}

@DeleteMapping("/byName/{name}")
@Operation(summary = "Delete game by name")
@ApiResponses(value = {
@ApiResponse(responseCode = "204", description = "Game deleted successfully")
})
public ResponseEntity<Game> deleteGameByName(@PathVariable("name") String name) {
gameService.deleteByName(name);
return ResponseEntity.noContent().build();
}

@PutMapping("/{name}")
@Operation(summary = "Update existing game")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Game updated successfully")
})
public ResponseEntity<Game> updateGame(@RequestBody Game updatedGame) {
Optional<Game> updated = gameService.update(updatedGame);
return updated
Expand All @@ -71,6 +99,10 @@ public ResponseEntity<Game> updateGame(@RequestBody Game updatedGame) {
}

@PostMapping
@Operation(summary = "Create game")
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "Game created successfully")
})
public ResponseEntity<Game> createGame(@RequestBody Game game) {
Game created = gameService.create(game);
URI location = ServletUriComponentsBuilder.fromCurrentRequest()
Expand Down