Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save namila007/5e7899536501f58fb469e95fadcf2861 to your computer and use it in GitHub Desktop.
Save namila007/5e7899536501f58fb469e95fadcf2861 to your computer and use it in GitHub Desktop.
@Validated
@RestController
@RequestMapping("/api/v1/author")
public class AuthorController
{
@Autowired
private AuthorService authorMainService;
@GetMapping(value = "/{id}")
public ResponseWrapper<Author> getAuthorById(
@Valid @Pattern(regexp = REGEX_FOR_NUMBERS, message = MESSAGE_FOR_REGEX_NUMBER_MISMATCH) @PathVariable(value = "id") String id )
{
return new ResponseWrapper<>( authorMainService.getById( Integer.parseInt( id ) ), HttpStatus.OK );
}
@GetMapping()
public ResponseWrapper<Page<Author>> getAuthorAll( Pageable pageable )
{
return new ResponseWrapper<>( authorMainService.getAll( pageable ), HttpStatus.OK );
}
@PostMapping
public ResponseWrapper<Author> createAuthor( @Valid @RequestBody Author author )
{
return new ResponseWrapper<>( authorMainService.add( author ), HttpStatus.OK );
}
@DeleteMapping(value = "/{id}")
public ResponseWrapper<Author> deleteAuthor(
@Valid @Pattern(regexp = REGEX_FOR_NUMBERS, message = MESSAGE_FOR_REGEX_NUMBER_MISMATCH) @PathVariable(value = "id") String id )
{
return new ResponseWrapper<>( authorMainService.deleteById( Integer.parseInt( id ) ), HttpStatus.OK );
}
@PatchMapping(value = "/{id}")
public ResponseWrapper<Author> UpdateAuthor( @Valid @RequestBody Author author,
@Valid @Pattern(regexp = REGEX_FOR_NUMBERS, message = MESSAGE_FOR_REGEX_NUMBER_MISMATCH) @PathVariable(value = "id") String id )
{
return new ResponseWrapper<>( authorMainService.update( author, Integer.parseInt( id ) ), HttpStatus.OK );
}
@GetMapping(value = "/{id}/books")
public ResponseWrapper<List<Book>> getAuthorBooksById(
@Valid @Pattern(regexp = REGEX_FOR_NUMBERS, message = MESSAGE_FOR_REGEX_NUMBER_MISMATCH) @PathVariable(value = "id") String id )
{
return new ResponseWrapper<>( authorMainService.getBooksById( Integer.parseInt( id ) ), HttpStatus.OK );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment