This project is a RESTful API built with Node.js, Express.js, and MongoDB. It allows users to perform CRUD (Create, Read, Update, Delete) operations on a movie collection. The API is designed to manage movie data such as name, genre, and release year, with robust validation using Joi and data management via Mongoose.
- Create a Movie: Add a new movie with name, genre, and year.
- Read Movies: Retrieve all movies or a specific movie by ID.
- Update a Movie: Modify the details of an existing movie.
- Delete a Movie: Remove a movie from the collection by its ID.
- Input Validation: Ensures valid data using Joi before saving to the database.
- Node.js: Backend runtime environment.
- Express.js: Framework for building RESTful APIs.
- MongoDB: NoSQL database for storing movie data.
- Mongoose: ODM (Object Data Modeling) library for MongoDB.
- Joi: Data validation library for validating input data.
- Description: Add a new movie.
- Request Body:
{ "name": "Movie Name", "genre": "Genre", "year": 2023 }
- Response:
- Success (201):
{ "_id": "movie_id", "name": "Movie Name", "genre": "Genre", "year": 2023, "createdAt": "2024-11-24T00:00:00.000Z", "updatedAt": "2024-11-24T00:00:00.000Z" }
- Error (401):
{ "message": "Error message" }
- Success (201):
- Method:
GET
- Description: Retrieve all movies in the collection.
- Response:
- Success (200):
[ { "_id": "movie_id", "name": "Movie Name", "genre": "Genre", "year": 2023 }, ... ]
- Error (500):
{ "message": "Server error" }
- Success (200):
- Method:
GET
- Description: Retrieve a movie by its ID.
- Response:
- Success (200):
{ "_id": "movie_id", "name": "Movie Name", "genre": "Genre", "year": 2023 }
- Error (404):
{ "message": "Movie not found" }
- Success (200):
- Method:
PUT
- Description: Update a movie's details by its ID.
- Request Body:
{ "name": "Updated Movie Name", "genre": "Updated Genre", "year": 2024 }
- Response:
- Success (200):
{ "_id": "movie_id", "name": "Updated Movie Name", "genre": "Updated Genre", "year": 2024 }
- Error (404):
{ "message": "Movie not found" }
- Error (400):
{ "message": "Validation error" }
- Success (200):
- Method:
DELETE
- Description: Delete a movie from the collection by its ID.
- Response:
- Success (200):
{ "message": "Movie deleted successfully." }
- Error (404):
{ "message": "Movie not found" }
- Success (200):
- Name: Required, between 3 and 100 characters.
- Genre: Required, between 3 and 150 characters.
- Year: Required, an integer between 1888 and the current year.
git clone /~https://github.com/Choubi-Mohammed/Project-CRUD-Movies.git
cd Project-CRUD-Movies
npm install
- Ensure MongoDB is running locally, or use a remote MongoDB service like MongoDB Atlas.
- Update the connection URL in your project if necessary.
npm start
The API will be running at http://localhost:3000
.
- 400 Bad Request: When the request body doesn't meet validation criteria.
- 404 Not Found: When the movie with the given ID is not found.
- 500 Internal Server Error: When an unexpected error occurs on the server.
POST /api/movies
{
"name": "Inception",
"genre": "Sci-Fi",
"year": 2010
}
{
"_id": "movie_id",
"name": "Inception",
"genre": "Sci-Fi",
"year": 2010,
"createdAt": "2024-11-24T12:00:00.000Z",
"updatedAt": "2024-11-24T12:00:00.000Z"
}
- User authentication and authorization (JWT, OAuth).
- Advanced search and filtering options.
- Front-end interface for interacting with the API.
This project is licensed under the MIT License - see the LICENSE file for details.