Skip to content

Commit ab4bae7

Browse files
committed
upgrade method put
1 parent 00e9340 commit ab4bae7

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

src/main/java/com/kaluzny/web/BookRestController.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ public Collection<Book> findBookByName(@RequestParam(value = "name") String name
6060
@ResponseStatus(HttpStatus.OK)
6161
public Book refreshBook(@PathVariable("id") long id, @RequestBody Book book) {
6262
log.info("refreshBook() - start: id = {}, book = {}", id, book);
63-
Book updatedBook = repository.save(putBook(id, book));
63+
Book updatedBook = repository.findById(id)
64+
.map(entity -> {
65+
entity.setName(book.getName());
66+
entity.setDescription(book.getDescription());
67+
entity.setTags(book.getTags());
68+
return repository.save(entity);
69+
})
70+
.orElseThrow(() -> new EntityNotFoundException("Book with id = Not found"));
6471
log.info("refreshBook() - end: updatedBook = {}", updatedBook);
6572
return updatedBook;
6673
}
@@ -80,15 +87,4 @@ public void removeAllBooks() {
8087
repository.deleteAll();
8188
log.info("removeAllBooks() - end");
8289
}
83-
84-
private Book putBook(long id, Book existingBook) {
85-
log.info("putBook() - start: id = {} existingBook = {}", id, existingBook);
86-
Book putBook = repository.findById(id)
87-
.orElseThrow(() -> new EntityNotFoundException("Entity with id = Not found"));
88-
putBook.setName(existingBook.getName());
89-
putBook.setDescription(existingBook.getDescription());
90-
putBook.setTags(existingBook.getTags());
91-
log.info("putBook() - end: putBook = {}", putBook);
92-
return putBook;
93-
}
9490
}

src/main/resources/Request.http

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# * 'mptr' and 'fptr' create a POST request to submit a form with a text or file field (multipart/form-data);
88

99
### Send POST request with json body
10-
POST http://localhost:8091/api/books
10+
POST http://localhost:8080/api/books
1111
Accept: application/json
1212
Authorization: Basic dXNlcjp1c2Vy
1313
Content-Type: application/json

src/main/resources/application.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ spring:
1616
# JPA properties
1717
jpa:
1818
hibernate:
19-
ddl-auto: none # When you launch the application for the first time - switch "none" at "create"
19+
ddl-auto: update # When you launch the application for the first time - switch "none" at "create"
2020
show-sql: true
2121
database: postgresql
2222
database-platform: org.hibernate.dialect.PostgreSQLDialect
2323
open-in-view: false
24-
generate-ddl: false
24+
generate-ddl: true
2525
# Logger configuration
2626
logging:
2727
pattern:
2828
console: "%d %-5level %logger : %msg%n"
2929
level:
30-
org.springframework: INFO
31-
org.hibernate: DEBUG
30+
org.springframework: info
31+
org.hibernate: debug
3232
# Server configuration
3333
server:
34-
port: 8081 #set your port
34+
port: 8080 #set your port

0 commit comments

Comments
 (0)