https://github.com/vishal-raj-1/library
Create Book And Author Management System
https://github.com/vishal-raj-1/library
Last synced: 2 days ago
JSON representation
Create Book And Author Management System
- Host: GitHub
- URL: https://github.com/vishal-raj-1/library
- Owner: Vishal-raj-1
- Created: 2023-11-28T17:05:00.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-13T09:52:18.000Z (almost 2 years ago)
- Last Synced: 2025-01-14T02:13:16.939Z (9 months ago)
- Language: Java
- Size: 83 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Book and Author Management System
### Task to performed
0) Created a db with following structure in mongodb:
1) `Book(id, copiesAvailable, authorId, genre)` & `Author(id, name, address(house no, city,state)`
2) Create a GET api to get all the books from db
3) Create a GET api to get all the books from db whose genre matches the query param input
4) Create a GET api to get all the books from db whose genre matches the query param input and copiesAvailable is more than input provided in the path param.
5) Create a POST api to save book detail in db (including validations of not blank, not null etc)
6) Create a POST api to save author detail in db (including validations of not blank, not null etc)
7) Create a GET api to fetch all books whose author is in one of the input provided in the query params
8) Create a GET api to fetch all authors whose name matches the regular expression provided in the input query params.### Model
Let's create Book Model with `id` as primary key. `Book(id, copiesAvailable, authorId, genre)`
```java
@Document
public class Book {
@Id
private String id;
private String genre;
private String authorId;
private int copiesAvailable;
}
```Now for creating the Author Model, we need `Address` (house no, city, state), so let's create one address class. **NOTE**: It's not a collection, it is used just for author details.
```java
public class Address {
private String houseNo;
private String city;
private String state;
}
```Now, we can create Author Model using Address.
```java
@Document
public class Author {
@Id
private String id;
private String name;
private Address address;
private List bookList;
}
```### Repository
Create `BookRepository` and `AuthorRepository` which extends the `MongoRepository`. Also add additional methods, as per the needs.
BookRepository
```java
public interface BookRepository extends MongoRepository {@Query("{ genre : ?0 }")
List findByGenre(String genre);@Query("{ genre : ?0 , copiesAvailable : { $gt : ?1 } }")
List findByGenreAndCopiesAvailableGreaterThan(String genre, int copiesAvailable);List findByAuthorIdIn(List Author);
}
```AuthorRepository
```java
public interface AuthorRepository extends MongoRepository {@Query("{ name : ?0 }")
Author findByName(String authorName);@Query("{ name: { $regex : ?0 } }")
List findByNameRegex(String nameRegex);
}
```### Services
Create `BookService` and `AuthorService`.
BookService
```java
@Service
public class BookService {@Autowired
private BookRepository bookRepository;
@Autowired
private AuthorRepository authorRepository;
public List getAllBooks() {
return bookRepository.findAll();
}public List getBooksByGenre(String genre) {
return bookRepository.findByGenre(genre);
}public List getBooksByGenreAndCopiesAvailable(String genre, int copiesAvailable) {
return bookRepository.findByGenreAndCopiesAvailableGreaterThan(genre, copiesAvailable);
}public Book getBookById(String id) {
return bookRepository.findById(id).orElse(null);
}public Book saveBook(Book book, String authorName) {
if( book!=null && book.getCopiesAvailable()>=0 && !authorName.isEmpty() && book.getGenre()!=null ) {
Author author = authorRepository.findByName(authorName);
if(author == null){
author = new Author();
author.setName(authorName);String authorId = new ObjectId().toHexString();
author.setId(authorId);authorRepository.save(author);
book.setAuthorId(authorId);
} else {
book.setAuthorId(author.getId());
}Book savedBook = bookRepository.save(book);
author.getBookList().add(savedBook.getId());
authorRepository.save(author);return savedBook;
} else{
throw new IllegalArgumentException("Invalid/Incomplete details given");
}
}public List getBooksByAuthorNames(List authors) {
List books = new ArrayList<>();for (String authorName : authors) {
Author author = authorRepository.findByName(authorName);
List bookIds = author.getBookList();for(String bookId: bookIds) {
Optional book = bookRepository.findById(bookId);book.ifPresent(books::add);
}
}return books;
}
}
```AuthorService
```java
@Service
public class AuthorService {@Autowired
private AuthorRepository authorRepository;public List getAllAuthors() {
return authorRepository.findAll();
}public Author getAuthorById(String id) {
return authorRepository.findById(id).orElse(null);
}public Author saveAuthor(Author author) {
return authorRepository.save(author);
}public List getAuthorsByNameRegex(String nameRegex) {
return authorRepository.findByNameRegex(nameRegex);
}
}
```
### ControllersCreate `BookControllers` and `AuthorController`
BookController
```java
@RestController
@RequestMapping("/api/books")
public class BookController {@Autowired
private BookService bookService;@GetMapping
public ResponseEntity> getAllBooks() {
List books = bookService.getAllBooks();
return new ResponseEntity<>(books, HttpStatus.OK);
}@GetMapping("/{bookId}")
public ResponseEntity getBookById(@PathVariable String bookId){
Book book = bookService.getBookById(bookId);
return new ResponseEntity<>(book, HttpStatus.OK);
}@GetMapping("/byGenre")
public ResponseEntity> getBooksByGenre(@RequestParam String genre) {
List books = bookService.getBooksByGenre(genre);
return new ResponseEntity<>(books, HttpStatus.OK);
}@GetMapping("/byGenreAndCopiesAvailable")
public ResponseEntity> getBooksByGenreAndCopiesAvailable(
@RequestParam String genre,
@RequestParam int copiesAvailable) {
List books = bookService.getBooksByGenreAndCopiesAvailable(genre, copiesAvailable);
return new ResponseEntity<>(books, HttpStatus.OK);
}@PostMapping
public ResponseEntity saveBook(@RequestBody BookAuthorDTO request) {
String authorName = request.getAuthorName();
Book book = request.getBook();Book savedBook = bookService.saveBook(book, authorName);
return new ResponseEntity<>(savedBook, HttpStatus.CREATED);
}@GetMapping("/byAuthors")
public ResponseEntity> getBooksByAuthorIds(@RequestBody List authors) {
List books = bookService.getBooksByAuthorNames(authors);
return new ResponseEntity<>(books, HttpStatus.OK);
}
}
```AuthorController
```java
@RestController
@RequestMapping("/api/authors")
public class AuthorController {@Autowired
private AuthorService authorService;@GetMapping("/{authorId}")
public ResponseEntity getAuthorById(@PathVariable String authorId) {
Author authors = authorService.getAuthorById(authorId);
return new ResponseEntity<>(authors, HttpStatus.OK);
}@GetMapping
public ResponseEntity> getAllAuthors() {
List authors = authorService.getAllAuthors();
return new ResponseEntity<>(authors, HttpStatus.OK);
}@GetMapping("/byNameRegex")
public ResponseEntity> getAllAuthorsByNameRegex(@RequestParam String nameRegex) {
List authors = authorService.getAuthorsByNameRegex(nameRegex);
return new ResponseEntity<>(authors, HttpStatus.OK);
}@PostMapping
public ResponseEntity saveAuthor(@RequestBody Author author) {
Author savedAuthor = authorService.saveAuthor(author);
return new ResponseEntity<>(savedAuthor, HttpStatus.CREATED);
}
}
```