This is a CRUD (Create, Read, Update, Delete) API for managing movie records with user authentication. The API allows you to perform various operations on movies and users using different endpoints, also including upload an image to local or cloud storage.
You can try the API in here or deploy it manually below.
- User registration and login functionality
- Secure authentication using JWT (JSON Web Tokens)
- Create, Read, Update, and Delete operations for movie records
- Authorization for admin-only actions
- Upload image for movie records into local or cloud storage
To get started with the API, follow the steps below:
-
Clone this repository:
git clone https://github.com/mch-fauzy/movies-db-api.git
-
Navigate to the project directory:
cd movies-db-api
-
Install the required dependencies:
npm install
-
(Optional) Sign-up and configure your cloud storage in https://cloudinary.com (or you can use other cloud storage) if you want to use
/v1/movies/:id/upload-cloud
endpoint. -
Edit the database configuration in
.env.development
with your PostgreSQL credentials and configure the setting in./infras/postgresql.js
. -
(Important) Import the required database schema from
migrations/movie-database.sql
into your PostgreSQL database using a tool like pgAdmin's restore function. -
(Important) Seed
Admin
user using sql frommigrations/admin-seed.sql
and run the sql frommigrations/add-image-column.sql
in pgAdmin or other database administration tool (e.g. DBeaver). -
Start the server:
npm run dev
The API will be available at http://localhost:3000.
To access the API documentation using Swagger, follow these steps:
- Make sure the server is running locally.
- Open your web browser and go to http://localhost:3000/.
- You'll see the Swagger UI interface with a list of endpoints, request parameters, and example requests/responses.
- In Swagger, set your servers to
http://localhost:3000
to access your local database. - You can interact with the API directly from the Swagger interface.
- Endpoint:
POST /v1/auth/register
- Description: Register a new user (first admin can be seeded)
- Request Body Parameters:
email
,gender
,password
-
Endpoint:
POST /v1/auth/login
-
Description: User login.
-
Request Body Parameters:
email
,password
-
Example:
Admin:
{ "email":"admin@gmail.com", "password":"adminpassword" }
Non-Admin:
{ "email":"non.admin@gmail.com", "password":"nonadminpassword" }
- Endpoint:
GET /v1/users
- Description: Get all users with pagination.
- Authentication: Requires authentication and admin role.
- Query Parameters:
page
(default: 1),size
(default: 10)
- Endpoint:
GET /v1/movies
- Description: Get all movies with pagination.
- Authentication: Requires authentication.
- Query Parameters:
page
(default: 1),size
(default: 10)
- Endpoint:
POST /v1/movies
- Description: Insert a new movie.
- Authentication: Requires authentication and admin role.
- Request Body Parameters:
title
,genres
,year
- Endpoint:
POST /v1/movies/:id/upload-local
- Description: Upload an image for a movie to local storage. The uploaded image is stored in the
public/images
directory and associated with the specified movie. - Authentication: Requires authentication and admin role.
- Request Body Parameter:
image
- Path Parameter:
id
(movie ID)
- Endpoint:
POST /v1/movies/:id/upload-cloud
- Description: Upload an image for a movie to cloud storage. The uploaded image is stored in the https://cloudinary.com and associated with the specified movie.
- Authentication: Requires authentication and admin role.
- Request Body Parameter:
image
- Path Parameter:
id
(movie ID)
- Endpoint:
PUT /v1/movies/:id
- Description: Update a movie by ID.
- Authentication: Requires authentication and admin role.
- Path Parameter:
id
(movie ID) - Request Body Parameters:
title
,genres
,year
- Endpoint:
DELETE /v1/movies/:id
- Description: Delete a movie by ID.
- Authentication: Requires authentication and admin role.
- Path Parameter:
id
(movie ID)
The API provides appropriate error messages and status codes for various scenarios, including invalid requests, authentication failures, and internal server errors.
Contributions are welcome! Feel free to open issues and submit pull requests for enhancements or bug fixes.
This project is licensed under the MIT License. See the LICENSE file for details.