Skip to content

Implement PATCH method for partial updates #461

@nanotaboada

Description

@nanotaboada

Problem

The API supports PUT for player updates, which requires sending the complete player payload even when only one or two fields need to change. A PATCH endpoint would allow callers to update specific fields without providing the entire resource.

Proposed Solution

Add PATCH /players/squadnumber/{squad_number} for partial updates.

  • All fields are patchable except squad_number (natural key, present in the URL path) and id (UUID surrogate key).
  • Only the fields present in the request body are updated; absent fields retain their current values.
  • If the request body includes squad_number or id, the endpoint returns 400 Bad Request.

Suggested Approach

  1. Model — Add PlayerPatchRequestModel in models/player_model.py with all patchable fields as Optional[T] = None. Exclude squad_number and id. Use model_config = ConfigDict(extra="forbid") is optional; alternatively, explicitly check for forbidden fields in the route.
  2. Service — Add async def patch_by_squad_number_async(async_session, squad_number, patch_model) in services/player_service.py. Use patch_model.model_dump(exclude_unset=True) to get only the provided fields and apply them.
  3. Route — Add @api_router.patch("/players/squadnumber/{squad_number}", status_code=status.HTTP_204_NO_CONTENT) in routes/player_route.py. Check for forbidden fields → 400. Look up player → 404 if missing. Invalidate cache on success.
  4. Tests — Add tests in tests/test_main.py following the existing naming pattern.

Acceptance Criteria

  • PATCH /players/squadnumber/{squad_number} is implemented
  • All fields except squad_number and id are patchable
  • Fields absent from the request body are left unchanged
  • Returns 204 No Content on success
  • Returns 400 Bad Request if the body contains squad_number or id
  • Returns 400 Bad Request on field validation failure
  • Returns 404 Not Found when no player has that squad number
  • Cache is invalidated after a successful patch
  • Tests added following existing naming conventions
  • All existing tests continue to pass
  • CHANGELOG.md updated

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestpriority:mediumPlanned enhancement. Queue for upcoming work.pythonPull requests that update Python code

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions