This is a backend API for a hotel reservation system built in Go. The API allows users to manage hotels, rooms, and bookings. It also includes user authentication and authorization.
- Fiber: Web framework for building REST APIs.
- JWT: User authentication and authorization using JWT.
- GoDotEnv: Loads environment variables from a
.env
file. - MongoDB Go Driver: MongoDB driver for Go.
- POST /api/auth: Authenticates a user and returns a JWT token.
- Handler:
HandleAuthentication
- Implementation: Validates user credentials and generates a JWT token using
CreateTokenFromUser
- Handler:
-
POST /api/v1/user: Creates a new user.
- Handler:
HandlePostUser
- Implementation: Parses user data, validates it, and stores it in the database.
- Handler:
-
GET /api/v1/users: Retrieves all users.
- Handler:
HandleGetUsers
- Implementation: Fetches all users from the database.
- Handler:
-
GET /api/v1/user/:id: Retrieves a user by ID.
- Handler:
HandleGetUser
- Implementation: Fetches a user by ID from the database.
- Handler:
-
PUT /api/v1/user/:id: Updates a user by ID.
- Handler:
HandleUpdateUser
- Implementation: Updates user data in the database.
- Handler:
-
DELETE /api/v1/user/:id: Deletes a user by ID.
- Handler:
HandleDeleteUser
- Implementation: Deletes a user from the database.
- Handler:
-
GET /api/v1/hotels: Retrieves all hotels.
- Handler:
GetHotels
- Implementation: Fetches all hotels from the database.
- Handler:
-
GET /api/v1/hotel/:id: Retrieves a hotel by ID.
- Handler:
GetHotel
- Implementation: Fetches a hotel by ID from the database.
- Handler:
-
GET /api/v1/hotel/:id/rooms: Retrieves all rooms for a hotel.
- Handler:
GetRooms
- Implementation: Fetches all rooms for a specific hotel from the database.
- Handler:
- POST /api/v1/room/:id/book: Books a room.
- Handler:
HandleBookRoom
- Implementation: Validates booking data, checks room availability, and stores the booking in the database.
- Handler:
-
GET /api/v1/booking/:id: Retrieves a booking by ID.
- Handler:
HandleGetBooking
- Implementation: Fetches a booking by ID from the database.
- Handler:
-
GET /api/v1/bookings: Retrieves all bookings.
- Handler:
HandleGetBookings
- Implementation: Fetches all bookings from the database.
- Handler:
-
GET /api/v1/booking/:id/cancel: Cancels a booking by ID.
- Handler:
HandleCancelBooking
- Implementation: Cancels a booking by ID in the database.
- Handler:
- GET /api/v1/admin/bookings: Retrieves all bookings (admin only).
- Handler:
HandleGetBookings
- Implementation: Fetches all bookings from the database.
- Handler:
-
Clone the repository:
git clone https://github.com/yourusername/hotel-reservation.git cd hotel-reservation
-
Install dependencies:
go mod tidy
-
Set up environment variables: Create a
.env
file in the root directory and add the following variables:JWT_SECRET=your_jwt_secret DB_URI=your_mongodb_uri DB_NAME=your_database_name
-
Use Make:
-
Build the application:
make build
-
Run the application:
make run
-
Seed the database:
make seed
-
Run tests:
make test