Skip to content

Commit

Permalink
Merge pull request #204 from bounswe/backend/feature/203-cuisines/{cu…
Browse files Browse the repository at this point in the history
…isineId}-reimplement

GET cuisines/{cuisineId} reimplemented
  • Loading branch information
EnesBaserr authored May 13, 2024
2 parents adf4ebb + 9b15a89 commit 8044ed0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.group1.cuisines.controllers;

import com.group1.cuisines.dao.response.ApiResponse;
import com.group1.cuisines.dao.response.ErrorResponse;
import com.group1.cuisines.dao.response.SuccessResponse;
import com.group1.cuisines.dto.CuisineDetailsDto;
import com.group1.cuisines.entities.Cuisine;
import com.group1.cuisines.repositories.CuisineRepository;
import com.group1.cuisines.services.CuisineService;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -21,14 +26,12 @@ public class CuisineController {
private final CuisineRepository cuisineRepository;

@GetMapping("/{cuisineId}")
public ResponseEntity<?> getCuisineDetails(
@PathVariable String cuisineId,
@RequestParam(defaultValue = "false") boolean includeDishes) {

Cuisine cuisine = cuisineService.getCuisineById(cuisineId, includeDishes);
if (cuisine == null) {
return ResponseEntity.notFound().build();
public ResponseEntity<?> getCuisineById(@PathVariable String cuisineId, @RequestParam(required = false) Boolean includeDishes) {
try {
CuisineDetailsDto cuisineDetails = cuisineService.getCuisineById(cuisineId, Boolean.TRUE.equals(includeDishes));
return ResponseEntity.ok(new SuccessResponse<>(cuisineDetails, "Cuisine details fetched successfully"));
} catch (EntityNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Cuisine not found"));
}
return ResponseEntity.ok(cuisine);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.group1.cuisines.dto;
import lombok.*;

import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CuisineDetailsDto {
private String id;
private String name;
// private String description;
//private String image;
// private Boolean isSelfFollowing;
private List<DishDto> dishes;
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
package com.group1.cuisines.services;

import com.group1.cuisines.dto.CuisineDetailsDto;
import com.group1.cuisines.dto.DishDto;
import com.group1.cuisines.entities.Cuisine;
import com.group1.cuisines.entities.Dish;
import com.group1.cuisines.repositories.CuisineRepository;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class CuisineService {

@Autowired
private CuisineRepository cuisineRepository;

public Cuisine getCuisineById(String cuisineId, boolean includeDishes) {
return cuisineRepository.findById(cuisineId)
.map(cuisine -> {
if (!includeDishes) {
cuisine.setDishes(null); // Clear the dishes if not required
}
return cuisine;
})
.orElse(null);
public CuisineDetailsDto getCuisineById(String cuisineId, boolean includeDishes) {
Cuisine cuisine = cuisineRepository.findById(cuisineId)
.orElseThrow(() -> new EntityNotFoundException("Cuisine not found"));

CuisineDetailsDto detailsDto = new CuisineDetailsDto(
cuisine.getId(),
cuisine.getName(),
// cuisine.getDescription(),


includeDishes ? convertDishes(cuisine.getDishes()) : new ArrayList<>()
);
return detailsDto;
}

private List<DishDto> convertDishes(Set<Dish> dishes) {
return dishes.stream()
.map(dish -> new DishDto(dish.getId(), dish.getName(),dish.getImage()))
.collect(Collectors.toList());
}
}
2 changes: 1 addition & 1 deletion backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ spring.datasource.url=jdbc:mysql://localhost:3306/cuisines-test?createDatabaseIf
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
logging.level.com.group1.cuisines=DEBUG

Expand Down

0 comments on commit 8044ed0

Please sign in to comment.