net.jkratz.igdb.controller.GameController.java Source code

Java tutorial

Introduction

Here is the source code for net.jkratz.igdb.controller.GameController.java

Source

/*
 * Copyright 2002-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package net.jkratz.igdb.controller;

import javax.inject.Inject;
import javax.validation.Valid;

import net.jkratz.igdb.model.Game;
import net.jkratz.igdb.repository.GameRepository;
import net.jkratz.igdb.service.GameService;
import net.jkratz.igdb.util.PageResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 *  IGDB
 *  GameController
 *
 *  @author Joseph Kratz
 *  @author Okan Kaya
 *
 *  Handles HTTP requests for Games
 */
@RestController
@RequestMapping(produces = "application/json")
public class GameController {

    @Inject
    GameService gameService;

    private Logger logger = LoggerFactory.getLogger(getClass());

    @RequestMapping(value = { "/game", "/game/" }, method = RequestMethod.GET)
    public PageResource<Game> getGames(@RequestParam(value = "page", defaultValue = "0") int page,
            @RequestParam(value = "size", defaultValue = "25") int size,
            @RequestParam(value = "platform", required = false) Long platformId,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "year", required = false) Integer year,
            @RequestParam(value = "genre", required = false) Long genreId) {

        logger.debug("Page: {}", page);
        logger.debug("Size: {}", size);
        logger.debug("PlatformID: {}", platformId);
        logger.debug("Title: {}", title);
        logger.debug("Year: {}", year);
        logger.debug("GenreID: {}", genreId);

        Page<Game> pageResult = gameService.getGames(page, size, platformId, title, year, genreId);
        return new PageResource<>(pageResult, "page", "size");
    }

    @RequestMapping(value = "/game/{id}", method = RequestMethod.GET)
    public ResponseEntity<?> getGame(@PathVariable("id") Long id) {

        Game game = gameService.getGame(id);

        if (game == null) {
            return new ResponseEntity<>("User not found", HttpStatus.NOT_FOUND);
        } else {
            return new ResponseEntity<>(game, HttpStatus.OK);
        }
    }

    @RequestMapping(value = "/game", method = RequestMethod.POST)
    public ResponseEntity<Game> createGame(@Valid @RequestBody Game game) {
        Game newGame = gameService.saveGame(game);
        return new ResponseEntity<>(newGame, HttpStatus.OK);
    }

    @RequestMapping(value = "/game/{id}", method = RequestMethod.PUT)
    public ResponseEntity<Game> updateGame(@Valid @RequestBody Game game) {
        return null;
    }

    @RequestMapping(value = "/game/{id}", method = RequestMethod.DELETE)
    public boolean deleteGame(@PathVariable("id") Long id) {
        return true;
    }
}