7
7
import org .springframework .web .bind .annotation .*;
8
8
9
9
import javax .inject .Inject ;
10
+ import javax .persistence .EntityManager ;
11
+ import javax .persistence .PersistenceContext ;
10
12
import java .util .Collection ;
11
13
12
14
@ RestController
13
15
@ RequestMapping ("/api/books" )
14
- public class BookController {
16
+ public class BookRestController {
15
17
16
18
private BookRepository repository ;
17
19
20
+ @ PersistenceContext
21
+ private EntityManager entityManager ;
22
+
18
23
@ Inject
19
24
public void setRepository (BookRepository repository ) {
20
25
this .repository = repository ;
21
26
}
22
27
28
+ @ RequestMapping (
29
+ method = RequestMethod .POST )
30
+ public ResponseEntity <?> addBook (@ RequestBody Book book ) {
31
+ return new ResponseEntity <>(repository .save (book ), HttpStatus .CREATED );
32
+ }
33
+
23
34
@ RequestMapping (
24
35
method = RequestMethod .GET )
25
36
public ResponseEntity <Collection <Book >> getAllBooks () {
26
- return new ResponseEntity <>(( Collection < Book >) repository .findAll (), HttpStatus .OK );
37
+ return new ResponseEntity <>(repository .findAll (), HttpStatus .OK );
27
38
}
28
39
29
40
@ RequestMapping (
@@ -41,9 +52,16 @@ public ResponseEntity<Collection<Book>> findBookWithName(@RequestParam(value = "
41
52
}
42
53
43
54
@ RequestMapping (
44
- method = RequestMethod .POST )
45
- public ResponseEntity <?> addBook (@ RequestBody Book book ) {
46
- return new ResponseEntity <>(repository .save (book ), HttpStatus .CREATED );
55
+ value = "/{id}" ,
56
+ method = RequestMethod .PUT )
57
+ public ResponseEntity <Book > updateUserFromDB (@ PathVariable ("id" ) long id , @ RequestBody Book book ) {
58
+
59
+ Book currentBook = repository .findOne (id );
60
+ currentBook .setName (book .getName ());
61
+ currentBook .setDescription (book .getDescription ());
62
+ currentBook .setTags (book .getTags ());
63
+
64
+ return new ResponseEntity <>(repository .save (currentBook ), HttpStatus .OK );
47
65
}
48
66
49
67
@ RequestMapping (
@@ -58,4 +76,4 @@ public void deleteBookWithId(@PathVariable Long id) {
58
76
public void deleteAllBooks () {
59
77
repository .deleteAll ();
60
78
}
61
- }
79
+ }
0 commit comments