Skip to content

Commit 2ff8f5d

Browse files
committed
7_11_rest_controller
1 parent 71f9257 commit 2ff8f5d

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed
Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,59 @@
11
package 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;
48
import ru.javawebinar.topjava.model.User;
59

10+
import java.net.URI;
611
import java.util.List;
712

8-
@Controller
13+
@RestController
14+
@RequestMapping(value = AdminRestController.REST_URL, produces = MediaType.APPLICATION_JSON_VALUE)
915
public 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
}
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
package 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.web.bind.annotation.*;
46
import ru.javawebinar.topjava.model.User;
57

68
import static ru.javawebinar.topjava.web.SecurityUtil.authUserId;
79

8-
@Controller
10+
@RestController
11+
@RequestMapping(value = ProfileRestController.REST_URL, produces = MediaType.APPLICATION_JSON_VALUE)
912
public class ProfileRestController extends AbstractUserController {
13+
static final String REST_URL = "/rest/profile";
1014

15+
@GetMapping
1116
public User get() {
1217
return super.get(authUserId());
1318
}
1419

20+
@DeleteMapping
21+
@ResponseStatus(HttpStatus.NO_CONTENT)
1522
public void delete() {
1623
super.delete(authUserId());
1724
}
1825

19-
public void update(User user) {
26+
@PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
27+
@ResponseStatus(HttpStatus.NO_CONTENT)
28+
public void update(@RequestBody User user) {
2029
super.update(user, authUserId());
2130
}
2231
}

0 commit comments

Comments
 (0)