Skip to content

Commit

Permalink
Refactor structure from layer to feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaynegi45 committed Sep 16, 2024
1 parent c04ddee commit 997c2b7
Show file tree
Hide file tree
Showing 25 changed files with 147 additions and 159 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.libraryman_api.entity;
package com.libraryman_api.book;

import jakarta.persistence.*;


@Entity
public class Books {
public class Book {
@Id

@GeneratedValue(strategy = GenerationType.SEQUENCE,
Expand Down Expand Up @@ -33,10 +33,10 @@ public class Books {
@Column(name = "copies_available", nullable = false)
private int copiesAvailable;

public Books() {
public Book() {
}

public Books(String title, String author, String isbn, String publisher, int publishedYear, String genre, int copiesAvailable) {
public Book(String title, String author, String isbn, String publisher, int publishedYear, String genre, int copiesAvailable) {
this.title = title;
this.author = author;
this.isbn = isbn;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.libraryman_api.controller;
package com.libraryman_api.book;

import com.libraryman_api.entity.Books;
import com.libraryman_api.exception.ResourceNotFoundException;
import com.libraryman_api.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -25,22 +23,22 @@ public class BookController {
/**
* Retrieves a list of all books in the library.
*
* @return a list of {@link Books} objects representing all the books in the library.
* @return a list of {@link Book} objects representing all the books in the library.
*/
@GetMapping
public List<Books> getAllBooks() {
public List<Book> getAllBooks() {
return bookService.getAllBooks();
}

/**
* Retrieves a book by its ID.
*
* @param id the ID of the book to retrieve.
* @return a {@link ResponseEntity} containing the {@link Books} object, if found.
* @return a {@link ResponseEntity} containing the {@link Book} object, if found.
* @throws ResourceNotFoundException if the book with the specified ID is not found.
*/
@GetMapping("/{id}")
public ResponseEntity<Books> getBookById(@PathVariable int id) {
public ResponseEntity<Book> getBookById(@PathVariable int id) {
return bookService.getBookById(id)
.map(ResponseEntity::ok)
.orElseThrow(() -> new ResourceNotFoundException("Book not found"));
Expand All @@ -49,23 +47,23 @@ public ResponseEntity<Books> getBookById(@PathVariable int id) {
/**
* Adds a new book to the library.
*
* @param book the {@link Books} object representing the new book to add.
* @return the added {@link Books} object.
* @param book the {@link Book} object representing the new book to add.
* @return the added {@link Book} object.
*/
@PostMapping
public Books addBook(@RequestBody Books book) {
public Book addBook(@RequestBody Book book) {
return bookService.addBook(book);
}

/**
* Updates an existing book in the library.
*
* @param id the ID of the book to update.
* @param bookDetails the {@link Books} object containing the updated book details.
* @return the updated {@link Books} object.
* @param bookDetails the {@link Book} object containing the updated book details.
* @return the updated {@link Book} object.
*/
@PutMapping("/{id}")
public Books updateBook(@PathVariable int id, @RequestBody Books bookDetails) {
public Book updateBook(@PathVariable int id, @RequestBody Book bookDetails) {
return bookService.updateBook(id, bookDetails);
}

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/libraryman_api/book/BookRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.libraryman_api.book;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface BookRepository extends JpaRepository<Book, Integer> {
}



Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.libraryman_api.service;
package com.libraryman_api.book;

import com.libraryman_api.entity.Books;
import com.libraryman_api.exception.ResourceNotFoundException;
import com.libraryman_api.repository.BookRepository;
import org.springframework.stereotype.Service;

import java.util.List;
Expand Down Expand Up @@ -42,7 +40,7 @@ public BookService(BookRepository bookRepository) {
*
* @return a list of all books
*/
public List<Books> getAllBooks() {
public List<Book> getAllBooks() {
return bookRepository.findAll();
}

Expand All @@ -52,7 +50,7 @@ public List<Books> getAllBooks() {
* @param bookId the ID of the book to retrieve
* @return an {@code Optional} containing the found book, or {@code Optional.empty()} if no book was found
*/
public Optional<Books> getBookById(int bookId) {
public Optional<Book> getBookById(int bookId) {
return bookRepository.findById(bookId);
}

Expand All @@ -62,7 +60,7 @@ public Optional<Books> getBookById(int bookId) {
* @param book the book to be added
* @return the saved book
*/
public Books addBook(Books book) {
public Book addBook(Book book) {
return bookRepository.save(book);
}

Expand All @@ -74,8 +72,8 @@ public Books addBook(Books book) {
* @return the updated book
* @throws ResourceNotFoundException if the book with the specified ID is not found
*/
public Books updateBook(int bookId, Books bookDetails) {
Books book = bookRepository.findById(bookId)
public Book updateBook(int bookId, Book bookDetails) {
Book book = bookRepository.findById(bookId)
.orElseThrow(() -> new ResourceNotFoundException("Book not found"));
book.setTitle(bookDetails.getTitle());
book.setAuthor(bookDetails.getAuthor());
Expand All @@ -94,7 +92,7 @@ public Books updateBook(int bookId, Books bookDetails) {
* @throws ResourceNotFoundException if the book with the specified ID is not found
*/
public void deleteBook(int bookId) {
Books book = bookRepository.findById(bookId)
Book book = bookRepository.findById(bookId)
.orElseThrow(() -> new ResourceNotFoundException("Book not found"));
bookRepository.delete(book);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.libraryman_api.controller;
package com.libraryman_api.borrowing;

import com.libraryman_api.entity.Borrowings;
import com.libraryman_api.exception.ResourceNotFoundException;
import com.libraryman_api.service.BorrowingService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.libraryman_api.repository;
package com.libraryman_api.borrowing;

import com.libraryman_api.entity.Borrowings;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.libraryman_api.service;
package com.libraryman_api.borrowing;

import com.libraryman_api.entity.Books;
import com.libraryman_api.entity.Borrowings;
import com.libraryman_api.entity.Fines;
import com.libraryman_api.book.BookService;
import com.libraryman_api.book.Book;
import com.libraryman_api.fine.Fines;
import com.libraryman_api.exception.ResourceNotFoundException;
import com.libraryman_api.repository.BorrowingRepository;
import com.libraryman_api.repository.FineRepository;
import com.libraryman_api.fine.FineRepository;
import com.libraryman_api.notification.NotificationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
Expand Down Expand Up @@ -88,10 +88,10 @@ public Optional<Borrowings> getBorrowingById(int borrowingId) {
*/
@Transactional
public synchronized Borrowings borrowBook(Borrowings borrowing) {
Optional<Books> book = bookService.getBookById(borrowing.getBook().getBookId());
Optional<Book> book = bookService.getBookById(borrowing.getBook().getBookId());

if (book.isPresent()) {
Books bookEntity = book.get();
Book bookEntity = book.get();

if (bookEntity.getCopiesAvailable() > 0) {
updateBookCopies(borrowing.getBook().getBookId(), "REMOVE", 1);
Expand Down Expand Up @@ -198,10 +198,10 @@ public String payFine(int borrowingId) {
* @throws ResourceNotFoundException if the book is not found or if there are not enough copies to remove
*/
public void updateBookCopies(int bookId, String operation, int numberOfCopies) {
Optional<Books> book = bookService.getBookById(bookId);
Optional<Book> book = bookService.getBookById(bookId);

if (book.isPresent()) {
Books bookEntity = book.get();
Book bookEntity = book.get();
if (operation.equals("ADD")) {
bookEntity.setCopiesAvailable(bookEntity.getCopiesAvailable() + numberOfCopies);
} else if (operation.equals("REMOVE")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.libraryman_api.entity;
package com.libraryman_api.borrowing;

import com.libraryman_api.book.Book;
import com.libraryman_api.fine.Fines;
import com.libraryman_api.member.Members;
import jakarta.persistence.*;

import java.util.Date;
Expand All @@ -17,7 +20,7 @@ public class Borrowings {

@ManyToOne
@JoinColumn(name = "book_id", nullable = false)
private Books book;
private Book book;

@OneToOne
@JoinColumn(name = "fine_id")
Expand All @@ -40,7 +43,7 @@ public class Borrowings {
public Borrowings() {
}

public Borrowings(Books book, Members member, Date borrowDate, Date dueDate, Date returnDate) {
public Borrowings(Book book, Members member, Date borrowDate, Date dueDate, Date returnDate) {
this.book = book;
this.member = member;
this.borrowDate = borrowDate;
Expand All @@ -60,11 +63,11 @@ public int getBorrowingId() {
return borrowingId;
}

public Books getBook() {
public Book getBook() {
return book;
}

public void setBook(Books book) {
public void setBook(Book book) {
this.book = book;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/libraryman_api/email/EmailSender.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.libraryman_api.email;

import com.libraryman_api.entity.Notifications;
import com.libraryman_api.notification.Notifications;

/**
* Interface representing an email sending service for the Library Management System.
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/libraryman_api/email/EmailService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.libraryman_api.email;

import com.libraryman_api.entity.NotificationStatus;
import com.libraryman_api.entity.Notifications;
import com.libraryman_api.repository.NotificationRepository;
import com.libraryman_api.notification.NotificationStatus;
import com.libraryman_api.notification.Notifications;
import com.libraryman_api.notification.NotificationRepository;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.slf4j.Logger;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.libraryman_api.controller;
package com.libraryman_api.exception;

import com.libraryman_api.exception.ErrorDetails;
import com.libraryman_api.exception.ResourceNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.libraryman_api.repository;
package com.libraryman_api.fine;

import com.libraryman_api.entity.Fines;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.libraryman_api.entity;
package com.libraryman_api.fine;

import jakarta.persistence.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.libraryman_api.controller;
package com.libraryman_api.member;

import com.libraryman_api.entity.Members;
import com.libraryman_api.exception.ResourceNotFoundException;
import com.libraryman_api.service.MemberService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.libraryman_api.repository;
package com.libraryman_api.member;

import com.libraryman_api.entity.Borrowings;
import com.libraryman_api.entity.Members;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.libraryman_api.service;
package com.libraryman_api.member;

import com.libraryman_api.entity.Members;
import com.libraryman_api.exception.ResourceNotFoundException;
import com.libraryman_api.repository.MemberRepository;
import com.libraryman_api.notification.NotificationService;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.libraryman_api.entity;
package com.libraryman_api.member;

import jakarta.persistence.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.libraryman_api.entity;
package com.libraryman_api.member;

public enum Role {
ADMIN, LIBRARIAN, USER
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.libraryman_api.repository;
package com.libraryman_api.notification;

import com.libraryman_api.entity.Notifications;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand Down
Loading

0 comments on commit 997c2b7

Please sign in to comment.