Skip to content

A RESTful API for managing a movie collection with CRUD operations. Built using Node.js, Express.js, and MongoDB, this API allows for creating, reading, updating, and deleting movies. It includes robust input validation with Joi and integrates with a MongoDB database through Mongoose.

License

Notifications You must be signed in to change notification settings

Choubi-Mohammed/Project-CRUD-Movies

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Project-CRUD-Movies

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.


Features

  • 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.

Technologies Used

  • 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.

API Endpoints

1. POST /api/movies

  • 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"
      }

2. GET /api/movies

  • 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"
      }

3. GET /api/movies/:id

  • 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"
      }

4. PUT /api/movies/:id

  • 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"
      }

5. DELETE /api/movies/:id

  • 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"
      }

Validation Rules

  • Name: Required, between 3 and 100 characters.
  • Genre: Required, between 3 and 150 characters.
  • Year: Required, an integer between 1888 and the current year.

Installation

1. Clone the repository:

git clone /~https://github.com/Choubi-Mohammed/Project-CRUD-Movies.git
cd Project-CRUD-Movies

2. Install dependencies:

npm install

3. Set up MongoDB:

  • Ensure MongoDB is running locally, or use a remote MongoDB service like MongoDB Atlas.
  • Update the connection URL in your project if necessary.

4. Start the server:

npm start

The API will be running at http://localhost:3000.


Error Handling

  • 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.

Example Request and Response

POST /api/movies

POST /api/movies
{
  "name": "Inception",
  "genre": "Sci-Fi",
  "year": 2010
}

Response:

{
  "_id": "movie_id",
  "name": "Inception",
  "genre": "Sci-Fi",
  "year": 2010,
  "createdAt": "2024-11-24T12:00:00.000Z",
  "updatedAt": "2024-11-24T12:00:00.000Z"
}

Future Enhancements

  • User authentication and authorization (JWT, OAuth).
  • Advanced search and filtering options.
  • Front-end interface for interacting with the API.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A RESTful API for managing a movie collection with CRUD operations. Built using Node.js, Express.js, and MongoDB, this API allows for creating, reading, updating, and deleting movies. It includes robust input validation with Joi and integrates with a MongoDB database through Mongoose.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published