From 9b15a89c0aae4ef3763d3a3dc9355ec5c8f51eb9 Mon Sep 17 00:00:00 2001 From: EnesBaserr Date: Mon, 13 May 2024 15:36:48 +0300 Subject: [PATCH] GET users/{userId} endpoint implemented.(User Profile & bookmark recipe related infos.) --- .../controllers/CuisineController.java | 19 ++++++---- .../cuisines/dto/CuisineDetailsDto.java | 15 ++++++++ .../cuisines/services/CuisineService.java | 37 ++++++++++++++----- .../src/main/resources/application.properties | 2 +- 4 files changed, 55 insertions(+), 18 deletions(-) create mode 100644 backend/src/main/java/com/group1/cuisines/dto/CuisineDetailsDto.java diff --git a/backend/src/main/java/com/group1/cuisines/controllers/CuisineController.java b/backend/src/main/java/com/group1/cuisines/controllers/CuisineController.java index 996df87f..bca83b3f 100644 --- a/backend/src/main/java/com/group1/cuisines/controllers/CuisineController.java +++ b/backend/src/main/java/com/group1/cuisines/controllers/CuisineController.java @@ -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.*; @@ -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); } } diff --git a/backend/src/main/java/com/group1/cuisines/dto/CuisineDetailsDto.java b/backend/src/main/java/com/group1/cuisines/dto/CuisineDetailsDto.java new file mode 100644 index 00000000..e06a70d0 --- /dev/null +++ b/backend/src/main/java/com/group1/cuisines/dto/CuisineDetailsDto.java @@ -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 dishes; +} diff --git a/backend/src/main/java/com/group1/cuisines/services/CuisineService.java b/backend/src/main/java/com/group1/cuisines/services/CuisineService.java index e65a84ca..e0211558 100644 --- a/backend/src/main/java/com/group1/cuisines/services/CuisineService.java +++ b/backend/src/main/java/com/group1/cuisines/services/CuisineService.java @@ -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 convertDishes(Set dishes) { + return dishes.stream() + .map(dish -> new DishDto(dish.getId(), dish.getName(),dish.getImage())) + .collect(Collectors.toList()); } } diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index a88f9b2c..14898104 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -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