Skip to content

Instantly share code, notes, and snippets.

@indrabasak
Created August 8, 2017 18:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indrabasak/7b8049297359813b17569cf591e2807d to your computer and use it in GitHub Desktop.
Save indrabasak/7b8049297359813b17569cf591e2807d to your computer and use it in GitHub Desktop.
@RestController
@Slf4j
@Api(description = "Tiger API", produces = "application/json", tags = {"A2"})
public class TigerController {
private static final String TIGER_URL = "/tigers";
private static final String TIGER_BY_ID_URL = TIGER_URL + "/{id}";
private static final String HEADER_TXN_ID = "X-TXN-ID";
private static final String HEADER_TXN_DATE = "X-TXN-DATE";
private TigerService service;
@Autowired
public TigerController(TigerService service) {
this.service = service;
}
@ApiOperation(
value = "Creates a Tiger.",
notes = "Requires a Tiger name and gender.",
response = Tiger.class)
@ApiResponses({
@ApiResponse(code = 201, response = Tiger.class,
message = "Tiger created successfully")})
@RequestMapping(method = RequestMethod.POST, value = TIGER_URL,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public Tiger create(
@ApiParam(value = "Transaction ID as UUID")
@RequestHeader(HEADER_TXN_ID) UUID txnId,
@RequestBody MenagerieRequest request) {
return service.create(request);
}
@ApiOperation(
value = "Retrieves a tiger by ID.",
notes = "Requires a tiger identifier",
response = Tiger.class)
@RequestMapping(method = RequestMethod.GET, value = TIGER_BY_ID_URL,
produces = {MediaType.APPLICATION_JSON_VALUE})
@ResponseBody
public Tiger read(
@ApiParam(value = "Transaction ID as UUID")
@RequestHeader(HEADER_TXN_ID) UUID txnId,
@ApiParam(value = "Tiger ID", required = true)
@PathVariable("id") UUID id) {
return service.read(id);
}
@ApiOperation(
value = "Retrieves all the tigers associated with the search string.",
notes = "In absence of any parameter, it will return all the tigers.",
response = Tiger.class, responseContainer = "List")
@RequestMapping(method = RequestMethod.GET, value = TIGER_URL,
produces = {MediaType.APPLICATION_JSON_VALUE})
@ResponseBody
public List<Tiger> readAll(
@ApiParam(value = "Transaction ID as UUID")
@RequestHeader(HEADER_TXN_ID) UUID txnId,
@ApiParam(value = "Search string to search a tiger based on search criteria. Returns all tigers if empty.")
@RequestParam(value = "name", required = false) String name,
@RequestParam(value = "gender", required = false) Gender gender) {
return service.readAll(name, gender);
}
@ApiOperation(
value = "Retrieves all the tigers associated with the search string.",
notes = "In absence of any parameter, it will return all the tigers.",
response = Tiger.class, responseContainer = "List")
@RequestMapping(method = RequestMethod.GET, value = TIGER_URL + "/map",
produces = {MediaType.APPLICATION_JSON_VALUE})
@ResponseBody
public Map<UUID, Tiger> readMap(
@ApiParam(value = "Transaction ID as UUID")
@RequestHeader(HEADER_TXN_ID) UUID txnId) {
return service.readAllAsMap();
}
@ApiOperation(value = "Updates a tiger.", response = Tiger.class)
@ApiResponses({
@ApiResponse(code = 201, response = Tiger.class,
message = "Updated a tiger successfully")})
@RequestMapping(method = RequestMethod.PUT, value = TIGER_BY_ID_URL,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public Tiger update(
@ApiParam(value = "Transaction ID as UUID")
@RequestHeader(HEADER_TXN_ID) UUID txnId,
@ApiParam(value = "Tiger ID", required = true)
@PathVariable("id") UUID id,
@RequestBody MenagerieRequest request) {
return service.update(id, request);
}
@ApiOperation(value = "Deletes a tiger by ID.")
@RequestMapping(method = RequestMethod.DELETE, value = TIGER_BY_ID_URL)
@ResponseBody
public void delete(
@ApiParam(value = "Transaction ID as UUID")
@RequestHeader(HEADER_TXN_ID) UUID txnId,
@ApiParam(value = "Tiger ID", required = true)
@PathVariable("id") UUID id) {
service.delete(id);
}
@ApiOperation(value = "Deletes all tigers.")
@RequestMapping(method = RequestMethod.DELETE, value = TIGER_URL)
@ResponseBody
public void deleteAll(
@ApiParam(value = "Transaction ID as UUID")
@RequestHeader(HEADER_TXN_ID) UUID txnId) {
service.deleteAll();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment