Skip to content

Repository files navigation

📚 Library API

Simple library API project enabling to manage books, login and register users.

Technologies Used

  • Java 21
  • Spring Boot 4.0.6
  • Spring Data JPA
  • Spring Security
  • PostgreSQL 18.1
  • JWT (JSON Web Tokens)
  • Gradle Kotlin DSL
  • Flyway

Installation

  1. Clone the repository:
    git clone git@github.com:Qbiterv/library-api.git
  2. Navigate to the project directory:
    cd library-api

Usage

Important note

  1. Preferred Postgres version is 18.1

  2. Preferred Java version is 21

  3. To make integration tests you need Docker installed and running on your machine.

  4. Then you can run tests with command:

      ./gradlew test
  5. There is a way to create default admin user by running the application with local profile and setting it up in properties. This is not recommended for production use, but can be useful for testing and development purposes.

    1. test.admin.create=true - This property enables the creation of a default admin user.
    2. test.admin.username=admin - This property sets the username for the default admin user.
    3. test.admin.password=admin - This property sets the password for the default admin user.

Ways to run the application

Way to run in local development environment:

  1. Configure database connection and JWT settings:
    1. Create a application-local.properties file in the src/main/resources directory with the following content ( application-local.properties.example can be used as a template):
      spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
      spring.datasource.username=postgres
      spring.datasource.password=password
      jwt.expiration=8640000
      jwt.secret=your_jwt_secret_key
      
      test.admin.create=true
      test.admin.username=admin
      test.admin.password=admin
  2. Build the project using Gradle:
    ./gradlew build
  3. Run the application:
    ./gradlew bootRun --args='--spring.profiles.active=local'

(One of many) way to run in more-production way:

  1. Build the project using Gradle:

    ./gradlew build
  2. Export environment variables for database connection and JWT settings:

     export SPRING_DATASOURCE_URL=jdbc:postgresql://localhost:5432/postgres
     export SPRING_DATASOURCE_USERNAME=postgres
     export SPRING_DATASOURCE_PASSWORD=password
     export JWT_EXPIRATION=8640000
     export JWT_SECRET=your_jwt_secret_key
  3. Run the application:

    java -jar build/libs/library-api-0.0.1-SNAPSHOT.jar

API Endpoints

  1. User Registration: POST /auth/register
     {
       "username": "string",
       "password": "string"
     }
  2. User Login: POST /auth/login
     {
       "username": "string",
       "password": "string"
     }
    • Response:
    {
      "code": "USER_LOGIN",
      "message": "token",
      "timestamp": "yyyy-mm-ddThh:mm:ss"
    }

All requests below requires Auth Bearer Header

  1. Get All Books: GET /books
  2. Get Book by ID: GET /books/{id}
  3. Get Book by ISBN: GET /books/isbn/{isbn}

All requests below requires User with ADMIN role

  1. Create Book: POST /books
    {
        "title": "string",
        "author": "string",
        "publishedYear": "short",
        "isbn": "string",
        "pages": "int"
    }
  2. Delete Book by ID: DELETE /books/{id}
  3. Delete Book by ISBN: DELETE /books/isbn/{isbn}

Updates can be made partially

  • Full Request:
    {
        "title": "string",
        "author": "string",
        "publishedYear": "short",
        "isbn": "string",
        "pages": "int"
    } 
  • Example Request (updating pages count)
    {
      "pages": 103
    }  
  1. Update Book by ID: PATCH /books/{id}
  2. Update Book by ISBN: PATCH /books/isbn/{isbn}

About

Library API project based on Spring Boot.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages