Ticket Booking API

Made with Python Built with Love Open Source Love

A Ticket Booking System API made using Django (Python-based free and open-source web framework)

Problem Statement

You have to design a REST interface for a movie theatre ticket booking system. It should support the following business cases:

  • An endpoint to book a ticket using a user’s name, phone number, and timings.
  • Maximum 20 tickets can be booked for a particular time.
  • An endpoint to update a ticket timing.
  • An endpoint to view all the tickets for a particular time.
  • An endpoint to delete a particular ticket.
  • An endpoint to view the user’s details based on the ticket id.
  • Mark a ticket as expired if there is a diff of 8 hours between the ticket timing and current
  • Delete all the tickets which are expired automatically.
  • Write the tests for all the endpoints.

RESTful Structure of API

Endpoint HTTP Method CRUD Method Result
api/tickets GET READ Get all tickets
api/tickets/{id} GET READ Get a ticket using ID
api/tickets?date={date}&time={time} GET READ Get all tickets with specified ID
api/tickets POST CREATE Book a Ticket
api/tickets/{id} PUT UPDATE Update a ticket
api/tickets/{id} DELETE DELETE Delete a ticket

Additional Features of API:

  • Every 5 minutes - Mark ticket as expired if there is a diff of 8 hours between the ticket time and current time
  • Every 10 minutes - Deleting all the Expired Tickets Automatically
  • Test Cases - Test Driven RESTful API Development

DataBase Fields

Name Phone number Date Time Expired

Operations Supported by API

  • An endpoint to view all the Tickets.
GET
/api/tickets

GET ALL TICKETS

  • An endpoint to view the user’s details based on the ticket id.
GET
/api/tickets/{id}

GET TICKET BY ID

  • An endpoint to view all the tickets for a particular time.

There are 3 functionalities for getting the tickets for a specified Timing, which are:

  1. To view the tickets for a specified Date and Time
GET
/api/tickets?date={date}&time={time}

GET TICKET BY DATE AND TIME

  1. To view the tickets for a specified Date
GET
/api/tickets?date={date}

GET TICKETS BY DATE

  1. To view the tickets for a specified Time
GET
/api/tickets?time={time}

GET TICKETS BY TIME

  • An endpoint to book a ticket using a user’s name, phone number, and timings.
POST
/api/tickets

POST TICKET

  • An endpoint to update a ticket timing.
PUT
/api/tickets/{id}/

UPDATE TICKET

  • An endpoint to delete a particular ticket
DELETE
/api/tickets/{id}/

DELETE TICKET

Additional Features

  • Mark a ticket as expired if there is a diff of 8 hours between the ticket timing and current time.

    Added a Cron Job to run at the interval of 5 minutes To Mark a Ticket as Expired if the Difference is greater than or equal to 8 hours.

    MARK TICKETS AS EXPIRED CRON FUNCTION

    Cron function to mark expired

    CRON JOB (Specifying CRON FUNCTION to run every 5 minutes)

    Cron Job to mark expired

  • Delete all the tickets which are expired.

    Added a Cron Job to run at the interval of 10 minutes to Delete all the Expired Tickets.

    CRON FUNCTION TO DELETE EXPIRED TICKETS

    Cron function to delete expired tickets

    CRON JOB (Specifying the CRON FUNCTION to run every 10 minutes)

    Cron Job to delete expired ticket

    To understand the working of the Cron Jobs

    • Making Ticket Expired
    • Deleting Expired Tickets

    Watch the below video,

    Image

Testing the API (Writing Test Cases)

  • To run the test cases, use the command
python manage.py test

To write some more test cases for the API, head over to the file API/test.py. It's the file containing all the test cases for the API.

TEST RUN SUCCESSFUL

Installation and Running

  • Run the command
pip install -r requirements.txt

to install all the required dependencies.

  • Now run the command
python manage.py runserver

To run the Django Server on your localhost.

NOTE: If the default port is busy, you can run the server on any other port using the command,

python manage.py runserver localhost:{port number}

Django Admin

You can see the data inside the database in the Django Admin Panel.

Just go to the link

localhost:{port number}/admin

and provide the Username and Password Details. (For simplicity I have already created a SuperUser)

Username Password
test test@123