Skip to content

Commit

Permalink
get who bookmarked a recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
atakanyasar committed May 10, 2024
1 parent 50d8d6e commit ddb34da
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import com.group1.cuisines.dto.NewRecipeDto;
import com.group1.cuisines.dto.RatingDto;
import com.group1.cuisines.dto.RecipeDetailDto;
import com.group1.cuisines.entities.User;
import com.group1.cuisines.services.RecipeService;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
Expand All @@ -11,6 +12,8 @@
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;

import java.util.List;

@RestController
@RequestMapping("/api/v1")
public class RecipeController {
Expand Down Expand Up @@ -84,4 +87,16 @@ public ResponseEntity<?> bookmarkRecipe(@PathVariable Integer recipeId) {
}
}

@GetMapping("/recipes/{recipeId}/bookmarks")
public ResponseEntity<?> getBookmarks(@PathVariable Integer recipeId) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null || authentication.getPrincipal().equals("anonymousUser")) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Authentication required.");
}

List<User> whoBookmarked = recipeService.getWhoBookmarked(recipeId);
return ResponseEntity.ok().body(whoBookmarked);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
Expand Down Expand Up @@ -140,4 +141,8 @@ public boolean bookmarkRecipe(Integer recipeId, String username) {
return true;
}

public List<User> getWhoBookmarked(Integer recipeId) {
return bookmarkRepository.findByRecipeId(recipeId).stream().map(Bookmark::getUser).toList();
}

}

0 comments on commit ddb34da

Please sign in to comment.