ca.qhrtech.controllers.GameController.java Source code

Java tutorial

Introduction

Here is the source code for ca.qhrtech.controllers.GameController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.qhrtech.controllers;

import ca.qhrtech.entities.Category;
import ca.qhrtech.entities.Game;
import ca.qhrtech.services.interfaces.BGGService;
import ca.qhrtech.services.interfaces.CategoryService;
import ca.qhrtech.services.interfaces.GameService;
import org.jsondoc.core.annotation.Api;
import org.jsondoc.core.annotation.ApiMethod;
import org.jsondoc.core.pojo.ApiStage;
import org.jsondoc.core.pojo.ApiVisibility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 *
 * @author bryan
 */
@Api(name = "Game Services", description = "Methods for managing games", group = "Games", visibility = ApiVisibility.PUBLIC, stage = ApiStage.ALPHA)
@RestController
public class GameController {

    @Autowired
    private GameService gameService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private BGGService bggService;

    @ApiMethod(description = "Search for Games. Null query will return all Games")
    @RequestMapping("/game")
    public List<Game> searchGames(@RequestParam(value = "query", required = false) String query) {
        return gameService.searchGames(query);
    }

    @ApiMethod(description = "Search games of a specific category")
    @RequestMapping("/game/category/{categoryId}")
    public List<Game> searchGamesByCategory(@PathVariable("categoryId") long categoryId) {
        return gameService.searchGamesByCategory(categoryId);
    }

    @ApiMethod(description = "Retrieve the Game at the specified location")
    @RequestMapping("/game/{id}")
    public Game getGame(@PathVariable("id") long id) {
        return gameService.findGameById(id);
    }

    @ApiMethod(description = "Create a new Game")
    @RequestMapping(value = "/game", method = RequestMethod.POST)
    public ResponseEntity<Game> createGame(@RequestBody Game game) {
        if (!gameService.doesGameExist(game)) {
            Game newGame = gameService.saveGame(game);
            return new ResponseEntity<>(newGame, HttpStatus.CREATED);
        }
        return new ResponseEntity<>(HttpStatus.CONFLICT);
    }

    @ApiMethod(description = "Updates the Game at the specified location. Passed Game should contain the updated fields")
    @RequestMapping(value = "/game/{id}", method = RequestMethod.PUT)
    public ResponseEntity<Game> updateGame(@PathVariable("id") long id, @RequestBody Game game) {
        Game currentGame = gameService.findGameById(id);
        if (game == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        currentGame.setBggId(game.getBggId());
        currentGame.setCategories(game.getCategories());
        currentGame.setImagePath(game.getImagePath());
        currentGame.setMaxPlayTimeMins(game.getMaxPlayTimeMins());
        currentGame.setMaxPlayers(game.getMaxPlayers());
        currentGame.setMinPlayTimeMins(game.getMinPlayTimeMins());
        currentGame.setMinPlayers(game.getMinPlayers());
        currentGame.setName(game.getName());
        gameService.updateGame(currentGame);
        return new ResponseEntity<>(currentGame, HttpStatus.OK);

    }

    @ApiMethod(description = "Adds the Category at the specified location to the Game at the specified location")
    @RequestMapping(value = "/game/{id}/category/{categoryId}", method = RequestMethod.PUT)
    public ResponseEntity<Game> addCategory(@PathVariable("id") long id,
            @PathVariable("categoryId") long categoryId) {

        Category category = categoryService.findCategoryById(categoryId);
        Game game = gameService.findGameById(id);
        if (category == null || game == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        gameService.addCategory(game, category);
        return new ResponseEntity<>(game, HttpStatus.OK);
    }

    @ApiMethod(description = "Removes the Category at the specified location from the Game at the specified location")
    @RequestMapping(value = "/game/{id}/category/{categoryId}", method = RequestMethod.DELETE)
    public ResponseEntity<Game> removeCategory(@PathVariable("id") long id,
            @PathVariable("categoryId") long categoryId) {
        Category category = categoryService.findCategoryById(categoryId);
        Game game = gameService.findGameById(id);
        if (category == null || game == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        gameService.removeCategory(game, category);
        return new ResponseEntity<>(game, HttpStatus.OK);
    }

    @ApiMethod(description = "Deletes a game from the bgl database")
    @RequestMapping(value = "/game/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<Game> deleteGame(@PathVariable("id") long id) {
        if (gameService.findGameById(id) == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        gameService.deleteGame(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ApiMethod(description = "Searches for BoardGameGeek Games to Link")
    @RequestMapping(value = "/game/{id}/link", method = RequestMethod.GET)
    public ResponseEntity<List<Game>> findLinkableGames(@PathVariable("id") long id) {
        List<Game> linkableGames = bggService.findLinkableGames(id);
        if (linkableGames.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return new ResponseEntity(linkableGames, HttpStatus.OK);
    }

    @ApiMethod(description = "Links the BGL Game to the BGG Game")
    @RequestMapping(value = "/game{bglId}/link/{bggId}", method = RequestMethod.GET)
    public ResponseEntity<Game> linkGame(@PathVariable("bglId") long bglId, @PathVariable("bggId") long bggId) {
        Game linkedGame = bggService.linkGame(bglId, bggId);
        if (linkedGame.getBggId() <= 0) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(linkedGame, HttpStatus.OK);
    }

}