Skip to content

Library Management System using Spring Boot. The system facilitates librarians in efficiently managing books, patrons, and borrowing records.

Notifications You must be signed in to change notification settings

AhmedLotfy02/Library-Management-System

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Library Management System

Spring Boot MySQL Hibernate Maven

Table of Contents

Overview

The Library Management System is a Spring Boot application designed to manage library operations, including managing books, patrons, and borrowing records. The system includes secure API endpoints for various CRUD operations.

Prerequisites

  • Java 20 or later
  • Maven 3.6+
  • Spring Boot 3.3.2
  • MySQL
  • Postman or any API testing tool (optional)

Getting Started

1. Clone the Repository

git clone https://github.com/AhmedLotfy02/LibraryManagementSystem.git
cd LibraryManagementSystem

2. Configure the Database

Update the application.properties file with your database configuration:

spring.datasource.url=jdbc:mysql://localhost:3306/library_db
spring.datasource.username=your_db_username
spring.datasource.password=your_db_password
spring.jpa.hibernate.ddl-auto=update

3. Run the Application

Use Maven to build and run the application:

mvn clean install
mvn spring-boot:run

Authentication

The application uses JWT for authentication. To access secured endpoints, you need to include the JWT token in the Authorization header as a Bearer token.

Example:

Authorization: Bearer your_jwt_token

Obtaining a Token

  1. Use the /authenticate endpoint to get a JWT token by providing valid user credentials.
  2. Include the token in the Authorization header for subsequent API requests.

4. Access the API

The application will start on http://localhost:8081.

There is a postman collection contains all api documentations and requests in the repo "Library Management System APIs.postman_collection.json"

API Endpoints

Authentication

Login

Endpoint: /authenticate

Method: POST

Description: Authenticates a user and returns a JWT token.

Note: for this demo use username = "admin" and password = "password" to access all APIs.

Request Body:

{
"username": "admin",
"password": "password"
}

Response:

{
"token": "jwt_token"
}

Books

Get All Books

Endpoint: /api/books

Method: GET

Description: Retrieves a list of all books.

Response:

[
    {
        "id": 1,
        "title": "Book Title",
        "author": "Book Author",
        "publicationYear": 2020,
        "isbn": "1234567890"
    }
]

Get Book by ID

Endpoint: /api/books/{id}

Method: GET

Description: Retrieves a book by its ID.

Response:

{
    "id": 1,
    "title": "Book Title",
    "author": "Book Author",
    "publicationYear": 2020,
    "isbn": "1234567890"
}

Create Book

Endpoint: /api/books

Method: POST

Description: Creates a new book.

Request Body:

{
    "title": "New Book",
    "author": "New Author",
    "publicationYear": 2021,
    "isbn": "0987654321"
}

Response:

{
    "id": 2,
    "title": "New Book",
    "author": "New Author",
    "publicationYear": 2021,
    "isbn": "0987654321"
}

Update Book

Endpoint: /api/books/{id}

Method: PUT

Description: Updates an existing book.

Request Body:

{
    "title": "Updated Book",
    "author": "Updated Author",
    "publicationYear": 2022,
    "isbn": "1122334455"
}

Response:

{
    "id": 1,
    "title": "Updated Book",
    "author": "Updated Author",
    "publicationYear": 2022,
    "isbn": "1122334455"
}

Delete Book

Endpoint: /api/books/{id}

Method: DELETE

Description: Deletes a book by its ID.

Response:

HTTP Status 204 (No Content)

Patrons

Get All Patrons

Endpoint: /api/patrons

Method: GET

Description: Retrieves a list of all patrons.

Response:

[
    {
        "id": 1,
        "name": "John Doe",
        "email": "[email protected]"
    }
]

Get Patron by ID

Endpoint: /api/patrons/{id}

Method: GET

Description: Retrieves a patron by its ID.

Response:

{
    "id": 1,
    "name": "John Doe",
    "email": "[email protected]"
}

Create Patron

Endpoint: /api/patrons

Method: POST

Description: Creates a new patron.

Request Body:

{
    "name": "Jane Doe",
    "email": "[email protected]"
}

Response:

{
    "id": 2,
    "name": "Jane Doe",
    "email": "[email protected]"
}

Update Patron

Endpoint: /api/patrons/{id}

Method: PUT

Description: Updates an existing patron.

Request Body:

{
    "name": "John Smith",
    "email": "[email protected]"
}

Response:

{
    "id": 1,
    "name": "John Smith",
    "email": "[email protected]"
}

Delete Patron

Endpoint: /api/patrons/{id}

Method: DELETE

Description: Deletes a patron by its ID.

Response:

HTTP Status 204 (No Content)

Borrowing Records

Borrow Book

Endpoint: /api/borrow/{bookId}/patron/{patronId}

Method: POST

Description: Borrows a book for a patron.

Response:

{
    "id": 1,
    "bookId": 1,
    "patronId": 1,
    "borrowDate": "2023-01-01",
    "returnDate": null
}

Return Book

Endpoint: /api/return/{bookId}/patron/{patronId}

Method: PUT

Description: Returns a borrowed book.

Response:

{
    "id": 1,
    "bookId": 1,
    "patronId": 1,
    "borrowDate": "2023-01-01",
    "returnDate": "2023-02-01"
}

Database Schema

The Library Management System database schema includes the following entities and relationships:

schema

  • Book: Represents the books in the library with attributes like id, title, author, publicationYear, and isbn.
  • BorrowingRecord: Represents the records of books borrowed by patrons with attributes like id, borrowDate, returnDate, patronId, and bookId.
  • Patron: Represents the library patrons with attributes like id, name, email, and phone.

The relationships between these entities are as follows:

  • A Book can have multiple BorrowingRecord entries (One-to-Many relationship).
  • A BorrowingRecord is associated with one Book (Many-to-One relationship).
  • A Patron can have multiple BorrowingRecord entries (One-to-Many relationship).
  • A BorrowingRecord is associated with one Patron (Many-to-One relationship).

Caching

The application uses Spring Cache to improve performance by caching frequently accessed data.

Configuration

Enable caching in your main application class:

@SpringBootApplication
@EnableCaching
public class LibraryManagementSystemApplication {
public static void main(String[] args) {
  SpringApplication.run(LibraryManagementSystemApplication.class, args);
}
}

Usage

Annotate the service methods you want to cache:

@Service
public class BookService {

@Cacheable("books") public List getAllBooks() { // method implementation }

@Cacheable(value = "books", key = "#id") public Book getBookById(Long id) { // method implementation } }

AOP (Aspect-Oriented Programming)

The application uses AOP for logging and performance monitoring.

Configuration

Enable AOP in your main application class:

@SpringBootApplication
@EnableAspectJAutoProxy
public class LibraryManagementSystemApplication {
public static void main(String[] args) {
  SpringApplication.run(LibraryManagementSystemApplication.class, args);
}
}

Usage

Create an aspect for logging:

@Aspect
@Component
public class LoggingAspect {

@Before("execution(* com.LibraryManagementSystem.demo.service..(..))") public void logBefore(JoinPoint joinPoint) { System.out.println("Executing method: " + joinPoint.getSignature().getName()); }

@After("execution(* com.LibraryManagementSystem.demo.service..(..))") public void logAfter(JoinPoint joinPoint) { System.out.println("Completed method: " + joinPoint.getSignature().getName()); }

@Around("execution(* com.LibraryManagementSystem.demo.service..(..))") public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable { long start = System.currentTimeMillis(); Object result = joinPoint.proceed(); long elapsedTime = System.currentTimeMillis() - start; System.out.println("Method " + joinPoint.getSignature().getName() + " executed in " + elapsedTime + " ms"); return result; } }

Running Tests

The project includes unit tests for the controllers, services, and utilities. These tests ensure that the application logic works correctly.

Controller Tests

These tests validate the functionality of the API endpoints:

  • shouldReturnBookById: Tests the retrieval of a book by its ID.
  • shouldReturnAllBooks: Tests the retrieval of all books.
  • shouldAddBook: Tests the addition of a new book.
  • shouldUpdateBook: Tests the update of an existing book.
  • shouldDeleteBook: Tests the deletion of a book by its ID.

Service Tests

These tests validate the business logic of the services:

  • getAllBooks: Tests the retrieval of all books.
  • getBookById: Tests the retrieval of a book by its ID.
  • addBook: Tests the addition of a new book.
  • updateBook: Tests the update of an existing book.
  • deleteBook: Tests the deletion of a book by its ID.

JWT Token Utility Tests

These tests validate the functionality of the JWT token utility:

  • testGenerateToken: Tests the generation of a JWT token.
  • testValidateToken: Tests the validation of a JWT token.
  • testGetUsernameFromToken: Tests the extraction of the username from a JWT token.
  • testIsTokenExpired: Tests the expiration check of a JWT token.

Run this command

mvn test

Coverage Report

Class: 100% , Method: 81% , Lines: 79%

coverage

Contribution

Feel free to fork this repository and contribute by submitting pull requests. Make sure to follow the project's coding standards and include tests for new features.

Contact

For any questions or support, please open an issue on GitHub or contact the repository owner at [email protected].

About

Library Management System using Spring Boot. The system facilitates librarians in efficiently managing books, patrons, and borrowing records.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages