Skip to content

A modern API for booking train tickets at stations. Built with Node.js Express, TypeScript, Prisma, and MySQL, featuring authentication, ticket management, and station administration endpoints. Secure and efficient, ready for integration into your projects.

License

Notifications You must be signed in to change notification settings

redaezziani/train-api

Repository files navigation

Train Station Ticket Reservation API

Train Station Ticket Reservation API

Welcome to the Train Station Ticket Reservation API repository! This project provides a modern and efficient solution for booking train tickets at stations. The API is built using Node.js Express, with TypeScript, Zod, Prisma, MySQL, and Passport.js.

Table of Contents

  1. Setup Instructions
  2. Available Endpoints
  3. Running the Project
  4. Security Considerations
  5. Model Routes
  6. Links

Setup Instructions

  1. Clone the Repository:
git clone https://github.com/redaezziani/train-station-ticket-reservation-api.git
cd train-station-ticket-reservation-api
  1. Install Dependencies:
npm install
  1. Set Environment Variables:

    Create a .env file in the root directory and add the following environment variables:

# Rename this file to .env and fill in the values for your environment

# PostgreSQL Database URL
DATABASE_URL="postgresql://your_username:your_password@your_host/your_database?sslmode=require"

# Secret key for JWT token
JWT_SECRET="your_jwt_secret_key"

# JWT token expiration time
JWT_EXPIRES_IN="1d"

# Google OAuth2 Client ID
GOOGLE_CLIENT_ID="your_google_client_id"

# Google OAuth2 Client Secret
GOOGLE_CLIENT_SECRET="your_google_client_secret"

# API key for resending service
RESEND_API_KEY="your_resend_api_key"

# Secret key for session
SESSION_SECRET="your_session_secret_key"

# Base URL of your application
HOST_URL="http://your_host:your_port"

# Port for your application
PORT=your_port
  1. Run Migrations:

    Run the Prisma migrations to create the database schema:

npx prisma migrate dev
  1. Run generate:

    Run the Prisma generate to generate the client:

npx prisma generate
  1. Seed the Database:

    Seed the database with initial data:

npx prisma db seed
  1. Start the Server:

    Start the development server:

npm run dev

The API provides the following endpoints:

Available Endpoints

-you can visit the API documentation at http://localhost:3000/api-docs to see the available endpoints and their descriptions.

Train Station Ticket Reservation API

  • Authentication Routes:

  • /auth/register: Register a new user.

  • /auth/login: Login with existing credentials.

  • /auth/forgot-password: Request a password reset.

  • /auth/reset-password: Reset password with a token.

  • Line Routes:

  • /line/lines: Get all train lines.

  • /line/:id: Get details of a specific train line.

  • /line: Create a new train line (Admin only).

  • /line/:id: Update details of a train line (Admin only).

  • /line/:id: Delete a train line (Admin only).

  • Car Routes:

  • /car/cars: Get all train cars.

  • /car/:id: Get details of a specific train car.

  • /car: Create a new train car (Admin only).

  • /car/:id: Update details of a train car (Admin only).

  • /car/:id: Delete a train car (Admin only).

Running the Project

To run the project, follow the setup instructions above. After starting the server, you can access the API at http://localhost:3000.

Security Considerations

The API uses JWT tokens for authentication and authorization. Make sure to keep the JWT_SECRET environment variable secure. The API also includes rate limiting to prevent abuse.

Model Routes

The API provides the following routes for each model:

  • User:

  • /user/users: Get all users.

  • /user/:id: Get details of a specific user.

  • /user: Create a new user (Admin only).

  • /user/:id: Update details of a user (Admin only).

  • /user/:id: Delete a user (Admin only).

  • Ticket:

  • /ticket/tickets: Get all tickets.

  • /ticket/:id: Get details of a specific ticket.

  • /ticket: Create a new ticket.

  • /ticket/:id: Update details of a ticket.

  • /ticket/:id: Delete a ticket.

  • Reservation:

  • /reservation/reservations: Get all reservations.

  • /reservation/:id: Get details of a specific reservation.

  • /reservation: Create a new reservation.

  • /reservation/:id: Update details of a reservation.

  • /reservation/:id: Delete a reservation.

  • Station:

  • /station/stations: Get all stations.

  • /station/:id: Get details of a specific station.

  • /station: Create a new station (Admin only).

  • /station/:id: Update details of a station (Admin only).

  • /station/:id: Delete a station (Admin only).

  • Train:

  • /train/trains: Get all trains.

  • /train/:id: Get details of a specific train.

  • /train: Create a new train (Admin only).

  • /train/:id: Update details of a train (Admin only).

  • /train/:id: Delete a train (Admin only). -and more...

Links

Badges

MySQL NodeJS TailwindCSS Zod Prisma

About

A modern API for booking train tickets at stations. Built with Node.js Express, TypeScript, Prisma, and MySQL, featuring authentication, ticket management, and station administration endpoints. Secure and efficient, ready for integration into your projects.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published