Skip to content

DeNNYHZ/book-management-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Book Management API

A RESTful API for managing a collection of books built with Go, the Fiber web framework, and PostgreSQL.

Description

This project provides a RESTful API to manage books in a PostgreSQL database. It supports CRUD (Create, Read, Update, Delete) operations for book records. The API allows you to add new books, retrieve all books or a specific book by its ID, and delete books from the database.

Features

  • Create Book: Add a new book to the database with details such as author, title, and publisher.
  • Get All Books: Retrieve a list of all books stored in the database.
  • Get Book by ID: Fetch details of a specific book using its unique ID.
  • Delete Book: Remove a book from the database using its unique ID.

Endpoints

Create Book

  • POST /api/create_books
    • Description: Adds a new book to the database.
    • Request Body:
      {
        "author": "Author Name",
        "title": "Book Title",
        "publisher": "Publisher Name"
      }
    • Response:
      {
        "message": "Book created successfully",
        "data": {
          "id": 1,
          "author": "Author Name",
          "title": "Book Title",
          "publisher": "Publisher Name"
        }
      }

Get All Books

  • GET /api/books
    • Description: Retrieves a list of all books from the database.
    • Response:
      {
        "message": "Books fetched successfully",
        "data": [
          {
            "id": 1,
            "author": "Author Name",
            "title": "Book Title",
            "publisher": "Publisher Name"
          }
        ]
      }

Get Book by ID

  • GET /api/get_books/:id
    • Description: Retrieves details of a book by its ID.
    • Response:
      {
        "message": "Book fetched successfully",
        "data": {
          "id": 1,
          "author": "Author Name",
          "title": "Book Title",
          "publisher": "Publisher Name"
        }
      }

Delete Book

  • DELETE /api/delete_book/:id
    • Description: Deletes a book from the database using its ID.
    • Response:
      {
        "message": "Book deleted successfully"
      }

Setup and Installation

Prerequisites

Installation

  1. Clone the Repository:

    git clone https://github.com/yourusername/go-fiber-postgres.git
    cd go-fiber-postgres
  2. Install Dependencies:

    go mod tidy
  3. Setup Environment Variables

    Create a .env file in the root directory with the following content:

    DB_HOST=localhost
    DB_PORT=5432
    DB_USER=your_db_user
    DB_PASSWORD=your_db_password
    DB_NAME=your_db_name
    DB_SSLMODE=disable
    
  4. Run Database Migrations:

    Ensure PostgreSQL is running and accessible, then start the application to run the migrations:

    go run main.go

Usage

  • Start the Server:

    go run main.go

    The server will start on http://localhost:8080.

  • Access the API: Use tools like Postman or curl to interact with the API endpoints.

Benchmarking

To benchmark your PostgreSQL database, you can use tools like pgBench or SysBench.

Using pgBench

  1. Initialize the Database:

    pgbench -i -s 10 your_database
  2. Run the Benchmark:

    pgbench -c 10 -j 2 -T 60 your_database

Using SysBench

  1. Prepare the Database:

    sysbench --db-driver=pgsql --pgsql-host=localhost --pgsql-user=postgres --pgsql-password=your_password --pgsql-db=your_database oltp_read_write prepare
  2. Run the Benchmark:

    sysbench --db-driver=pgsql --pgsql-host=localhost --pgsql-user=postgres --pgsql-password=your_password --pgsql-db=your_database oltp_read_write --threads=10 --time=60 run

Contributing

Feel free to fork the repository and submit pull requests. Contributions are welcome!

Contact

For questions or feedback, please open an issue on GitHub or contact [email protected].