Java tutorial
/* * 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.Genre; import net.jkratz.igdb.repository.GenreRepository; 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 * Genre Controller * * @author Joseph Kratz * @author Okan Kaya * * Handles HTTP requests for Genres */ @RestController @RequestMapping(produces = "application/json", value = "/genre") public class GenreController { @Inject GenreRepository genreRepository; private Logger logger = LoggerFactory.getLogger(getClass()); /** * Returns all genres in database * * @return List of Genre objects * @see Genre */ @RequestMapping(value = { "", "/" }, method = RequestMethod.GET) public ResponseEntity<List<Genre>> getGenres() { List<Genre> genres = genreRepository.findAll(); return new ResponseEntity<>(genres, HttpStatus.OK); } /** * Returns a single genre object if exists, otherwise returns HTTP status code 404 * * @param id ID of the Genre * @return Genre when found * @see Genre */ @RequestMapping(value = "/{id}", method = RequestMethod.GET) public ResponseEntity<?> getGenre(@PathVariable("id") Long id) { logger.debug("Attempting to fetch genre with ID: {}", id); Genre genre = genreRepository.findOne(id); if (genre == null) { return new ResponseEntity<>("Genre not found", HttpStatus.NOT_FOUND); } else { return new ResponseEntity<>(genre, HttpStatus.OK); } } }