Added getStar to RecipeController and related.

This commit is contained in:
Jesse Brault 2024-08-13 11:43:21 -05:00
parent c467bd75b3
commit f47e767612
5 changed files with 58 additions and 8 deletions

View File

@ -150,6 +150,23 @@ public class RecipeControllerTests {
.andExpect(jsonPath("$.date").exists()); .andExpect(jsonPath("$.date").exists());
} }
@Test
@DirtiesContext
public void getStarForRecipe() throws Exception {
final User owner = this.createTestUser("recipe-owner");
final User starer = this.createTestUser("recipe-starer");
final Recipe recipe = this.createTestRecipe(owner, true);
this.recipeStarService.create(recipe.getId(), starer.getUsername());
this.mockMvc.perform(
get("/recipes/{username}/{slug}/stars", recipe.getOwner().getUsername(), recipe.getSlug())
.header("Authorization", "Bearer " + this.getAccessToken(starer))
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.isStarred").value(true))
.andExpect(jsonPath("$.star").isMap())
.andExpect(jsonPath("$.star.date").exists());
}
@Test @Test
@DirtiesContext @DirtiesContext
public void deleteStarFromRecipe() throws Exception { public void deleteStarFromRecipe() throws Exception {

View File

@ -1,11 +1,13 @@
package app.mealsmadeeasy.api.recipe.star; package app.mealsmadeeasy.api.recipe.star;
import app.mealsmadeeasy.api.recipe.Recipe; import app.mealsmadeeasy.api.recipe.Recipe;
import app.mealsmadeeasy.api.recipe.RecipeException;
import app.mealsmadeeasy.api.recipe.RecipeService; import app.mealsmadeeasy.api.recipe.RecipeService;
import app.mealsmadeeasy.api.recipe.spec.RecipeCreateSpec; import app.mealsmadeeasy.api.recipe.spec.RecipeCreateSpec;
import app.mealsmadeeasy.api.user.User; import app.mealsmadeeasy.api.user.User;
import app.mealsmadeeasy.api.user.UserCreateException; import app.mealsmadeeasy.api.user.UserCreateException;
import app.mealsmadeeasy.api.user.UserService; import app.mealsmadeeasy.api.user.UserService;
import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
@ -72,6 +74,21 @@ public class RecipeStarServiceTests {
assertThat(star.getDate(), is(notNullValue())); assertThat(star.getDate(), is(notNullValue()));
} }
@Test
@DirtiesContext
public void find() throws RecipeException {
final User owner = this.getTestUser("recipe-owner");
final User starer = this.getTestUser("recipe-starer");
final Recipe recipe = this.getTestRecipe(owner, "test-recipe", true);
this.recipeStarService.create(recipe.getId(), starer.getUsername());
final @Nullable RecipeStar star = this.recipeStarService.find(
recipe.getOwner().getUsername(),
recipe.getSlug(),
starer
).orElse(null);
assertThat(star, is(notNullValue()));
}
@Test @Test
@DirtiesContext @DirtiesContext
public void deleteViaUsernameAndSlug() { public void deleteViaUsernameAndSlug() {

View File

@ -6,6 +6,7 @@ import app.mealsmadeeasy.api.recipe.view.FullRecipeView;
import app.mealsmadeeasy.api.recipe.view.RecipeExceptionView; import app.mealsmadeeasy.api.recipe.view.RecipeExceptionView;
import app.mealsmadeeasy.api.recipe.view.RecipeInfoView; import app.mealsmadeeasy.api.recipe.view.RecipeInfoView;
import app.mealsmadeeasy.api.user.User; import app.mealsmadeeasy.api.user.User;
import org.jetbrains.annotations.Nullable;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice; import org.springframework.data.domain.Slice;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@ -74,6 +75,20 @@ public class RecipeController {
return ResponseEntity.status(HttpStatus.CREATED).body(this.recipeStarService.create(username, slug, principal)); return ResponseEntity.status(HttpStatus.CREATED).body(this.recipeStarService.create(username, slug, principal));
} }
@GetMapping("/{username}/{slug}/stars")
public ResponseEntity<Map<String, Object>> getStar(
@PathVariable String username,
@PathVariable String slug,
@AuthenticationPrincipal User principal
) throws RecipeException {
final @Nullable RecipeStar star = this.recipeStarService.find(username, slug, principal).orElse(null);
if (star != null) {
return ResponseEntity.ok(Map.of("isStarred", true, "star", star));
} else {
return ResponseEntity.ok(Map.of("isStarred", false));
}
}
@DeleteMapping("/{username}/{slug}/stars") @DeleteMapping("/{username}/{slug}/stars")
public ResponseEntity<Object> removeStar( public ResponseEntity<Object> removeStar(
@PathVariable String username, @PathVariable String username,

View File

@ -3,10 +3,14 @@ package app.mealsmadeeasy.api.recipe.star;
import app.mealsmadeeasy.api.recipe.RecipeException; import app.mealsmadeeasy.api.recipe.RecipeException;
import app.mealsmadeeasy.api.user.User; import app.mealsmadeeasy.api.user.User;
import java.util.Optional;
public interface RecipeStarService { public interface RecipeStarService {
RecipeStar create(long recipeId, String ownerUsername); RecipeStar create(long recipeId, String ownerUsername);
RecipeStar create(String recipeOwnerUsername, String recipeSlug, User starer) throws RecipeException; RecipeStar create(String recipeOwnerUsername, String recipeSlug, User starer) throws RecipeException;
RecipeStar get(long recipeId, String ownerUsername) throws RecipeException;
Optional<RecipeStar> find(String recipeOwnerUsername, String recipeSlug, User starer) throws RecipeException;
void delete(long recipeId, String ownerUsername); void delete(long recipeId, String ownerUsername);
void delete(String recipeOwnerUsername, String recipeSlug, User starer) throws RecipeException; void delete(String recipeOwnerUsername, String recipeSlug, User starer) throws RecipeException;
} }

View File

@ -45,13 +45,10 @@ public class RecipeStarServiceImpl implements RecipeStarService {
} }
@Override @Override
public RecipeStar get(long recipeId, String ownerUsername) throws RecipeException { public Optional<RecipeStar> find(String recipeOwnerUsername, String recipeSlug, User starer) throws RecipeException {
return this.recipeStarRepository.findByRecipeIdAndOwnerUsername(recipeId, ownerUsername).orElseThrow( final Recipe recipe = this.recipeService.getByUsernameAndSlug(recipeOwnerUsername, recipeSlug, starer);
() -> new RecipeException( return this.recipeStarRepository.findByRecipeIdAndOwnerUsername(recipe.getId(), starer.getUsername())
RecipeException.Type.INVALID_ID, .map(RecipeStar.class::cast);
"No such RecipeStar for recipeId: " + recipeId + " and ownerUsername: " + ownerUsername
)
);
} }
@Override @Override