Java tutorial
/* * 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.services.interfaces.CategoryService; import java.util.List; 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; /** * * @author bryan */ @Api(name = "Category Services", description = "Methods for managing game categories", group = "Games", visibility = ApiVisibility.PUBLIC, stage = ApiStage.ALPHA) @RestController public class CategoryController { @Autowired private CategoryService categoryService; @ApiMethod(description = "Retrieves a list of all Categories") @RequestMapping("/category") public List<Category> searchCategories(@RequestParam(value = "query", required = false) String query) { return categoryService.searchCategories(query); } @ApiMethod(description = "Retrieves the Category at the specified location") @RequestMapping("/category/{id}") public Category getCategory(@PathVariable("id") long id) { return categoryService.findCategoryById(id); } @ApiMethod(description = "Creates a new Category") @RequestMapping(value = "/category", method = RequestMethod.POST) public ResponseEntity<Category> createCategory(@RequestBody Category category) { if (!categoryService.doesCategoryExist(category)) { Category newCategory = categoryService.saveCategory(category); return new ResponseEntity<>(newCategory, HttpStatus.CREATED); } return new ResponseEntity<>(HttpStatus.CONFLICT); } @ApiMethod(description = "Updates the Category at the specified location. Passed Category should contain updated fields") @RequestMapping(value = "/category/{id}", method = RequestMethod.PUT) public ResponseEntity<Category> updateCategory(@PathVariable("id") long id, @RequestBody Category category) { Category currentCategory = categoryService.findCategoryById(id); if (category == null) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } currentCategory.setName(category.getName()); categoryService.updateCategory(currentCategory); return new ResponseEntity<>(currentCategory, HttpStatus.OK); } @ApiMethod(description = "Deletes the Category at the specified location") @RequestMapping(value = "/category/{id}", method = RequestMethod.DELETE) public ResponseEntity<Category> deleteCategory(@PathVariable("id") long id) { if (categoryService.findCategoryById(id) == null) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } categoryService.deleteCategory(id); return new ResponseEntity<>(HttpStatus.NO_CONTENT); } }