This project implements a simple service for managing collections of Fruits and Vegetables. The service supports adding, removing, listing, and querying produce items with filtering and sorting options.
Units are normalized to grams internally, while input/output can be expressed in either grams or kilograms.
-
Process a
request.jsonpayload and separate items into Fruits and Vegetables collections. -
Each collection supports:
add()remove()list()
-
All quantities stored as grams.
-
Storage engine: In-memory (can be swapped with DB in future).
-
API Endpoints:
-
POST /api/produce→ add new item to Fruits or Vegetables collection. -
GET /api/produce→ list items with optional filters:type=fruit|vegetablesearch=<name>sortBy=kg|namedirection=asc|descpage,perPage
-
-
Bonus features:
- Option to return results in kilograms or grams.
- Basic
search()support. - Built with Symfony (latest).
# Clone the repo
git clone [email protected]:UnnikrishnanBhargavakurup/fruits-and-vegetables-challenge.git
cd fruits-and-vegetables-challenge
# Build docker image
docker build -t tturkowski/fruits-and-vegetables -f docker/Dockerfile .
# Install dependencies (inside container)
docker run --rm -it -w /app -v "$(pwd)":/app tturkowski/fruits-and-vegetables composer installdocker run --rm -it -w /app -v "$(pwd)":/app tturkowski/fruits-and-vegetables ./vendor/bin/phpunitdocker run -it -w/app -v$(pwd):/app -p8080:8080 tturkowski/fruits-and-vegetables php -S 0.0.0.0:8080 -t /app/public
# Open http://127.0.0.1:8080 in your browsercurl -X POST http://127.0.0.1:8080/api/produce \
-H "Content-Type: application/json" \
-d '{
"id": 18,
"name": "Kiwi",
"type": "fruit",
"quantity": 10,
"unit": "kg"
}'curl -X GET "http://127.0.0.1:8080/api/produce?type=fruit&search=kiwi&sortBy=kg&direction=desc&page=1&perPage=10" \
-H "Accept: application/json"- Duplicate IDs are not validated (in-memory storage, no persistence).
- Quantities: request.json provides
kgas integers; internally stored in grams. - Authentication: not implemented (open API).
- Linting: not configured (out of scope).
- Service is timeboxed to 3–4 hours for demonstration purposes.
- KISS, DRY, YAGNI, SOLID applied.
- Clean domain model with minimal duplication.
- No business logic in controllers.
- PHPUnit tests included.