Skip to content

Commit

Permalink
Formatting responses for /search (dishes)
Browse files Browse the repository at this point in the history
  • Loading branch information
EnesBaserr committed May 13, 2024
1 parent 8044ed0 commit a992124
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.group1.cuisines.dao.response.AuthenticationTokenResponse;
import com.group1.cuisines.services.AuthenticationService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -20,10 +21,18 @@ public class AuthenticationController {
private final AuthenticationService authenticationService; // Authentication service

@PostMapping("/signup") // Sign up endpoint
public ResponseEntity<ApiResponse<AuthenticationTokenResponse>> signup(
public ResponseEntity<?> signup(
@RequestBody SignUpRequest request
) {
return ResponseEntity.ok(authenticationService.signup(request)); // Return response
ApiResponse<AuthenticationTokenResponse> response = authenticationService.signup(request);

if (response.getStatus() == 409) {
return ResponseEntity.status(HttpStatus.CONFLICT).body(response);
} else if (response.getStatus() == 400) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
} else {
return ResponseEntity.status(HttpStatus.CREATED).body(response);
}
}

@PostMapping("/login") // Sign in endpoint
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.group1.cuisines.controllers;

import com.group1.cuisines.dao.response.ApiResponse;
import com.group1.cuisines.dto.DishResponseDto;
import com.group1.cuisines.entities.Dish;
import com.group1.cuisines.entities.User;
import com.group1.cuisines.services.SearchService;
Expand Down Expand Up @@ -34,9 +35,9 @@ public ResponseEntity<?> searchUsers(@RequestParam(required = false) String q) {
}

@GetMapping("/dishes")
public ApiResponse<List<Dish>> searchDishes(@RequestParam(required = false) String q,
@RequestParam(required = false) String cuisine,
@RequestParam(required = false) String foodType) {
public ApiResponse<List<DishResponseDto>> searchDishes(@RequestParam(required = false) String q,
@RequestParam(required = false) String cuisine,
@RequestParam(required = false) String foodType) {
return new ApiResponse<>(
200,
"Search completed",
Expand Down
19 changes: 19 additions & 0 deletions backend/src/main/java/com/group1/cuisines/dto/DishResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.group1.cuisines.dto;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DishResponseDto {
private String id;
private String name;
private String image;
private String description;
private String countries;
private String ingredients;
private String foodTypes;
private String cuisines;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.group1.cuisines.exceptions;

import com.group1.cuisines.dao.response.ApiResponse;
import org.springframework.boot.context.config.ConfigDataResourceNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(Exception.class)
public ResponseEntity<ApiResponse<String>> handleException(Exception e) {
ApiResponse<String> response = new ApiResponse<>(500, "An unexpected error occurred: " + e.getMessage(), null);
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<ApiResponse<String>> handleResourceNotFoundException(ResourceNotFoundException e) {
ApiResponse<String> response = new ApiResponse<>(400, e.getMessage(), null);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.group1.cuisines.exceptions;

public class ResourceNotFoundException extends RuntimeException {
public ResourceNotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public ApiResponse<AuthenticationTokenResponse> signup(
String token = jwtService.generateToken(user);

return new ApiResponse<>(
200,
201,
"User registered successfully.",
AuthenticationTokenResponse.builder().token(token).build()
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.group1.cuisines.services;

import com.group1.cuisines.dto.DishDto;
import com.group1.cuisines.dto.DishResponseDto;
import com.group1.cuisines.entities.Dish;
import com.group1.cuisines.exceptions.ResourceNotFoundException;
import com.group1.cuisines.repositories.DishRepository;
import com.group1.cuisines.repositories.UserRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,17 +19,23 @@
public class SearchService {

private final DishRepository dishRepository;
public List<Dish> searchDishes(String query, String cuisine, String foodType) {
public List<DishResponseDto> searchDishes(String query, String cuisine, String foodType) {
List<Dish> dishes = dishRepository.findAll();

// Filter by dish name
if (query != null && !query.isEmpty()) {
dishes = dishRepository.findByNameContainingIgnoreCase(query);
if (dishes.isEmpty()) {
throw new ResourceNotFoundException("No dishes found with the given name query.");
}
}

// Filter by cuisine name
if (cuisine != null && !cuisine.isEmpty()) {
List<Dish> dishesByCuisine = dishRepository.findByCuisinesName(cuisine);
if (dishesByCuisine.isEmpty()) {
throw new ResourceNotFoundException("No dishes found with the given cuisine.");
}
dishes = dishes.stream()
.filter(dishesByCuisine::contains)
.collect(Collectors.toList());
Expand All @@ -37,9 +46,25 @@ public List<Dish> searchDishes(String query, String cuisine, String foodType) {
dishes = dishes.stream()
.filter(d -> d.getFoodTypes() != null && d.getFoodTypes().contains(foodType))
.collect(Collectors.toList());

if (dishes.isEmpty()) {
throw new ResourceNotFoundException("No dishes found with the given food type.");
}
}

return dishes;
// Map to DishResponseDto
return dishes.stream()
.map(d -> new DishResponseDto(
d.getId(),
d.getName(),
d.getImage(),
d.getDescription(),
d.getCountries(),
d.getIngredients(),
d.getFoodTypes(),
d.getCuisines().isEmpty() ? null : d.getCuisines().get(0).getName()
))
.collect(Collectors.toList());
}


Expand Down

0 comments on commit a992124

Please sign in to comment.