Created
January 10, 2018 16:50
-
-
Save finsterthecat/da51c99782321bc16f802eb6e093298c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@PostMapping(consumes = "application/json", produces = "application/json") | |
@ResponseStatus(HttpStatus.CREATED) | |
public Hero createHero(Hero hero) { | |
LOG.debug("createHero: {}", hero.getName()); | |
Hero createdHero = heroRepository.save(hero); | |
LOG.debug("Created hero {} with id {}", createdHero.getName(), createdHero.getId()); | |
return createdHero; | |
} | |
@GetMapping(produces = "application/json") | |
@ResponseStatus(HttpStatus.OK) | |
public @ResponseBody Iterable<Hero> allHeroes() { | |
LOG.debug("allHeroes"); | |
return heroRepository.findAll(); | |
} | |
@GetMapping(value = "/{id}", produces = "application/json") | |
@ResponseStatus(HttpStatus.OK) | |
public @ResponseBody Hero singleHero(@PathVariable Long id) { | |
LOG.debug("singleHero for id {}", id); | |
Hero hero = heroRepository.findOne(id); | |
if (hero == null) { | |
throw new ResourceNotFoundException("Hero not found for id: " + id); | |
} | |
return hero; | |
} | |
@PutMapping(value = "/{id}", consumes = "application/json", produces = "application/json") | |
@ResponseStatus(HttpStatus.NO_CONTENT) | |
public void updateHero(@PathVariable Long id, @RequestBody Hero hero) { | |
Hero currentHero = heroRepository.findOne(id); | |
if (currentHero == null) { | |
throw new ResourceNotFoundException("Hero is not found for id=" + id); | |
} | |
LOG.debug("updateHero: modified name from {} to {}", currentHero.getName(), hero.getName()); | |
currentHero.setName(hero.getName()); | |
//this.heroRepository.save(currentHero); | |
} | |
@DeleteMapping(value = "/{id}") | |
@ResponseStatus(HttpStatus.NO_CONTENT) | |
public void deleteHero(@PathVariable Long id) { | |
LOG.debug("delete >{}<", id); | |
try { | |
heroRepository.delete(id); | |
} catch (EmptyResultDataAccessException e1) { | |
throw new ResourceNotFoundException("Cannot delete Hero with id " + id + ". Not found", e1); | |
} | |
} | |
@GetMapping(value = "/search/name", produces = "application/json") | |
@ResponseStatus(HttpStatus.OK) | |
public Iterable<Hero> findByName(@RequestParam("contains") String name) { | |
LOG.debug("findByName >{}<", name); | |
return heroRepository.findByName(name); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment