Skip to content

Feature: Add GET /items/:id endpoint to retrieve a single item #8

Description

@manish-jangra

Description

The API currently supports GET /items (list all), POST /items (create), PATCH /items/:id (update), and DELETE /items/:id (delete) — but there is no way to retrieve a single item by its ID.

Adding GET /items/:id is a standard REST convention and would allow clients to fetch details of a specific todo item without downloading the entire list.

Use Case

  • A client that only needs to display or verify one item shouldn't have to fetch all items and filter client-side.
  • Follows RESTful resource design where every resource has its own URI.
  • Useful for confirming an item's state after a PATCH operation or for deep-linking.

Proposed Implementation

@app.route("/items/<int:item_id>", methods=["GET"])
def get_item(item_id):
    items = _get_items()
    item = next((i for i in items if i["id"] == item_id), None)
    if item is None:
        return jsonify(error="item not found"), 404
    return jsonify(item)

Proposed Tests

def test_get_item(client):
    client.post("/items", json={"name": "My Task"})
    resp = client.get("/items/1")
    assert resp.status_code == 200
    data = resp.get_json()
    assert data["name"] == "My Task"
    assert data["id"] == 1

def test_get_item_not_found(client):
    resp = client.get("/items/999")
    assert resp.status_code == 404

Acceptance Criteria

  • GET /items/:id returns the item as JSON with status 200
  • Returns 404 with {"error": "item not found"} if the ID doesn't exist
  • Unit tests cover both the happy path and not-found case
  • README is updated with the new endpoint

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions