Skip to content

Commit 13b08d9

Browse files
committed
Merge branch 'develop' into backend/feature/175-get-comments-endpoint
# Conflicts: # backend/src/main/java/com/group1/cuisines/controllers/RecipeController.java # backend/src/main/java/com/group1/cuisines/services/RecipeService.java
2 parents 2a01519 + 75f562a commit 13b08d9

File tree

5 files changed

+133
-2
lines changed

5 files changed

+133
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: backend-staging-workflow
2+
3+
on:
4+
workflow_run:
5+
workflows: ["backend-test"]
6+
types:
7+
- completed
8+
push:
9+
branches:
10+
- staging
11+
paths:
12+
- 'backend/**'
13+
- compose.yml
14+
15+
jobs:
16+
deploy:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v4
21+
22+
- name: Authorize to with doctl
23+
run: echo ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }} | doctl auth init --context github-actions
24+
25+
- name: Login to Docker registry
26+
run: doctl registry login --context github-actions
27+
28+
- name: Build Docker image
29+
run: docker compose build
30+
31+
- name: Tag Docker image
32+
run: docker tag bounswe2024group1-backend:latest registry.digitalocean.com/semantic-browse/backend-staging:latest
33+
34+
- name: Push Docker image
35+
run: docker push registry.digitalocean.com/semantic-browse/backend-staging:latest
36+

.github/workflows/backend_test.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: backend-test
2+
3+
on:
4+
push:
5+
paths:
6+
- 'backend/**'
7+
- compose.yml
8+
- dev.yml
9+
- '.github/workflows/backend_test.yml'
10+
11+
pull_request:
12+
branches:
13+
- main
14+
- staging
15+
- develop
16+
17+
18+
jobs:
19+
test:
20+
runs-on: ubuntu-latest
21+
env:
22+
working-directory:
23+
backend
24+
steps:
25+
- name: Checkout
26+
uses: actions/checkout@v4
27+
28+
- name: Test with Maven
29+
run: docker compose -f dev.yml run --rm backend mvn test
30+
31+
32+

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

+31
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.group1.cuisines.dto.RatingDto;
44
import com.group1.cuisines.dto.RecipeDetailDto;
55
import com.group1.cuisines.entities.Comment;
6+
import com.group1.cuisines.entities.User;
67
import com.group1.cuisines.services.RecipeService;
78
import org.springframework.http.HttpStatus;
89
import org.springframework.security.core.Authentication;
@@ -13,6 +14,8 @@
1314
import org.springframework.security.access.prepost.PreAuthorize;
1415
import java.util.List;
1516

17+
import java.util.List;
18+
1619
@RestController
1720
@RequestMapping("/api/v1")
1821
public class RecipeController {
@@ -69,6 +72,34 @@ public ResponseEntity<?> rateRecipe(@PathVariable Integer recipeId, @RequestBody
6972
}
7073
}
7174

75+
@PostMapping("/recipes/{recipeId}/bookmarks")
76+
public ResponseEntity<?> bookmarkRecipe(@PathVariable Integer recipeId) {
77+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
78+
if (authentication == null || authentication.getPrincipal().equals("anonymousUser")) {
79+
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Authentication required.");
80+
}
81+
82+
String username = authentication.getName(); // Assuming the username can be obtained like this
83+
boolean success = recipeService.bookmarkRecipe(recipeId, username);
84+
85+
if (success) {
86+
return ResponseEntity.ok().body("Recipe bookmarked successfully");
87+
} else {
88+
return ResponseEntity.badRequest().body("Failed to bookmark recipe");
89+
}
90+
}
91+
92+
@GetMapping("/recipes/{recipeId}/bookmarks")
93+
public ResponseEntity<?> getBookmarks(@PathVariable Integer recipeId) {
94+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
95+
if (authentication == null || authentication.getPrincipal().equals("anonymousUser")) {
96+
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Authentication required.");
97+
}
98+
99+
List<User> whoBookmarked = recipeService.getWhoBookmarked(recipeId);
100+
return ResponseEntity.ok().body(whoBookmarked);
101+
}
102+
72103
@GetMapping("/recipes/{recipeId}/comments")
73104
public ResponseEntity<?> getComments(@PathVariable Integer recipeId) {
74105

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

+27-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public class RecipeService {
2727
private UserRepository userRepository;
2828
@Autowired
2929
private RatingRepository ratingRepository;
30+
@Autowired
31+
private BookmarkRepository bookmarkRepository;
3032

3133
@Autowired
3234
private CommentRepository commentRepository;
@@ -120,11 +122,34 @@ public boolean rateRecipe(Integer recipeId, String username, Integer ratingValue
120122
return false;
121123
}
122124

125+
public boolean bookmarkRecipe(Integer recipeId, String username) {
126+
User user = userRepository.findByUsername(username).orElse(null);
127+
if (user == null){
128+
return false;
129+
}
130+
Recipe recipe = recipeRepository.findById(recipeId).orElse(null);
131+
if (recipe == null){
132+
return false;
133+
}
134+
if (bookmarkRepository.findByUserIdAndRecipeId(user.getId(), recipeId).isPresent()){
135+
return false;
136+
}
123137

124-
public List<Comment> getCommentsByRecipeId(Integer recipeId) {
125-
return commentRepository.findByRecipeId(recipeId);
138+
Bookmark bookmark = Bookmark.builder()
139+
.user(user)
140+
.recipe(recipe)
141+
.build();
142+
bookmarkRepository.save(bookmark);
143+
144+
return true;
126145
}
127146

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

151+
public List<Comment> getCommentsByRecipeId(Integer recipeId) {
152+
return commentRepository.findByRecipeId(recipeId);
153+
}
129154

130155
}

0 commit comments

Comments
 (0)