Day 8 of 100daysofcode : CRUD Operations
Today, I delved into CRUD operations (Create, Read, Update, Delete) and how they power the interaction between the frontend and backend in my outdoor adventure gear store project. Here’s a closer look at how each operation works and connects the two sides:
1-Create (Adding New User):
-Frontend: I built a Sign-up form using React. When a user fills in regististration details like username, email, phone number, and password, the form data is validated and submitted.
-Backend: A POST request is sent to the server’s /api/users endpoint. The backend (Node.js and Express) processes the request, validates the data, and inserts a new user document into MongoDB.
-Database: MongoDB generates a unique ID for the user and stores it with all the submitted details.
2-Read (Displaying Products):
-Frontend: Users can browse products on the store’s product page. I used React to dynamically display product data in cards with options to filter and search.
-Backend: When the frontend sends a GET request to /api/products, the server queries MongoDB for the product list. The results are sent back as JSON.
-Integration: React uses the fetched data to render a responsive UI, ensuring users see updated products immediately.
3-Update (Editing Product Details):
-Frontend: Admins can select a product to edit, triggering a pre-filled form with the product’s current details. Upon submitting the updated data, a PUT request is sent to /api/products/:id.
-Backend: The server retrieves the product by its ID, updates the necessary fields in the MongoDB document, and responds with the updated product data.
-Integration: The frontend reflects the changes immediately, ensuring the UI stays in sync with the database.
4-Delete (Removing Products):
-Frontend: Admins can click a Delete button on a product card, confirming their action through a dialog box. This triggers a DELETE request to /api/products/:id.
-Backend: The server locates the product in MongoDB using its ID and removes it from the database.
-Integration: The frontend dynamically removes the deleted product from the displayed list without requiring a page refresh.
Key Learnings:
Properly structured API endpoints are crucial for seamless communication between the frontend and backend.
Using state management in React ensures data updates are reflected in real time.
Error handling at both the frontend and backend ensures users are notified of any issues.
CRUD operations are the backbone of any dynamic application, and building them has deepened my understanding of how the frontend and backend collaborate to manage data. It’s exciting to see how each part fits together!
100daysofcode lebanon-mug