Skip to content

Commit ef001ed

Browse files
authored
Merge pull request #186 from bounswe/backend/feature/179-post-bookmark
POST "/recipes/{recipeId}/bookmarks" endpoint implemented
2 parents 69af42c + 50d8d6e commit ef001ed

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

backend/src/main/java/com/group1/cuisines/controllers/RecipeController.java

+15
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,21 @@ public ResponseEntity<?> rateRecipe(@PathVariable Integer recipeId, @RequestBody
6767
}
6868
}
6969

70+
@PostMapping("/recipes/{recipeId}/bookmarks")
71+
public ResponseEntity<?> bookmarkRecipe(@PathVariable Integer recipeId) {
72+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
73+
if (authentication == null || authentication.getPrincipal().equals("anonymousUser")) {
74+
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Authentication required.");
75+
}
76+
77+
String username = authentication.getName(); // Assuming the username can be obtained like this
78+
boolean success = recipeService.bookmarkRecipe(recipeId, username);
7079

80+
if (success) {
81+
return ResponseEntity.ok().body("Recipe bookmarked successfully");
82+
} else {
83+
return ResponseEntity.badRequest().body("Failed to bookmark recipe");
84+
}
85+
}
7186

7287
}

backend/src/main/java/com/group1/cuisines/repositories/BookmarkRepository.java

+7
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44
import org.springframework.data.jpa.repository.JpaRepository;
55
import org.springframework.stereotype.Repository;
66

7+
import java.util.List;
8+
import java.util.Optional;
9+
710
@Repository
811
public interface BookmarkRepository extends JpaRepository<Bookmark,Integer> {
12+
List<Bookmark> findByUserId(Integer userId);
13+
List<Bookmark> findByRecipeId(Integer recipeId);
14+
Optional<Bookmark> findByUserIdAndRecipeId(Integer userId, Integer recipeId);
15+
916
}
1017

1118

backend/src/main/java/com/group1/cuisines/services/RecipeService.java

+25
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public class RecipeService {
2626
private UserRepository userRepository;
2727
@Autowired
2828
private RatingRepository ratingRepository;
29+
@Autowired
30+
private BookmarkRepository bookmarkRepository;
2931

3032
@Transactional
3133
public RecipeDetailDto createRecipe(NewRecipeDto newRecipe, String username) throws Exception {
@@ -115,4 +117,27 @@ public boolean rateRecipe(Integer recipeId, String username, Integer ratingValue
115117
}
116118
return false;
117119
}
120+
121+
public boolean bookmarkRecipe(Integer recipeId, String username) {
122+
User user = userRepository.findByUsername(username).orElse(null);
123+
if (user == null){
124+
return false;
125+
}
126+
Recipe recipe = recipeRepository.findById(recipeId).orElse(null);
127+
if (recipe == null){
128+
return false;
129+
}
130+
if (bookmarkRepository.findByUserIdAndRecipeId(user.getId(), recipeId).isPresent()){
131+
return false;
132+
}
133+
134+
Bookmark bookmark = Bookmark.builder()
135+
.user(user)
136+
.recipe(recipe)
137+
.build();
138+
bookmarkRepository.save(bookmark);
139+
140+
return true;
141+
}
142+
118143
}

0 commit comments

Comments
 (0)