perf(ReadRecipe): implementado readAllByUser para leer las recetas de un usuario…

perf(ReadRecipe): implementado readAllByUser para leer las recetas de un usuario concreto con Paginación
parent d8319638
package com.example.apprecetas.recipe.application; package com.example.apprecetas.recipe.application;
import com.example.apprecetas.recipe.domain.entity.Recipe; import com.example.apprecetas.recipe.domain.entity.Recipe;
import org.springframework.data.domain.Page;
import java.util.List; import org.springframework.data.domain.Pageable;
public interface ReadRecipeUseCase { public interface ReadRecipeUseCase {
Recipe readById(String id); Recipe readById(String id);
List<Recipe> readAll(); Page<Recipe> readAllByUser(String userId, Pageable pageable);
} }
...@@ -8,10 +8,10 @@ import com.example.apprecetas.recipe.infrastructure.mapper.RecipeMapper; ...@@ -8,10 +8,10 @@ import com.example.apprecetas.recipe.infrastructure.mapper.RecipeMapper;
import com.example.apprecetas.recipe.infrastructure.repository.mongodb.RecipeDocument; import com.example.apprecetas.recipe.infrastructure.repository.mongodb.RecipeDocument;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
public class ReadRecipeUseCaseImpl implements ReadRecipeUseCase { public class ReadRecipeUseCaseImpl implements ReadRecipeUseCase {
...@@ -28,9 +28,8 @@ public class ReadRecipeUseCaseImpl implements ReadRecipeUseCase { ...@@ -28,9 +28,8 @@ public class ReadRecipeUseCaseImpl implements ReadRecipeUseCase {
} }
@Override @Override
public List<Recipe> readAll() { public Page<Recipe> readAllByUser(String userId, Pageable pageable) {
return repository.readAll().stream() return repository.readAllByUser(userId, pageable)
.map(mapper::mapDocument) .map(mapper::mapDocument);
.toList();
} }
} }
package com.example.apprecetas.recipe.domain.repository; package com.example.apprecetas.recipe.domain.repository;
import com.example.apprecetas.recipe.infrastructure.repository.mongodb.RecipeDocument; import com.example.apprecetas.recipe.infrastructure.repository.mongodb.RecipeDocument;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Optional; import java.util.Optional;
public interface ReadRecipeRepository { public interface ReadRecipeRepository {
Optional<RecipeDocument> readById(String id); Optional<RecipeDocument> readById(String id);
List<RecipeDocument> readAll(); Page<RecipeDocument> readAllByUser(String userId, Pageable pageable);
} }
...@@ -5,13 +5,11 @@ import com.example.apprecetas.recipe.infrastructure.controller.dto.output.Recipe ...@@ -5,13 +5,11 @@ import com.example.apprecetas.recipe.infrastructure.controller.dto.output.Recipe
import com.example.apprecetas.recipe.infrastructure.mapper.RecipeMapper; import com.example.apprecetas.recipe.infrastructure.mapper.RecipeMapper;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController @RestController
@RequestMapping("/recipe") @RequestMapping("/recipe")
...@@ -27,15 +25,17 @@ public class ReadRecipeController { ...@@ -27,15 +25,17 @@ public class ReadRecipeController {
return ResponseEntity.ok().body(mapper.map(service.readById(id))); return ResponseEntity.ok().body(mapper.map(service.readById(id)));
} }
@GetMapping @GetMapping("/{userId}")
public ResponseEntity<List<RecipeOutputDto>> readAll() { public ResponseEntity<Page<RecipeOutputDto>> readAll(@PathVariable String userId,
if (service.readAll().isEmpty()) { @RequestParam(required = false) int page,
@RequestParam(required = false, defaultValue = "6") int size) {
Pageable pageable = PageRequest.of(page, size);
if (service.readAllByUser(userId, pageable).isEmpty()) {
return ResponseEntity.noContent().build(); return ResponseEntity.noContent().build();
} }
return ResponseEntity.ok().body(service.readAll().stream()
.map(mapper::map) return ResponseEntity.ok().body(service.readAllByUser(userId, pageable).map(mapper::map));
.toList()
);
} }
} }
...@@ -4,9 +4,10 @@ import com.example.apprecetas.recipe.domain.repository.ReadRecipeRepository; ...@@ -4,9 +4,10 @@ import com.example.apprecetas.recipe.domain.repository.ReadRecipeRepository;
import com.example.apprecetas.recipe.infrastructure.repository.mongodb.RecipeDocument; import com.example.apprecetas.recipe.infrastructure.repository.mongodb.RecipeDocument;
import com.example.apprecetas.recipe.infrastructure.repository.mongodb.RecipeRepository; import com.example.apprecetas.recipe.infrastructure.repository.mongodb.RecipeRepository;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional; import java.util.Optional;
@Repository @Repository
...@@ -21,7 +22,7 @@ public class ReadRecipeRepositoryImpl implements ReadRecipeRepository { ...@@ -21,7 +22,7 @@ public class ReadRecipeRepositoryImpl implements ReadRecipeRepository {
} }
@Override @Override
public List<RecipeDocument> readAll() { public Page<RecipeDocument> readAllByUser(String userId, Pageable pageable) {
return repositoryJpa.findAll(); return repositoryJpa.findByUserId(userId, pageable);
} }
} }
...@@ -26,4 +26,6 @@ public class RecipeDocument { ...@@ -26,4 +26,6 @@ public class RecipeDocument {
private Set<IngredientDocument> ingredients = new HashSet<>(); private Set<IngredientDocument> ingredients = new HashSet<>();
private Set<StepDocument> steps = new HashSet<>(); private Set<StepDocument> steps = new HashSet<>();
private String userId;
} }
package com.example.apprecetas.recipe.infrastructure.repository.mongodb; package com.example.apprecetas.recipe.infrastructure.repository.mongodb;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.MongoRepository;
public interface RecipeRepository extends MongoRepository<RecipeDocument, String> { public interface RecipeRepository extends MongoRepository<RecipeDocument, String> {
Page<RecipeDocument> findByUserId(String userId, Pageable pageable);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment