Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project Mongo API #510

Open
wants to merge 41 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
af58839
Change port to 1224. Add elves.json file with data.
joheri1 Dec 12, 2024
adb50f7
Import elves from elves.json
joheri1 Dec 12, 2024
69bcbaf
Add dependencies to README. Install expressListEndpoints and import i…
joheri1 Dec 12, 2024
3269742
Add Documentation endpoint to server.js. Update README with this info.
joheri1 Dec 12, 2024
8460941
Add Route for /elves/all. Add Mongoose model for Elf and implement se…
joheri1 Dec 12, 2024
9cc6c95
Change data to elves on line 26 in server.js, elves.forEach((elfData)
joheri1 Dec 12, 2024
8d2f86e
Define properties that match the keys from the elves.json
joheri1 Dec 12, 2024
b373da7
Add the important steps to get started with the project in the README…
joheri1 Dec 12, 2024
912a0bf
Add error handling for mongoose connections status
joheri1 Dec 12, 2024
6bbe8b2
Fix: Change mongoose.connect.readyState to mongoose.connection.readyS…
joheri1 Dec 12, 2024
8c32000
Fix: Add promise.all to make the Seed work
joheri1 Dec 12, 2024
b265e24
Fix: Change Name to name in the defined properties. Change comment to…
joheri1 Dec 12, 2024
f60fd07
Fix: Move the endpoint /elves/all outside of the documentation endpoint.
joheri1 Dec 12, 2024
d98529b
Fix: Resolve nesting issues, and add missing comma.
joheri1 Dec 12, 2024
9b7af4a
Add the endpoints for getting all elves, get the top 12 elves, filter…
joheri1 Dec 12, 2024
4d90157
Update code to fetch the Top TwElves
joheri1 Dec 13, 2024
b87f61e
Fix: Change typo from Eld to Elf
joheri1 Dec 13, 2024
ee4c4b3
Commit changes that prepares seeding from json file
joheri1 Dec 13, 2024
303114d
Delete all files in Data folder except the elves.json that I am using…
joheri1 Dec 13, 2024
746c0b0
Add async/await to fetch elfs by id from MongoDB
joheri1 Dec 14, 2024
0ce1a80
Fix: Change esponse to response.status(404)
joheri1 Dec 14, 2024
24e9ffa
Fix: Change name to Name in the defined properties to match the key i…
joheri1 Dec 14, 2024
c94ee0b
Update json file so that all elves has the key language, not language…
joheri1 Dec 14, 2024
f3027cd
Update Mongoose model for Name, and change all keys in the elves.json…
joheri1 Dec 14, 2024
63ec83f
Merge the endpoint /all, /top-twelves and /titles/:title to one endpo…
joheri1 Dec 14, 2024
f783cde
Update documentation endpoint
joheri1 Dec 14, 2024
584f8d8
Update README with all documentation needed to submit assignment
joheri1 Dec 14, 2024
2489144
Out-comment elves.json to ensure that nothing is read from that file.
joheri1 Dec 14, 2024
94bbbf6
Add import elves from elves.json
joheri1 Dec 14, 2024
c58b02f
Outcomment the import from the elves.json, and outcomment the seed lo…
joheri1 Dec 15, 2024
8d7fbf1
Remove deprecated Mongoose options useUnifiedTopology and useNewUrlPa…
joheri1 Dec 15, 2024
7cbbef7
Remove endpoint for json file. Update comments regarding Middelaware …
joheri1 Dec 15, 2024
91b73f8
Remove dist folder and create a new one for deployment.
joheri1 Dec 15, 2024
4642be7
Update variable for port
joheri1 Dec 15, 2024
0da7d71
Delete dist folder and create a new one
joheri1 Dec 15, 2024
50c0430
Delete fallback for variable mongoUrl
joheri1 Dec 15, 2024
965c827
Commit any unsaved changes
joheri1 Dec 15, 2024
44d08a7
Changed order in code and added more comments
joheri1 Dec 16, 2024
3579cd6
Create a new component for seedDatabase, and change the outcommented …
joheri1 Dec 16, 2024
5e2e1de
Change 404 error message for finding one elf by id to return a json o…
joheri1 Dec 16, 2024
36b717b
Update dist folder with a new one
joheri1 Dec 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 105 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,113 @@
# Project Mongo API

Replace this readme with your own information about your project.
This project builds on [Project Express API](https://github.com/joheri1/project-express-api) to create a RESTful API, and using MongoDB as a database. Mongoose is used to store, retrieve, and filter data more efficiently. The API leverages Mongoose methods instead of plain JavaScript for operations like filtering.

Start by briefly describing the assignment in a sentence or two. Keep it short and to the point.
## Requirements:
1. The API should have at least 3 routes.
This API has the following routes:
- "/": Returns documentation of the API using express-list-endpoints.[Express List Endpoints](https://www.npmjs.com/package/express-list-endpoints).
- "/elves": endpoint to return a collection of results (all elves or filter using query params e.g., ?title=backend dasher&top_twelves=true").
- "/elves/:id": Endpoint to return a single result (Get a specific elf by ID).
- "/test": Test endpoint.
2. The API should use Mongoose model.
This API uses Mongoose models to model the data and use these models to fetch data from the database.
```bash
const Elf = mongoose.model('Elf', {
"elfID": Number,
"title": String,
"name": String,
"language": [String],
"reviews_count": Number
```
3. The API should be RESTful
This API's RESTful examples:
- The /elves endpoint acts as a flexible entry point by the use of query parameters.
- Path parameters (/elves/:id) are used only for identifying unique resources.
- API returns appropriate HTTP status codes:
200 → Success (e.g., when data is found).
404 → Not Found (e.g., when an elf with the given ID does not exist).
500 → Internal Server Error (e.g., database issues).
- Uses GET for fetching data.
4. Follow the guidelines on how to write clean code.
This API's examples of clean code:
- Variable names are clear and descriptive (e.g. request, response, title).

## The problem
## Dependency Installation & Startup Development Server
This project uses npm (Node Package Manager) and Express.js to manage dependencies and run the development server. It uses MongoDB as the database and the Mongoose library for database interaction.

Describe how you approached to problem, and what tools and techniques you used to solve it. How did you plan? What technologies did you use? If you had more time, what would be next?
Follow these steps to get started:
1. Install Project Dependencies
Run the following commands to install necessary packages and set up the development environment:
```bash
npm install
```
2. Run in development mode: Use the following command during testing:
```bash
npm run dev
```
3. When preparing for production, build the project using:
```bash
npm run build
```
4. If Express.js is not already installed, initialize your project and install it:
```bash
npm init -y
npm install express
```
5. Start your server
Launch the server:
```bash
npm start
```
6. The package used to generate a list of all available API endpoints automatically (shown on the endpoint /). Install it with:
```bash
npm install express-list-endpoints
```
7. To start your local MongoDB, run:
```bash
mongod
```
8. Seed the database. To reset and seed the database with initial data, run:
```bash
RESET_DB=true npm start
```
9. Delete dist folder to update code
```bash
rm -rf dist
````

## .env files

```bash
npm install dotenv
```

## Useful resources
- [Mongoose Query objects](https://mongoosejs.com/docs/queries.html)
- [Use Regular Expression for case insensitivity](https://stackoverflow.com/questions/52073031/case-insensitive-key-name-match-mongoose)


## The problem
### Deploying to Render
When deploying to Render, I encountered some issues:
- The first issue was related to the MongoDB user I had created. The deployment failed due to an authentication error.
Solution: I deleted the user and created a new one.

- The second issue involved my dist folder not updating with the latest code. Despite rebuilding the project, the files in dist caused issues during deployment.
Solution: I used the following command to remove the folder everytime I wanted to deploy to Render:

```bash
rm -rf dist
```
Then, I rebuilt the project using:
```bash
npm run build
```
Then, I manually deployed the latest commit on Render.

## If I had more time
I started working on creating new components for the seeding logic, but it isn't completed yet. If I had more time, I would focus on finishing the seeding component and adding separate files for the Mongoose model and the Routes to make the application more modular and maintainable.

## View it live

Every project should be deployed somewhere. Be sure to include the link to the deployed project so that the viewer can click around and see what it's all about.
[Project Mongo API](https://project-mongo-api-x8p0.onrender.com)
Loading