Skip to content

Commit

Permalink
Merge pull request #251 from bounswe/backend/feature/250-detailed-my-…
Browse files Browse the repository at this point in the history
…profile-endpoint

feat(backend): detailed my profile page
  • Loading branch information
atakanyasar authored May 15, 2024
2 parents ad5cab1 + b6d3b84 commit 98d7dff
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.group1.cuisines.controllers;
import com.group1.cuisines.dao.response.ErrorResponse;
import com.group1.cuisines.dao.response.SuccessResponse;
import com.group1.cuisines.dto.*;
import com.group1.cuisines.entities.Comment;
Expand Down Expand Up @@ -29,19 +30,19 @@ public class RecipeController {
public ResponseEntity<?> getRecipeById(@PathVariable Integer recipeId) {
RecipeDetailsDto recipeDetails = recipeService.getRecipeById(recipeId);
if (recipeDetails != null) {
return ResponseEntity.ok(new SuccessResponse<>(200,recipeDetails, "Recipe fetched successfully"));
return ResponseEntity.ok(new SuccessResponse<>(200, recipeDetails, "Recipe fetched successfully"));
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Recipe not found");
return ResponseEntity.ok(new ErrorResponse(200, ""));
}
}


@GetMapping("/recipes")
public ResponseEntity<List<RecipeDto>> getRecipes(@RequestParam(required = false) String sort,
public ResponseEntity<?> getRecipes(@RequestParam(required = false) String sort,
@RequestParam(required = false) String dishId,
@RequestParam(required = false) String cuisineId) {
List<RecipeDto> recipes = recipeService.findRecipes(sort, dishId, cuisineId);
return ResponseEntity.ok(recipes);
return ResponseEntity.ok(new SuccessResponse<>(200, recipes, "Recipes fetched successfully"));
}


Expand All @@ -52,80 +53,79 @@ public ResponseEntity<?> createRecipe(@RequestBody NewRecipeDto newRecipe) throw

// Check if the user is authenticated
if (username == null) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Authentication required.");
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new ErrorResponse(401, "Authentication required"));
}

RecipeDetailDto recipeDetails = recipeService.createRecipe(newRecipe, username);
if (recipeDetails != null) {
return ResponseEntity.status(HttpStatus.CREATED).body(recipeDetails);
return ResponseEntity.ok(new SuccessResponse<>(201, recipeDetails, "Recipe created successfully"));
} else {
return ResponseEntity.badRequest().body("Failed to create recipe");
return ResponseEntity.ok(new ErrorResponse(400, "Failed to create recipe"));
}
}
@DeleteMapping("/recipes/{id}")
public ResponseEntity<?> deleteRecipe(@PathVariable Integer id) {
String username = authenticationService.getUser().map(User::getUsername).orElse(null);

if (username == null) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Authentication required.");
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new ErrorResponse(401, "Authentication required"));
}

if (recipeService.deleteRecipe(id, username)) {
return ResponseEntity.status(HttpStatus.NO_CONTENT).body("Recipe deleted successfully.");
return ResponseEntity.ok(new SuccessResponse<>(200, "", "Recipe deleted successfully"));
} else {
return ResponseEntity.badRequest().body("Failed to delete recipe");
return ResponseEntity.ok(new ErrorResponse(400, "Failed to delete recipe"));
}
}
@PostMapping("/recipes/{recipeId}/rating")
public ResponseEntity<?> rateRecipe(@PathVariable Integer recipeId, @RequestBody RatingDto ratingDto) {
String username = authenticationService.getUser().map(User::getUsername).orElse(null);
if (username == null) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Authentication required.");
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new ErrorResponse(401, "Authentication required"));
}

boolean success = recipeService.rateRecipe(recipeId, username, ratingDto.getRating());

if (success) {
return ResponseEntity.ok().body("Rating added successfully");
return ResponseEntity.ok(new SuccessResponse<>(200, "", "Rating added successfully"));
} else {
return ResponseEntity.badRequest().body("Failed to add rating");
return ResponseEntity.ok(new ErrorResponse(400, "Failed to add rating"));
}
}

@PostMapping("/recipes/{recipeId}/bookmarks")
public ResponseEntity<?> bookmarkRecipe(@PathVariable Integer recipeId) {
String username = authenticationService.getUser().map(User::getUsername).orElse(null);
if (username == null) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Authentication required.");
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new ErrorResponse(401, "Authentication required"));
}

boolean success = recipeService.bookmarkRecipe(recipeId, username);

if (success) {
return ResponseEntity.ok().body("Recipe bookmarked successfully");
} else {
return ResponseEntity.badRequest().body("Failed to bookmark recipe");
return ResponseEntity.ok(new SuccessResponse<>(200, "", "Recipe bookmarked successfully"));
}
return ResponseEntity.ok(new ErrorResponse(400, "Failed to bookmark recipe"));
}

@GetMapping("/recipes/{recipeId}/bookmarks")
public ResponseEntity<?> getBookmarks(@PathVariable Integer recipeId) {
String username = authenticationService.getUser().map(User::getUsername).orElse(null);
if (username == null) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Authentication required.");
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new ErrorResponse(401, "Authentication required"));
}

List<User> whoBookmarked = recipeService.getWhoBookmarked(recipeId);
return ResponseEntity.ok().body(whoBookmarked);
return ResponseEntity.ok(new SuccessResponse<>(200, whoBookmarked, "Bookmarks fetched successfully"));
}

@GetMapping("/recipes/{recipeId}/comments")
public ResponseEntity<?> getComments(@PathVariable Integer recipeId) {
List<CommentsDto> commentsDto = recipeService.getCommentsByRecipeId(recipeId);
if (commentsDto.isEmpty()) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No comments found for this recipe.");
return ResponseEntity.ok(new ErrorResponse(204, "No comments found"));
}
return ResponseEntity.ok(commentsDto);
return ResponseEntity.ok(new SuccessResponse<>(200, commentsDto, "Comments fetched successfully"));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,16 @@ public ResponseEntity<?> getUserById(@PathVariable Integer userId, Authenticatio

@GetMapping("/me")
public ResponseEntity<?> getUserDetails(@AuthenticationPrincipal UserDetails userDetails) {
if (userDetails instanceof com.group1.cuisines.entities.User) {
User user = (User) userDetails;
Map<String, String> userInfo = new HashMap<>();
userInfo.put("username", user.getUsername());
userInfo.put("email", user.getEmail());
userInfo.put("bio", user.getBio());
userInfo.put("country", user.getCountry());

return ResponseEntity.ok(userInfo);
}

return ResponseEntity.status(HttpStatus.FORBIDDEN).body("User not authenticated");

if (userDetails != null) {
User user = userRepository.findByUsername(userDetails.getUsername()).orElse(null);
if (user != null) {
UserProfileDto userProfile = userService.getUserProfileById(user.getId(), userDetails.getUsername());
return ResponseEntity.ok(new SuccessResponse<>(200, userProfile, "User profile fetched successfully"));
}
}
return ResponseEntity.ok(new ErrorResponse(204, "User not found"));
}

@DeleteMapping("/{userId}/unfollow")
public ResponseEntity<?> unfollowUser(@PathVariable Integer userId) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Expand Down

0 comments on commit 98d7dff

Please sign in to comment.