Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package edu.csumb.cst438.api.controller;

import edu.csumb.cst438.api.record.MealPlanCreate;
import edu.csumb.cst438.api.record.MealPlanEntryCreate;
import edu.csumb.cst438.api.record.MealPlanEntryPatch;
import edu.csumb.cst438.api.record.MealPlanEntryResponse;
import edu.csumb.cst438.api.record.MealPlanPatch;
import edu.csumb.cst438.api.record.MealPlanResponse;
import edu.csumb.cst438.api.service.MealPlanService;
Expand All @@ -13,6 +16,7 @@

import java.net.URI;
import java.security.Principal;
import java.util.List;

/**
* API routes for the signed-in user's weekly meal plans, under /api/v1/meal-plans.
Expand Down Expand Up @@ -101,4 +105,63 @@ public ResponseEntity<Void> deleteMealPlan(Principal principal, @PathVariable Lo
mealPlanService.delete(principal.getName(), mealPlanId);
return ResponseEntity.noContent().build();
}

/**
* Lists the planned meals in a meal plan.
*
* @param principal the signed-in user
* @param mealPlanId the meal plan id
* @return 200 with the entries
*/
@GetMapping("/{mealPlanId}/entries")
public List<MealPlanEntryResponse> listMealPlanEntries(Principal principal, @PathVariable Long mealPlanId) {
return mealPlanService.listEntries(principal.getName(), mealPlanId);
}

/**
* Adds a recipe to a meal plan.
*
* @param principal the signed-in user
* @param mealPlanId the meal plan id
* @param body the recipe, date, and meal type
* @return 201 with the new entry and its Location
*/
@PostMapping("/{mealPlanId}/entries")
public ResponseEntity<MealPlanEntryResponse> addMealPlanEntry(Principal principal, @PathVariable Long mealPlanId,
@Valid @RequestBody MealPlanEntryCreate body) {
MealPlanEntryResponse created = mealPlanService.addEntry(principal.getName(), mealPlanId, body);
return ResponseEntity.created(
URI.create("/api/v1/meal-plans/" + mealPlanId + "/entries/" + created.id())).body(created);
}

/**
* Changes the fields that were sent on one planned meal.
*
* @param principal the signed-in user
* @param mealPlanId the meal plan id
* @param entryId the entry id
* @param body the fields to change, at least one
* @return 200 with the updated entry
*/
@PatchMapping("/{mealPlanId}/entries/{entryId}")
public MealPlanEntryResponse updateMealPlanEntry(Principal principal, @PathVariable Long mealPlanId,
@PathVariable Long entryId,
@Valid @RequestBody MealPlanEntryPatch body) {
return mealPlanService.updateEntry(principal.getName(), mealPlanId, entryId, body);
}

/**
* Removes one planned meal. The recipe is kept.
*
* @param principal the signed-in user
* @param mealPlanId the meal plan id
* @param entryId the entry id
* @return 204 with no body
*/
@DeleteMapping("/{mealPlanId}/entries/{entryId}")
public ResponseEntity<Void> deleteMealPlanEntry(Principal principal, @PathVariable Long mealPlanId,
@PathVariable Long entryId) {
mealPlanService.deleteEntry(principal.getName(), mealPlanId, entryId);
return ResponseEntity.noContent().build();
}
}
24 changes: 21 additions & 3 deletions api/src/main/java/edu/csumb/cst438/api/entity/MealPlan.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import jakarta.persistence.*;
import lombok.Getter;
import org.hibernate.Hibernate;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -62,7 +64,8 @@ public void addMealPlanEntry(MealPlanEntry mealPlanEntry){
if(mealPlanEntry == null){
throw new IllegalArgumentException("Meal plan entry must not be null.");
}
if(mealPlanEntry.getMealPlan() != null && mealPlanEntry.getMealPlan() != this){
// unproxy: a lazy-loaded getter returns a Hibernate proxy, which is never == this
if(mealPlanEntry.getMealPlan() != null && Hibernate.unproxy(mealPlanEntry.getMealPlan()) != this){
throw new IllegalStateException("A meal plan entry cannot be transferred between meal plans.");
}
mealPlanEntries.add(mealPlanEntry);
Expand All @@ -77,14 +80,15 @@ public void addMealPlanEntry(MealPlanEntry mealPlanEntry){
* @throws IllegalArgumentException if the entry doesn't belong to this meal plan or if the entry is null
*/
public void removeMealPlanEntry(MealPlanEntry mealPlanEntry){
if(mealPlanEntry == null || mealPlanEntry.getMealPlan() != this){
if(mealPlanEntry == null || Hibernate.unproxy(mealPlanEntry.getMealPlan()) != this){
throw new IllegalArgumentException("Meal plan entry does not belong to meal plan.");
}
mealPlanEntries.remove(mealPlanEntry);
}

/**
* Moves this meal plan to a different week. Entries are left unchanged.
* Moves this meal plan to a different week. Entries move by the same number of days,
* so they stay on the same weekday inside the plan's week.
*
* @param weekStart the new week start, cannot be null
* @throws IllegalArgumentException if weekStart is null
Expand All @@ -93,9 +97,23 @@ public void changeWeekStart(LocalDate weekStart){
if(weekStart == null){
throw new IllegalArgumentException("Meal plans must have a starting week.");
}
long days = ChronoUnit.DAYS.between(this.weekStart, weekStart);
for(MealPlanEntry entry : mealPlanEntries){
entry.shiftPlannedDate(days);
}
this.weekStart = weekStart;
}

/**
* Checks whether a date is in this meal plan's week, weekStart through the 6 days after it.
*
* @param date the date to check
* @return true if the date is in the week
*/
public boolean coversDate(LocalDate date){
return !date.isBefore(weekStart) && date.isBefore(weekStart.plusDays(7));
}

/**
* Called by User.addMealPlan owning side cannot get out of sync similar to the recipe table.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,73 @@ protected MealPlanEntry(){}
* @param recipe the recipe this entry contains
* @param planned_date the date the meal is planned to be made
* @param meal_type the type of meal
* @throws IllegalArgumentException if anything inputted is null
* @throws IllegalArgumentException if anything inputted is null or the date isn't in the meal plan's week
*/
public MealPlanEntry(MealPlan meal_plan, Recipe recipe, LocalDate planned_date, mealType meal_type){
if(meal_plan == null || recipe == null || planned_date == null || meal_type == null){
throw new IllegalArgumentException("Meal plan entries must have a meal plan, recipe, planned date, and meal type.");
}
requireInWeek(meal_plan, planned_date);
meal_plan.addMealPlanEntry(this);
recipe.addMealPlanEntry(this);
plannedDate = planned_date;
mealType = meal_type;
}

/**
* Moves this entry to a different recipe and keeps both recipes' entry collections in sync.
*
* @param recipe the new recipe, cannot be null
* @throws IllegalArgumentException if recipe is null
*/
public void changeRecipe(Recipe recipe){
if(recipe == null){
throw new IllegalArgumentException("Meal plan entries must have a recipe.");
}
this.recipe.removeMealPlanEntry(this);
this.recipe = null;
recipe.addMealPlanEntry(this);
}

/**
* Changes the date the meal is planned for.
*
* @param plannedDate the new date, cannot be null
* @throws IllegalArgumentException if plannedDate is null or isn't in the meal plan's week
*/
public void changePlannedDate(LocalDate plannedDate){
if(plannedDate == null){
throw new IllegalArgumentException("Meal plan entries must have a planned date.");
}
requireInWeek(mealPlan, plannedDate);
this.plannedDate = plannedDate;
}

/**
* Called by MealPlan.changeWeekStart so entries move with their week.
*/
void shiftPlannedDate(long days){plannedDate = plannedDate.plusDays(days);}

private static void requireInWeek(MealPlan mealPlan, LocalDate plannedDate){
if(!mealPlan.coversDate(plannedDate)){
throw new IllegalArgumentException("Planned date " + plannedDate + " must be within the week starting "
+ mealPlan.getWeekStart() + ".");
}
}

/**
* Changes the type of meal.
*
* @param mealType the new meal type, cannot be null
* @throws IllegalArgumentException if mealType is null
*/
public void changeMealType(mealType mealType){
if(mealType == null){
throw new IllegalArgumentException("Meal plan entries must have a meal type.");
}
this.mealType = mealType;
}

/**
* Called by MealPlan.addMealPlanEntry so the mealPlan side cannot get out of sync.
*/
Expand Down
6 changes: 4 additions & 2 deletions api/src/main/java/edu/csumb/cst438/api/entity/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.persistence.*;
import lombok.Getter;
import org.hibernate.Hibernate;
import org.hibernate.annotations.CreationTimestamp;

import java.time.Instant;
Expand Down Expand Up @@ -147,7 +148,8 @@ public void addMealPlanEntry(MealPlanEntry mealPlanEntry){
if(mealPlanEntry == null){
throw new IllegalArgumentException("Meal plan entry must not be null.");
}
if(mealPlanEntry.getRecipe() != null && mealPlanEntry.getRecipe() != this){
// unproxy: a lazy-loaded getter returns a Hibernate proxy, which is never == this
if(mealPlanEntry.getRecipe() != null && Hibernate.unproxy(mealPlanEntry.getRecipe()) != this){
throw new IllegalStateException("A meal plan entry cannot be transferred between recipes.");
}
mealPlanEntries.add(mealPlanEntry);
Expand All @@ -162,7 +164,7 @@ public void addMealPlanEntry(MealPlanEntry mealPlanEntry){
* @throws IllegalArgumentException if the entry doesn't belong to this recipe or if the entry is null
*/
public void removeMealPlanEntry(MealPlanEntry mealPlanEntry){
if(mealPlanEntry == null || mealPlanEntry.getRecipe() != this){
if(mealPlanEntry == null || Hibernate.unproxy(mealPlanEntry.getRecipe()) != this){
throw new IllegalArgumentException("Meal plan entry does not have this recipe.");
}
mealPlanEntries.remove(mealPlanEntry);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package edu.csumb.cst438.api.record;

import edu.csumb.cst438.api.entity.mealType;
import jakarta.validation.constraints.NotNull;

import java.time.LocalDate;

/**
* Body for POST /api/v1/meal-plans/{mealPlanId}/entries.
*
* @param recipeId the recipe to plan
* @param plannedDate the date the meal is planned for, within the meal plan's week
* @param mealType breakfast, lunch, or dinner
*/
public record MealPlanEntryCreate(@NotNull Long recipeId, @NotNull LocalDate plannedDate, @NotNull mealType mealType) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package edu.csumb.cst438.api.record;

import edu.csumb.cst438.api.entity.mealType;
import jakarta.validation.constraints.AssertTrue;

import java.time.LocalDate;

/**
* Body for PATCH /api/v1/meal-plans/{mealPlanId}/entries/{entryId}.
* Only the non-null fields are changed. An unknown mealType fails JSON parsing, which is a 400.
*
* @param recipeId the new recipe
* @param plannedDate the new planned date
* @param mealType the new meal type
*/
public record MealPlanEntryPatch(Long recipeId, LocalDate plannedDate, mealType mealType) {

/**
* The contract requires at least one property (minProperties: 1).
*
* @return true if at least one field was sent
*/
@AssertTrue(message = "at least one field must be provided")
public boolean isNotEmpty() {
return recipeId != null || plannedDate != null || mealType != null;
}
}
Loading
Loading