11package ru .javawebinar .topjava .web .user ;
22
3- import org .springframework .stereotype .Controller ;
3+ import org .springframework .http .HttpStatus ;
4+ import org .springframework .http .MediaType ;
5+ import org .springframework .http .ResponseEntity ;
6+ import org .springframework .web .bind .annotation .*;
7+ import org .springframework .web .servlet .support .ServletUriComponentsBuilder ;
48import ru .javawebinar .topjava .model .User ;
59
10+ import java .net .URI ;
611import java .util .List ;
712
8- @ Controller
13+ @ RestController
14+ @ RequestMapping (value = AdminRestController .REST_URL , produces = MediaType .APPLICATION_JSON_VALUE )
915public class AdminRestController extends AbstractUserController {
1016
17+ static final String REST_URL = "/rest/admin/users" ;
18+
1119 @ Override
20+ @ GetMapping
1221 public List <User > getAll () {
1322 return super .getAll ();
1423 }
1524
1625 @ Override
17- public User get (int id ) {
26+ @ GetMapping ("/{id}" )
27+ public User get (@ PathVariable int id ) {
1828 return super .get (id );
1929 }
2030
21- @ Override
22- public User create (User user ) {
23- return super .create (user );
31+ @ PostMapping (consumes = MediaType .APPLICATION_JSON_VALUE )
32+ public ResponseEntity <User > createWithLocation (@ RequestBody User user ) {
33+ User created = super .create (user );
34+ URI uriOfNewResource = ServletUriComponentsBuilder .fromCurrentContextPath ()
35+ .path (REST_URL + "/{id}" )
36+ .buildAndExpand (created .getId ()).toUri ();
37+ return ResponseEntity .created (uriOfNewResource ).body (created );
2438 }
2539
2640 @ Override
27- public void delete (int id ) {
41+ @ DeleteMapping ("/{id}" )
42+ @ ResponseStatus (HttpStatus .NO_CONTENT )
43+ public void delete (@ PathVariable int id ) {
2844 super .delete (id );
2945 }
3046
3147 @ Override
32- public void update (User user , int id ) {
48+ @ PutMapping (value = "/{id}" , consumes = MediaType .APPLICATION_JSON_VALUE )
49+ @ ResponseStatus (HttpStatus .NO_CONTENT )
50+ public void update (@ RequestBody User user , @ PathVariable int id ) {
3351 super .update (user , id );
3452 }
3553
3654 @ Override
37- public User getByMail (String email ) {
55+ @ GetMapping ("/by-email" )
56+ public User getByMail (@ RequestParam String email ) {
3857 return super .getByMail (email );
3958 }
4059}
0 commit comments