Skip to content

Commit a6bb2e4

Browse files
committed
Added PUT method
1 parent 8c36bd5 commit a6bb2e4

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/main/java/com/kaluzny/web/BookController.java renamed to src/main/java/com/kaluzny/web/BookRestController.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,34 @@
77
import org.springframework.web.bind.annotation.*;
88

99
import javax.inject.Inject;
10+
import javax.persistence.EntityManager;
11+
import javax.persistence.PersistenceContext;
1012
import java.util.Collection;
1113

1214
@RestController
1315
@RequestMapping("/api/books")
14-
public class BookController {
16+
public class BookRestController {
1517

1618
private BookRepository repository;
1719

20+
@PersistenceContext
21+
private EntityManager entityManager;
22+
1823
@Inject
1924
public void setRepository(BookRepository repository) {
2025
this.repository = repository;
2126
}
2227

28+
@RequestMapping(
29+
method = RequestMethod.POST)
30+
public ResponseEntity<?> addBook(@RequestBody Book book) {
31+
return new ResponseEntity<>(repository.save(book), HttpStatus.CREATED);
32+
}
33+
2334
@RequestMapping(
2435
method = RequestMethod.GET)
2536
public ResponseEntity<Collection<Book>> getAllBooks() {
26-
return new ResponseEntity<>((Collection<Book>) repository.findAll(), HttpStatus.OK);
37+
return new ResponseEntity<>(repository.findAll(), HttpStatus.OK);
2738
}
2839

2940
@RequestMapping(
@@ -41,9 +52,16 @@ public ResponseEntity<Collection<Book>> findBookWithName(@RequestParam(value = "
4152
}
4253

4354
@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);
4765
}
4866

4967
@RequestMapping(
@@ -58,4 +76,4 @@ public void deleteBookWithId(@PathVariable Long id) {
5876
public void deleteAllBooks() {
5977
repository.deleteAll();
6078
}
61-
}
79+
}

0 commit comments

Comments
 (0)