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;
import com.example.apprecetas.recipe.domain.entity.Recipe;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
public interface ReadRecipeUseCase {
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;
import com.example.apprecetas.recipe.infrastructure.repository.mongodb.RecipeDocument;
import lombok.RequiredArgsConstructor;
import org.mapstruct.factory.Mappers;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@RequiredArgsConstructor
public class ReadRecipeUseCaseImpl implements ReadRecipeUseCase {
......@@ -28,9 +28,8 @@ public class ReadRecipeUseCaseImpl implements ReadRecipeUseCase {
}
@Override
public List<Recipe> readAll() {
return repository.readAll().stream()
.map(mapper::mapDocument)
.toList();
public Page<Recipe> readAllByUser(String userId, Pageable pageable) {
return repository.readAllByUser(userId, pageable)
.map(mapper::mapDocument);
}
}
package com.example.apprecetas.recipe.domain.repository;
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;
public interface ReadRecipeRepository {
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
import com.example.apprecetas.recipe.infrastructure.mapper.RecipeMapper;
import lombok.RequiredArgsConstructor;
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.web.bind.annotation.GetMapping;
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;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/recipe")
......@@ -27,15 +25,17 @@ public class ReadRecipeController {
return ResponseEntity.ok().body(mapper.map(service.readById(id)));
}
@GetMapping
public ResponseEntity<List<RecipeOutputDto>> readAll() {
if (service.readAll().isEmpty()) {
@GetMapping("/{userId}")
public ResponseEntity<Page<RecipeOutputDto>> readAll(@PathVariable String userId,
@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.ok().body(service.readAll().stream()
.map(mapper::map)
.toList()
);
return ResponseEntity.ok().body(service.readAllByUser(userId, pageable).map(mapper::map));
}
}
......@@ -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.RecipeRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
@Repository
......@@ -21,7 +22,7 @@ public class ReadRecipeRepositoryImpl implements ReadRecipeRepository {
}
@Override
public List<RecipeDocument> readAll() {
return repositoryJpa.findAll();
public Page<RecipeDocument> readAllByUser(String userId, Pageable pageable) {
return repositoryJpa.findByUserId(userId, pageable);
}
}
......@@ -26,4 +26,6 @@ public class RecipeDocument {
private Set<IngredientDocument> ingredients = new HashSet<>();
private Set<StepDocument> steps = new HashSet<>();
private String userId;
}
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;
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