Abheek Ranjan Das
Aravinda Reddy Gangalakunta
- Gokul Reddy Devarapalli
- Ramgopal Hyndav Bollepalli
- Danny Bylla
This project enhances our photo sharing application by integrating MongoDB for data persistence, allowing dynamic content management. We've encapsulated the environment setup using Docker Compose for ease of development and deployment.
- Docker and Docker Compose optional
- Node.js (for local development)
- Docker Compose Setup: To spin up the MongoDB database , simply run:
docker-compose up -dThis command starts all required services as defined in our docker-compose.yml file, including the MongoDB instance and express.
You should see containers for both the MongoDB database and express.
With the integration of MongoDB, our app's API now directly interacts with a live database, enhancing data management and scalability. Here's a brief overview:
-
User List (
/user/list): Fetches a list of all users from the MongoDBUsercollection, displaying essential details like name and occupation. -
User Details (
/user/:id): Retrieves detailed information about a specific user by their ID, including their photos and comments. -
Photos of User (
/photosOfUser/:id): Returns all photos uploaded by a specific user, along with comments on each photo, leveraging MongoDB for data retrieval.
app.get("/user/:id", async (request, response) => {
try {
const user = await User.findById(request.params.id);
if (!user) {
return response.status(404).send("User not found");
}
response.json(user);
} catch (error) {
response.status(500).send("Internal Server Error");
}
});This endpoint demonstrates how we interact with MongoDB to fetch and return user details, ensuring a dynamic and responsive application experience.
In this problem, we need to convert the web server of our photo sharing application to utilize MongoDB for data storage and retrieval. Currently, the application's model fetching routes use "magic models" rather than a database. Our task is to update these routes to interact with MongoDB and ensure that there are no accesses to models in the code.
- Modify the existing routes to fetch data from MongoDB instead of using magic models.
- Implement four specific API endpoints:
/test: Return schema info and object counts of the database for testing purposes./user/list: Return a list of users' models appropriate for the navigation sidebar list./user/:id: Return detailed information of a user specified by their ID./photosOfUser/:id: Return photos of the user with the specified ID, along with comments on each photo.
- Ensure that the GET requests return the required information for the UI without accessing models directly.
- Handle errors and edge cases appropriately, including invalid requests and database errors.
- Do not alter the database schema.
- The MongoDB system returns models from objects stored in the database, while the request should return the data models needed by the Photo App views. Special care must be taken to align these models properly.
- Utilize processing techniques to assemble the necessary model data for the front end.
- Avoid directly modifying Mongoose models to match front end requirements. Instead, create copies of the Mongoose model objects.
In this problem, we need to convert our photo app to use axios for fetching data from the web server instead of the FetchModel routine. Axios is a faster and more functional alternative for making HTTP requests.
- Replace all instances of FetchModel with axios.get in the photo app.
- Ensure that the app's functionality remains unchanged before and after the switch to axios.
- Import axios appropriately in components where data fetching is performed.
- Attach success and failure handlers using .then and .catch respectively.
- Handle both success and error cases for requests.
- Axios offers enhanced functionality and performance compared to FetchModel, making it a suitable choice for future development.
- Carefully transition each data-fetching component to use axios while ensuring that the app's behavior remains consistent.