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.GameResult; import ca.qhrtech.services.interfaces.ResultService; import java.util.HashSet; import java.util.Set; 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.bergen */ @Api(name = "Game Result Services", description = "Methods for managing game results", group = "Games", visibility = ApiVisibility.PUBLIC, stage = ApiStage.ALPHA) @RestController public class ResultController { @Autowired private ResultService resultService; @ApiMethod(description = "Retrieves a list of Game Results with optional filters") @RequestMapping("/result") public Set<GameResult> getResults(@RequestParam(value = "tableId", required = false) Long tableId, @RequestParam(value = "userId", required = false) Long userId, @RequestParam(value = "gameId", required = false) Long gameId) { Set<GameResult> results = new HashSet<>(); if (tableId != null) { results.addAll(resultService.getResultsForTable(tableId)); } if (userId != null) { results.addAll(resultService.getResultsForUser(userId)); } if (gameId != null) { results.addAll(resultService.getResultsForGame(gameId)); } return results; } @ApiMethod(description = "Retrieves the Game Result at the specified location") @RequestMapping("/result/{id}") public GameResult getResult(@PathVariable("id") long id) { return resultService.findResultById(id); } @ApiMethod(description = "Create a new Game Result") @RequestMapping(value = "/result", method = RequestMethod.POST) public ResponseEntity<GameResult> createResult(@RequestBody GameResult result) { if (!resultService.doesResultExist(result)) { GameResult newResult = resultService.saveResult(result); return new ResponseEntity<>(newResult, HttpStatus.CREATED); } return new ResponseEntity<>(HttpStatus.CONFLICT); } @ApiMethod(description = "Updates the Game Result at the specified location") @RequestMapping(value = "/result/{id}", method = RequestMethod.PUT) public ResponseEntity<GameResult> updateResult(@PathVariable("id") long id, @RequestBody GameResult result) { GameResult currentResult = resultService.findResultById(id); if (currentResult == null) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } currentResult.setComments(result.getComments()); currentResult.setTeam(result.getTeam()); currentResult.setWinner(result.isWinner()); resultService.updateResult(currentResult); return new ResponseEntity<>(currentResult, HttpStatus.OK); } @ApiMethod(description = "Deletes the Game Result at the specified location") @RequestMapping(value = "/result/{id}", method = RequestMethod.DELETE) public ResponseEntity<Void> deleteResult(@PathVariable("id") long id) { if (resultService.findResultById(id) == null) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } resultService.deleteResult(id); return new ResponseEntity<>(HttpStatus.NO_CONTENT); } }