Skip to content

feat: add JWT-protected entries CRUD - #38

Merged
Andreas7274 merged 2 commits into
mainfrom
feat/entries-crud
Nov 25, 2026
Merged

Andreas7274 merged 2 commits into
mainfrom
feat/entries-crud

Conversation

@Andreas7274

@Andreas7274 Andreas7274 commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Adds JWT-protected CRUD operations for entries.

Changes:
-Add create, list, get, update and delete endpoints for /entries
-Restrict all operations to the authenticated user's own entries
-Add schemas for entry creation, updates and responses
-Hide internal fields such as user_id and photo_path
-Add success, authentication and ownership tests

Testing:
Ruff and all the pytest tests pass locally.

Closes #22

@DarynaBb DarynaBb left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work: ownership checks are solid (filtering by user_id in the query plus returning 404 for other users' entries is the right call), and the tests cover the main cases well.

One real bug before merge (PATCH with null → 500, see the inline comment). The rest are small suggestions.

Comment thread backend/app/schemas/entry.py Outdated
days: int | None = Field(default=None, ge=0)

class EntryUpdate(BaseModel):
kind: Literal["receipt", "trip"] | None = None

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: PATCH with explicit null crashes with 500.

kind, entry_date and category are NOT NULL in the DB, but EntryUpdate accepts null for them. Because of exclude_unset=True, an explicitly sent null is set, so it goes straight into setattr and the commit fails:

PATCH /entries/1  {"kind": null}
→ IntegrityError: NOT NULL constraint failed: entries.kind  (500)

I checked locally: the same happens for category and entry_date.

One way to fix it: keep the fields optional (so they can be omitted) but reject explicit null:

from pydantic import field_validator

class EntryUpdate(BaseModel):
    ...

    @field_validator("kind", "entry_date", "category")
    @classmethod
    def not_null(cls, value):
        if value is None:
            raise ValueError("must not be null")
        return value

Validators don't run on defaults, so omitted fields still work and null returns 422. A test for this case would also help.

Comment thread backend/app/schemas/entry.py Outdated
kind: Literal["receipt", "trip"] | None = None
amount: int | None = Field(default=None, ge=0)
entry_date: date | None = None
category: Literal[

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: the kind/category literals are copied 3 times here (and once more in the DB check constraint). Consider defining them once, e.g. EntryKind = Literal["receipt", "trip"] and EntryCategory = Literal[...], then reusing them. Otherwise adding a category means editing several places, and it's easy to miss one.

Comment thread backend/app/routers/entries.py Outdated
current_user: CurrentUserDependency,
):
entry = session.exec(
select(Entry).where(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this "select by id + user_id, else 404" block appears in get, update and delete. A small helper such as get_own_entry_or_404(session, entry_id, user) would remove the duplication and keep the ownership check in one place.

@Andreas7274

Copy link
Copy Markdown
Collaborator Author

Addressed the PATCH null handling issue and the suggested cleanup:

-Required entry fields now reject explicit null with 422
-Added tests for nullable and non-nullable update fields
-Reused EntryKind and EntryCategory types
-Centralized entry ownership lookup in get_own_entry_or_404

Ruff and the full pytest suite pass locally.

@Andreas7274
Andreas7274 merged commit 0c9c805 into main Nov 25, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Sprint 2][B] CRUD /entries with JWT

2 participants