Skip to content

arfa79/booking

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Booking API

This project is a Django-based API for booking hotel rooms. It provides endpoints for user authentication and room booking.

Features

  • User authentication using JWT tokens
  • Room booking with date validation and conflict checking
  • Custom responses for API endpoints
  • Logging for debugging and monitoring

Technologies Used

  • Django: Web framework for building the API.
  • Django REST Framework: For creating RESTful API endpoints.
  • JWT Authentication: For secure user authentication.
  • PostgreSQL: Database used for storing data (you can modify it in the .env file).
  • Python: Programming language used.

Installation

Follow these steps to set up the project locally:

  1. Clone the repository:

    git clone https://github.com/arfa79/booking.git
    cd booking
  2. Create and activate a virtual environment:

    python -m venv venv
    source venv/bin/activate
  3. Install the dependencies:

    pip install -r requirements.txt
  4. Set up environment variables: Create a .env file in the root directory and add the following variables:

    SECRET_KEY=<your-secret-key>
    DEBUG=True
    DATABASE_URL=postgres://username:password@localhost/dbname
  5. Apply migrations:

    python manage.py migrate
  6. Create a superuser:

    python manage.py createsuperuser
  7. Run the development server:

    python manage.py runserver

    The development server will be running at http://127.0.0.1:8000.

Usage

Django Shell Usage For Database Data Injection

To create initial data for your application, you can use the Django shell.

  1. Open Django Shell
    Run the following command in your terminal:
    python manage.py shell
  2. Import the Necessary Models Use these imports to access your models and the Django User model:
    from core.models import Hotel, Room
    from django.contrib.auth.models import User
  3. Create a Hotel Object Replace "Grand Plaza Hotel" and "New York City" with your desired hotel name and location:
    hotel = Hotel.objects.create(name="Grand Plaza Hotel", location="New York City")
  4. Create Room Objects Use the Room model to create rooms associated with the hotel. Replace room numbers as needed:
    room1 = Room.objects.create(hotel=hotel, room_number="101")
    room2 = Room.objects.create(hotel=hotel, room_number="102")
  5. Create a User Replace "testuser" and "password123" with the username and password you want to use:
    user = User.objects.create_user(username="testuser", password="password123")
  6. Print the IDs Print the IDs of the created objects to confirm successful creation:
    print(f"Hotel ID: {hotel.id}, Room1 ID: {room1.id}, Room2 ID: {room2.id}, User ID: {user.id}")

Authentication Examples

  • Obtain a JWT token:

    POST /api/token/

    Request Body:

    {
      "username": "alireza",
      "password": "123"
    }

    Response:

    {
      "access": "your-access-token",
      "refresh": "your-refresh-token"
    }
  • Refresh a JWT token:

    POST /api/token/refresh/

    Request Body:

    {
      "refresh": "your-refresh-token"
    }

Booking Examples

  • Create a booking:

    POST /booking/

    Request Body:

    {
        "room": 1,
        "start_at": "2026-06-01T12:00:00",
        "end_at": "2026-06-01T14:00:00"
    }

    Response:

    {
        "detail": "Success",
        "code": "success",
        "error": null,
        "data": {
            "message": "Booking successful!",
            "booking_id": 1
        }
    }

Running Tests

To run the tests, use the following command: sh python manage.py test

Postman Examples

Below are examples of using Postman to interact with the API:

1. Obtain JWT Token

POST /api/token/

JWT Token Request

Response:

JWT Token Response


2. Create a Booking

POST /booking/

Create Booking Request

Response:

Create Booking Response


Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.

About

this app is an api for booking a hotel

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages