Backend Challenge #2: E-commerce Product Catalog with File Uploads

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:

  1. Create Product: Add new product details (e.g., name, description, price) to the catalog.
  2. 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.
  3. Retrieve Product Details: Fetch a single product’s full details, including its name, price, and the URL where its associated image can be accessed.
  4. 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:

ActionHTTP Method & URLDescription
Create ProductPOST /api/productsAccepts product details (e.g., name, price, description) and creates a new product entry in the database.
Upload Product ImagePOST /api/products/{productId}/imageAccepts 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 DetailsGET /api/products/{productId}Retrieves the details for a single product, including its metadata and the URL to access its image.
Serve Product ImageGET /api/products/{productId}/imageServes 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 uploads directory).
  • 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 productId or invalid file uploads.

### Next Steps

Think you’ve got a working solution?

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top