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

Java tutorial

Introduction

Here is the source code for net.jkratz.igdb.controller.ESRBRatingController.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 java.util.List;

import javax.inject.Inject;

import net.jkratz.igdb.model.ESRBRating;
import net.jkratz.igdb.repository.ESRBRatingRepository;
import net.jkratz.igdb.service.ESRBRatingService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 *  IGDB
 *  ESRBRatingController
 *
 *  @author Joseph Kratz
 *  @author Okan Kaya
 *
 *  Handles HTTP requests for ESRB Ratings
 */
@RestController
@RequestMapping(produces = "application/json", value = "/esrbrating")
public class ESRBRatingController {

    @Inject
    ESRBRatingService esrbRatingService;

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

    /**
     * Returns all ESRB ratings in database
     * 
     * @return List of ESRBRating objects
     * @see ESRBRating
     */
    @RequestMapping(value = { "", "/" }, method = RequestMethod.GET)
    public ResponseEntity<List<ESRBRating>> getESRBRatings() {
        List<ESRBRating> esrbRatings = esrbRatingService.getESRBRatings();
        return new ResponseEntity<>(esrbRatings, HttpStatus.OK);
    }

    /**
     * Returns a single ESRB rating object if exists, otherwise returns HTTP status code 404
     *
     * @param id ID of the ESRB Rating
     * @return ESRB Rating when found
     * @see ESRBRating
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity<?> getUser(@PathVariable("id") Long id) {
        logger.debug("Attempting to fetch ESRB rating with ID: {}", id);
        ESRBRating esrbRating = esrbRatingService.getESRBRating(id);
        //ESRBRating esrbRating = esrbRatingRepository.findOne(id);

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

}