Skip to content

Add, view, change, and remove meal plan entries - #60

Merged
annabelleb20 merged 7 commits into
mainfrom
doyoung/meal-del
Oct 22, 2026
Merged

annabelleb20 merged 7 commits into
mainfrom
doyoung/meal-del

Conversation

@Yooze0x4c8

Copy link
Copy Markdown
Collaborator

Closes #59

What changed

Adds the meal plan entry endpoints, so users can put recipes into a meal plan and manage them.

New endpoints

  • POST /api/v1/meal-plans/{mealPlanId}/entries: adds a recipe to a meal plan. Returns 201 with a Location header. All of recipeId, plannedDate, and mealType are required.
  • GET /api/v1/meal-plans/{mealPlanId}/entries: lists the meal plan's entries.
  • PATCH /api/v1/meal-plans/{mealPlanId}/entries/{entryId}: changes only the fields sent. An empty body is 400.
  • DELETE /api/v1/meal-plans/{mealPlanId}/entries/{entryId}: returns 204. The recipe is kept.

Rules

  • An entry's recipe must be the user's own or a prepopulated one. Another user's recipe or a missing recipe returns 404.
  • A meal plan or entry that doesn't exist, isn't in that meal plan, or belongs to another user returns 404, so the API never reveals other users' data.
  • ⚠️ New rule (needs team agreement): plannedDate must be within the meal plan's week (weekStart through weekStart + 6 days). Otherwise POST and PATCH return 400. It is checked in the MealPlanEntry entity, so every path follows it.
  • ⚠️ Behavior change: PATCH /api/v1/meal-plans/{id} with a new weekStart now moves the plan's entries by the same number of days, so they stay on the same weekday inside the new week. Before, entries were left unchanged.

Bug fix in existing code (Recipe.java, MealPlan.java)
add/removeMealPlanEntry compared entry.getRecipe() / entry.getMealPlan() to this with ==. When the entry is loaded from the database, the getter returns a Hibernate lazy proxy, not the real entity, so the check always failed and threw IllegalArgumentException (500 in the running app). Existing tests built entities with new, so no proxy was involved and the bug never showed up. The fix compares against Hibernate.unproxy(...). MealPlanServiceJpaTest now reloads entities from the database and covers this.

Other

  • MealPlanEntry: added changeRecipe, changePlannedDate, and changeMealType.
  • docs/openapi.yaml: documented the week rule on plannedDate.
  • MealPlanEntryPersistenceTest: changed dates that were outside the plan's week.

How I tested it

  • Unit tests pass locally (./gradlew test, 109 tests, 0 failures)
  • If the API contract changed, docs/openapi.yaml is updated and lints clean (npx @redocly/cli lint docs/openapi.yaml)

New tests:

  • MealPlanServiceTest: partial updates, moving to another recipe, rejected recipes, entry not in the plan, the week boundary (day 0 and day 6 are allowed, day -1 and day 7 are not), and entries moving when the week changes.
  • MealPlanServiceJpaTest (real database): deleting an entry keeps the recipe, changing the recipe after reloading from the database, and a new entry saved with a generated id.
  • MealPlanControllerTest: 200/201/204 responses, the Location header, 400 for missing or invalid fields, an empty PATCH body, or a date outside the week, 404 as Problem Details, and 401 without a token.

Reviewers

2 approving teammates who are not the author (1 for a team of 3).

annabelleb20
annabelleb20 previously approved these changes Oct 22, 2026

@annabelleb20 annabelleb20 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.

Overall I think this looks great, I like how you wrote the additional jpa tests and I think everything looks fine in the code. Personally I'm fine with the new rule and behavior change with the mealplan entry stuff, I think it makes sense and works for our project.

// A user can plan their own recipes and the prepopulated ones; anything else is hidden as 404.
private Recipe findUsableRecipe(User owner, Long recipeId) {
return recipeRepository.findById(recipeId)
.filter(r -> r.isPrepopulated() || r.getOwner() == owner)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This currently compares the Java objects. This could cause the users own recipe to be rejected with a 404 error. I think it would be better to compare the owner id or by recipe id and the owner.

@leslyVillanueva1020 leslyVillanueva1020 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.

Looks good

@Solian21 Solian21 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Nice fix. This looks good to me!

@annabelleb20 annabelleb20 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.

The fixes look good to me

@annabelleb20
annabelleb20 merged commit 41ea87a into main Oct 22, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

As a user, I want to add, view, change, and remove planned meals so that my meal plan stays accurate

4 participants