Skip to content

UnnikrishnanBhargavakurup/fruits-and-vegetables-challenge

 
 

Repository files navigation

🍎🥕 Fruits and Vegetables Service

🎯 Overview

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.


✨ Features Implemented

  • Process a request.json payload 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|vegetable
      • search=<name>
      • sortBy=kg|name
      • direction=asc|desc
      • page, perPage
  • Bonus features:

    • Option to return results in kilograms or grams.
    • Basic search() support.
    • Built with Symfony (latest).

🚀 Running the Project

1. Clone & Build Docker Image

# 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 install

2. Run Tests

docker run --rm -it -w /app -v "$(pwd)":/app tturkowski/fruits-and-vegetables ./vendor/bin/phpunit

3. Start Development Server

docker 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 browser

📡 API Examples

➕ Add Produce

curl -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"
  }'

📋 List Produce with Filters

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"

🧾 Assumptions & Notes

  • Duplicate IDs are not validated (in-memory storage, no persistence).
  • Quantities: request.json provides kg as 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.

✅ Development Principles

  • KISS, DRY, YAGNI, SOLID applied.
  • Clean domain model with minimal duplication.
  • No business logic in controllers.
  • PHPUnit tests included.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 97.7%
  • Dockerfile 2.3%