Skip to content

Create a Node, Express, and MongoDB Server Application with Docker.

License

Notifications You must be signed in to change notification settings

Michle99/sba_319_MongoDB_Database_Application

Repository files navigation

SBA 319: MongoDB Database Application

Michle99 - sba_319_MongoDB_Database_Application stars - sba_319_MongoDB_Database_Application forks - sba_319_MongoDB_Database_Application

SBA_319_MongoDB_Express_Docker_CICD

License

Use this template

Table of Contents

  1. Introduction
  2. Objectives
  3. Movies API Documentation
  4. Swagger Movie API Documentation
  5. Creating a Docker Image and Container
  6. Dependencies
  7. Resources
  8. Getting Started

Introduction

This assessment measures your understanding of MongoDB and your capability to implement its features in a practical manner. You have creative freedom in the topic, material, and purpose of the web application you will be developing, so have fun with it! However, remember to plan the scope of your project to the timeline you have been given.

Objectives

  1. Create a server application with Node, Express, and MongoDB.
  2. Create a CRUD API using Express and MongoDB.
  3. Use MongoDB indexing to make efficient queries.
  4. Create MongoDB validation rules.
  5. Use MongoDB validation to ensure data consistency.

Movies API Documentation

The Movies API allows you to manage a collection of movies. It supports CRUD (Create, Read, Update, Delete) operations to interact with movie data.

Base URL

http://localhost:5000/movies

Movies Routes Requests and Responses

1. Get All Movies

  • Endpoint: GET /

  • Description: Get a list of all movies.

  • Example Usage:

GET http://localhost:5000/movies
  • Example Response:
[
  {
    "_id": {
        "$oid": "573a1391f29313caabcd7db6"
    },
    "plot": "In Paris a wild girl becomes possessed by the soul of her twin who died to save her life.",
    "genres": [
        "Drama"
    ],
    "runtime": 82,
    "cast": [
        "Betty Compson",
        "Clive Brook",
        "Henry Victor",
        "A.B. Imeson"
    ],
    "title": "White Shadows",
    "fullplot": "In Paris a wild girl becomes possessed by the soul of her twin who died to save her life.",
    "languages": [
        "English"
    ],
    "released": {
        "$date": {
        "$numberLong": "-1440892800000"
        }
    },
    "directors": [
        "Graham Cutts"
    ],
    "writers": [
        "Alfred Hitchcock",
        "Michael Morton (novel)"
    ],
    "awards": {
        "wins": 1,
        "nominations": 0,
        "text": "1 win."
    },
    "lastupdated": "2015-05-10 00:19:38.277000000",
    "year": 1924,
    "imdb": {
        "rating": 6.5,
        "votes": 156,
        "id": 15493
    },
    "countries": [
        "UK"
    ],
    "type": "movie",
    "tomatoes": {
        "viewer": {
        "rating": 2.8,
        "numReviews": 11
        },
        "lastUpdated": {
        "$date": "2015-06-14T18:00:09.000Z"
        }
    },
    "num_mflix_comments": 0
  }
  // ...
]

Get All Movies Screenshots

GET Screenshot 1

GET Screenshot 2


2. Get a Single Movie

  • Endpoint: GET /:id

  • Description: Get details of a specific movie by ID.

  • Parameters:

    • id: Movie ID
  • Example Usage:

GET http://localhost:5000/movies/573a1391f29313caabcd7db6
  • Example Response:
{
  "_id": {
    "$oid": "573a1391f29313caabcd7db6"
  },
  "plot": "In Paris a wild girl becomes possessed by the soul of her twin who died to save her life.",
  "genres": [
    "Drama"
  ],
  "runtime": 82,
  "cast": [
    "Betty Compson",
    "Clive Brook",
    "Henry Victor",
    "A.B. Imeson"
  ],
  "title": "White Shadows",
  "fullplot": "In Paris a wild girl becomes possessed by the soul of her twin who died to save her life.",
  "languages": [
    "English"
  ],
  "released": {
    "$date": {
      "$numberLong": "-1440892800000"
    }
  },
  "directors": [
    "Graham Cutts"
  ],
  "writers": [
    "Alfred Hitchcock",
    "Michael Morton (novel)"
  ],
  "awards": {
    "wins": 1,
    "nominations": 0,
    "text": "1 win."
  },
  "lastupdated": "2015-05-10 00:19:38.277000000",
  "year": 1924,
  "imdb": {
    "rating": 6.5,
    "votes": 156,
    "id": 15493
  },
  "countries": [
    "UK"
  ],
  "type": "movie",
  "tomatoes": {
    "viewer": {
      "rating": 2.8,
      "numReviews": 11
    },
    "lastUpdated": {
      "$date": "2015-06-14T18:00:09.000Z"
    }
  },
  "num_mflix_comments": 0
}

Get a Single Movie Screenshots

Get a single movie screenshot 0

Get a single movie screenshot 1


3. Add a New Movie

  • Endpoint: GET /

  • Description: Add a new movie to the collection.

  • Example Usage:

POST http://localhost:5000/movies
  • Request Body:
{
  "plot": "Takuma Sakamoto, a reclusive gamer, is unexpectedly transported into the virtual world of his favorite MMORPG as his in-game character, the powerful Demon Lord Diablo. Two girls who summoned him to control him end up becoming his servants due to a magic rebound. Takuma, dealing with social anxiety, adopts his in-game persona to navigate the new world. Alongside his companions, Rem and Shera, he embarks on a quest to remove the magic collars while assisting them with their personal struggles that led to their summoning.",
  "genres": ["isekai"],
  "runtime":23,
  "cast": [
    "Diablo",
    "Sheera",
    "Rem"
  ],
  "poster": "https://thecinemaholic.com/wp-content/uploads/2021/06/How-not-to-summon.jpg",
  "title": "How not to summon a Demon Lord season 2",
  "fullplot": "Takuma Sakamoto, a reclusive gamer, is unexpectedly transported into the virtual world of his favorite MMORPG as his in-game character, the powerful Demon Lord Diablo. Two girls who summoned him to control him end up becoming his servants due to a magic rebound. Takuma, dealing with social anxiety, adopts his in-game persona to navigate the new world. Alongside his companions, Rem and Shera, he embarks on a quest to remove the magic collars while assisting them with their personal struggles that led to their summoning.",
  "languages": [
    "English"
  ],
  "released": "2021-04-09T00:00:00.000Z",
  "directors": [
    "Satoshi Kuwabara"
  ],
  "rated": "TV-G",
  "awards": {},
  "lastupdated": "",
  "year": 2018,
  "imdb": {},
  "countries": [
    "JAPAN"
  ],
  "type": "ANIME",
  "tomatoes": {
    "viewer": {},
    "fresh": 6,
    "critic": {
      "rating": 7.6,
      "numReviews": 6,
      "meter": 100
    },
    "rotten": 0,
    "lastUpdated": ""
  },
  "num_mflix_comments": 0
}
  • Example Response:
{
    "acknowledged": true,
    "insertedId": "6557d63b84def3c71fe4da1c"
}

Post request screenshot

Post Request and Response


4. Update a Movie

  • Endpoint: PUT /:id
  • Description: Update details of a specific movie by ID.
  • Parameters:
    • id: Movie ID
  • Request Body:
{
  "type": "ANIMES WEB SERIES"
}
  • Example Usage:
PUT http://localhost:5000/movies/573a1391f29313caabcd7db6

  • Example Response:
{
  "acknowledged": true,
  "modifiedCount": 1,
  "upsertedId": null,
  "upsertedCount": 0,
  "matchedCount": 1
}

PUT Request Screenshot

Update new movie

5. Delete a Movie

  • Endpoint: DELETE /:id

  • Description: Delete a specific movie by ID.

  • Parameters:

    • id: Movie ID
  • Example Usage:

DELETE http://localhost:5000/movies/573a1391f29313caabcd7db6

  • Example Response:
    {
    "acknowledged": true,
    "deletedCount": 1
    }

Delete Response Screenshot

Delete new movie

6. FAILED VALIDATIONS REQUEST

  • Validation Rules Function:
// Define validation rules for the movies collection
const validationRules = {
  $jsonSchema: {
    bsonType: "object",
    required: ["plot", "genres", "type", "title", "poster", "cast", "fullplot", "runtime"],
    // ...
  }
};
  • Validation Example REQUEST

Failed movie validations

  • Validation Example Response

Failed movie validations

7. Error Handling

  • If the request fails, the API will respond with an appropriate error message and status code.

Swagger Movie API Documentation

The below screenshot is the API documentation for the routers in the project.

The url to the swaggerUI API documentation is: swaggerUI API Documentation

Swagger API UI

Swagger UI API documentation interface

Movie Schema

Movie Schema

Get All Movies video

get_all_movies.mp4

Get a Single Movie

get_a_single_movie.mp4

Post a new Movie

post_a_movies.mp4

Update an existing Movie

update_a_movie.mp4

Delete an existing Movie

delete_a_movie.mp4

Creating a Docker Image and Container

Docker is is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly.

  • Install docker desktop application: To create a docker container & image, download & install docker desktop: Install Docker Desktop
  • Create a Dockerfile in the root directory of the project.
  • Create a .dockerignore to exclude modules. Sample .dockerignore.
  • Create a docker compose to define the service(s) that will be used for the project. Docker Compose is is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. Then, with a single command, you create and start all the services from your configuration.

Here is a sample Docker Compose.

  • Lastly, build the docker container with the command:
docker-compose up -d --build

The Express application will be running on http://localhost:5000/api/movies.

  • Docker Repository: The docker image can be downloaded on hub.docker.com. Use the command to pull the docker image:
docker pull sam12302021/sba_319_mongodb-api

Dependencies

  • chai
  • chai-http
  • dotenv
  • express
  • mocha
  • mongodb
  • nodemon
  • swagger-jsdoc
  • swagger-ui-express
  • yamljs

Resources

  1. Mocha and Chai for testing.
  2. Swagger for Movie API Documentation.
  3. Test a Node RESTful API with Mocha and Chai
  4. Documenting your Express API with Swagger
  5. Docker to build docker images.
  6. Docker Compose for defining and running multi-container Docker applications.

Getting Started

Clone this repo: sba_319_MongoDB_Database_Application.

  • Install dependencies:
npm install 

or

npm i
  • Start local server:
npm run start

License

Released under MIT by @Michle99.