Created
December 18, 2018 13:47
-
-
Save must1/b9e00b372863b3b68fef2b9754357b8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bookrental.service.book.rentals; | |
import bookrental.model.account.User; | |
import bookrental.model.book.Book; | |
import bookrental.model.book.BookRentals; | |
import bookrental.repository.account.UserRepository; | |
import bookrental.repository.book.BookRepository; | |
import bookrental.repository.book.BookRentalsRepository; | |
import flexjson.JSONSerializer; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.http.HttpHeaders; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.stereotype.Service; | |
import java.util.ArrayList; | |
import java.util.List; | |
@Service | |
public class BookRentalService { | |
private final UserRepository userRepository; | |
private final BookRepository bookRepository; | |
private final BookRentalsRepository bookRentalsRepository; | |
@Autowired | |
public BookRentalService(BookRepository bookRepository, BookRentalsRepository bookRentalsRepository, UserRepository userRepository) { | |
this.bookRepository = bookRepository; | |
this.bookRentalsRepository = bookRentalsRepository; | |
this.userRepository = userRepository; | |
} | |
.... | |
public ResponseEntity<String> findAllRentals() { | |
List<BookRentals> rentedBooks = new ArrayList<>(); | |
bookRentalsRepository.findAll().forEach(rentedBooks::add); | |
HttpHeaders headers = new HttpHeaders(); | |
headers.add("Content-Type", "application/json; charset=utf-8"); | |
return new ResponseEntity<>(new JSONSerializer().exclude("class").exclude("available").serialize(rentedBooks), headers,HttpStatus.OK); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bookrental.controller.book.rentals; | |
import bookrental.service.book.rentals.BookRentalService; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.PostMapping; | |
import org.springframework.web.bind.annotation.RequestParam; | |
import org.springframework.web.bind.annotation.RestController; | |
@RestController | |
public class BookRentalController { | |
private final BookRentalService bookRentalService; | |
@Autowired | |
public BookRentalController(BookRentalService bookRentalService) { | |
this.bookRentalService = bookRentalService; | |
} | |
... | |
@GetMapping(value = "/books/rentals", produces ="application/json") | |
public ResponseEntity<String> findAllRentals() { | |
return bookRentalService.findAllRentals(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"book": { | |
"author": "Henryk Sienkiewicz", | |
"available": false, | |
"category": "powieść historyczna", | |
"class": "bookrental.model.book.Book", | |
"id": 1, | |
"title": "Krzyżacy" | |
}, | |
"id": 1, | |
"user": { | |
"class": "bookrental.model.account.User", | |
"id": 2, | |
"name": "piotri", | |
"password": "123" | |
} | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment