This is the second challenge in our backend coding series, designed to test your ability to handle a crucial, real-world scenario: file uploads within a REST API. Beyond basic CRUD operations, you’ll need to integrate file system interactions with database persistence.
You can solve this backend coding challenge using any framework you choose, such as Spring Boot, Node.js (Express), Python (Django/Flask), or Ruby on Rails.
### 1. Problem Description
An e-commerce platform needs a backend API for managing its product catalog. The system must store product details (like name and price) and, crucially, allow for an associated image to be uploaded for each product. This image needs to be saved to the server’s file system, and its location linked to the product in the database.
Your API must robustly handle both structured product data and unstructured file data, ensuring they are correctly associated.
### 2. Functional Requirements
Your application must provide the following core functionalities:
- Create Product: Add new product details (e.g., name, description, price) to the catalog.
- Upload Product Image: Allow a user to upload an image file for an existing product. This image must be stored on the server’s file system, and its path or filename must be saved in the database, linked to the respective product.
- Retrieve Product Details: Fetch a single product’s full details, including its name, price, and the URL where its associated image can be accessed.
- Serve Product Image: The system must be able to serve the uploaded image file via an HTTP endpoint.
### 3. API Endpoint Specifications
To meet these requirements, you must implement the following RESTful API endpoints:
| Action | HTTP Method & URL | Description |
| Create Product | POST /api/products | Accepts product details (e.g., name, price, description) and creates a new product entry in the database. |
| Upload Product Image | POST /api/products/{productId}/image | Accepts a multipart file upload for an image and associates it with the product specified by {productId}. Stores the file on the server and its path in the database. |
| Get Product Details | GET /api/products/{productId} | Retrieves the details for a single product, including its metadata and the URL to access its image. |
| Serve Product Image | GET /api/products/{productId}/image | Serves the actual image file associated with the product specified by {productId}. |
Export to Sheets
### 4. Technical Guidelines & Constraints
- Database: Your solution must persist product metadata in a relational database (e.g., MySQL, PostgreSQL, H2). The image file path/name must be stored in the database.
- File Storage: Image files must be stored on the server’s local file system (e.g., in a dedicated
uploadsdirectory). - Architecture: Your code should be well-structured. Using a standard architectural pattern like MVC or a Layered Architecture is highly recommended.
- API Format: All data exchange (except for file uploads) must be in JSON format.
- Error Handling: The API should handle scenarios like a non-existent
productIdor invalid file uploads.
### Next Steps
Think you’ve got a working solution?
- Compare your work with our official Solution for the E-commerce Product Catalog Challenge.
- Ready for more? Try our Backend Coding Challenge #1: Design a Library Catalog API.