This repository contains a REST API project for managing a ranking system for men in various sports events. The API is built using Node.js, Express, and MongoDB, and it allows users to perform CRUD (Create, Read, Update, Delete) operations on the ranking data.
This project aims to create an API for managing the rankings of male athletes in different sports events. The API provides endpoints to add new ranking entries, retrieve rankings, update rankings, and delete ranking entries. The data is stored in a MongoDB database using Mongoose, which allows for easy modeling and interaction with the database.
The project is structured in the following way:
-
Dependencies and Schema: The project starts by importing necessary dependencies -
expressfor building the web application andmongoosefor working with MongoDB. It then defines amenSchemausing Mongoose's schema system. The schema specifies the structure of each ranking entry, including properties such asranking,name,dob,country,score, andevent. Some properties have specific requirements, like being required, unique, or having default values. -
Model: The
menSchemais used to create a Mongoose model namedMensRanking, which acts as an interface to the MongoDB collection named "MenRanking." This model provides methods to interact with the database and perform CRUD operations. -
API Endpoints: The project defines several API endpoints using Express's
Routermiddleware. Each endpoint corresponds to a specific CRUD operation.-
POST /mens: This endpoint handles the creation of a new ranking entry. It expects a JSON object representing a new player's data in the request body. The API creates a newMensRankingdocument and saves it to the MongoDB collection. -
GET /mens: This endpoint fetches all the ranking entries from the database and returns them in ascending order of their ranking. -
GET /mens/:id: This endpoint fetches a single ranking entry from the database based on the providedidparameter, which represents the unique identifier of the document. It returns the player's data as a response. -
PATCH /mens/:id: This endpoint handles the update of a specific ranking entry. It expects theidparameter to identify the entry to be updated and the new player's data in the request body. The ranking entry is updated with the new data, and the updated entry is returned as a response. -
DELETE /mens/:id: This endpoint handles the deletion of a specific ranking entry. It takes theidparameter to identify the entry to be deleted, and upon successful deletion, the deleted entry is returned as a response.
-
-
Database Connection: The project defines a function
connectDbto establish a connection to the MongoDB database. The function is called at the start of the application to ensure that the API can interact with the database. -
Express Application Setup: The Express application is created, and the required middleware is set up. The
express.json()middleware is used to parse incoming JSON requests. The router defined in therouter.jsfile is also included in the application usingapp.use(router). -
Server Start: The application listens for incoming requests on the specified port (default is 3000). When the server is started, a message indicating the successful establishment of the connection is displayed in the console.
To run this project locally, you need to have Node.js and MongoDB installed on your system. Follow the steps below to set up and run the project:
- Clone the repository to your local machine:
git clone https://github.com/your-username/rest-api-project.git
cd rest-api-project- Install the project dependencies:
npm install-
Set up the MongoDB database:
- Make sure MongoDB is installed and running on your system.
- Create a new database named
men_rankingsfor this project.
-
Configure the MongoDB connection:
- Open the
db/conn.jsfile in the project. - Replace
your_mongodb_uriwith the connection URI to your MongoDB database. The URI should include the database name (men_rankings).
- Open the
-
Start the server:
npm startThe server should now be running and listening on the specified port (default is 3000). You should see a message in the console indicating the successful establishment of the connection.
The following API endpoints are available for managing the men's ranking:
-
Create a New Ranking Entry
- Endpoint:
POST /mens - Request body: JSON object representing the new player's data
- Response: JSON object containing the newly created ranking entry
- Endpoint:
-
Get All Ranking Entries
- Endpoint:
GET /mens - Response: JSON array containing all ranking entries, sorted by ranking in ascending order
- Endpoint:
-
Get a Specific Ranking Entry
- Endpoint:
GET /mens/:id - Request parameter:
id- The unique identifier of the ranking entry - Response: JSON object representing the player's data with the specified
id
- Endpoint:
-
Update a Ranking Entry
- Endpoint:
PATCH /mens/:id - Request parameter:
id- The unique identifier of the ranking entry to be updated - Request body: JSON object representing the updated player's data
- Response: JSON object representing the updated ranking entry
- Endpoint:
-
Delete a Ranking Entry
- Endpoint:
DELETE /mens/:id - Request parameter:
id- The unique identifier of the ranking entry to be deleted - Response: JSON object representing the deleted ranking entry
- Endpoint:
Here are some examples of how to use the API:
-
Create a new ranking entry:
{ "ranking": 1, "name": "John Doe", "dob": "1990-05-15", "country": "USA", "score": 1000, "event": "100m" }' -
Get all ranking entries:
http://localhost:3000/mens
-
Get a specific ranking entry:
http://localhost:3000/mens/5f12a2eb3a2b9e2ea5d44c4b
-
Update a ranking entry:
{ "score": 1200 } http://localhost:3000/mens/5f12a2eb3a2b9e2ea5d44c4b -
Delete a ranking entry:
DELETE http://localhost:3000/mens/5f12a2eb3a2b9e2ea5d44c4b
Please note that the id used in the examples above should correspond to an existing ranking entry in the database.