feat(recipe): implementada funcionalidad para leer las recetas favoritas de un…

feat(recipe): implementada funcionalidad para leer las recetas favoritas de un usuario en repositorio, servicio y controlador ReadRecipe
parent b8c5937d
...@@ -8,4 +8,6 @@ public interface ReadRecipeUseCase { ...@@ -8,4 +8,6 @@ public interface ReadRecipeUseCase {
Recipe readById(String id); Recipe readById(String id);
Page<Recipe> readAllByUser(String userId, Pageable pageable); Page<Recipe> readAllByUser(String userId, Pageable pageable);
Page<Recipe> readFavoritesByUser(String userId, Pageable pageable);
} }
...@@ -32,4 +32,10 @@ public class ReadRecipeUseCaseImpl implements ReadRecipeUseCase { ...@@ -32,4 +32,10 @@ public class ReadRecipeUseCaseImpl implements ReadRecipeUseCase {
return repository.readAllByUser(userId, pageable) return repository.readAllByUser(userId, pageable)
.map(mapper::mapDocument); .map(mapper::mapDocument);
} }
@Override
public Page<Recipe> readFavoritesByUser(String userId, Pageable pageable) {
return repository.readFavoritesByUser(userId, pageable)
.map(mapper::mapDocument);
}
} }
...@@ -10,4 +10,6 @@ public interface ReadRecipeRepository { ...@@ -10,4 +10,6 @@ public interface ReadRecipeRepository {
Optional<RecipeDocument> readById(String id); Optional<RecipeDocument> readById(String id);
Page<RecipeDocument> readAllByUser(String userId, Pageable pageable); Page<RecipeDocument> readAllByUser(String userId, Pageable pageable);
Page<RecipeDocument> readFavoritesByUser(String userId, Pageable pageable);
} }
...@@ -29,17 +29,22 @@ public class ReadRecipeController { ...@@ -29,17 +29,22 @@ public class ReadRecipeController {
return ResponseEntity.ok().body(mapper.map(service.readById(id))); return ResponseEntity.ok().body(mapper.map(service.readById(id)));
} }
@GetMapping("/mine") @GetMapping
public ResponseEntity<Page<RecipeOutputDto>> readAll(@RequestParam(required = false, defaultValue = "0") int page, public ResponseEntity<Page<RecipeOutputDto>> readAll(@RequestParam(required = false, defaultValue = "0") int page,
@RequestParam(required = false, defaultValue = "6") int size) { @RequestParam(required = false, defaultValue = "6") int size) {
Pageable pageable = PageRequest.of(page, size); Pageable pageable = PageRequest.of(page, size);
String userId = SecurityContextHolder.getContext().getAuthentication().getName(); String userId = SecurityContextHolder.getContext().getAuthentication().getName();
if (service.readAllByUser(userId, pageable).isEmpty()) { return ResponseEntity.ok().body(service.readAllByUser(userId, pageable).map(mapper::map));
return ResponseEntity.noContent().build();
} }
return ResponseEntity.ok().body(service.readAllByUser(userId, pageable).map(mapper::map)); @GetMapping("/favorites")
public ResponseEntity<Page<RecipeOutputDto>> readFavorites(@RequestParam(required = false, defaultValue = "0") int page,
@RequestParam(required = false, defaultValue = "6") int size) {
Pageable pageable = PageRequest.of(page, size);
String userId = SecurityContextHolder.getContext().getAuthentication().getName();
return ResponseEntity.ok().body(service.readFavoritesByUser(userId, pageable).map(mapper::map));
} }
} }
...@@ -25,4 +25,9 @@ public class ReadRecipeRepositoryImpl implements ReadRecipeRepository { ...@@ -25,4 +25,9 @@ public class ReadRecipeRepositoryImpl implements ReadRecipeRepository {
public Page<RecipeDocument> readAllByUser(String userId, Pageable pageable) { public Page<RecipeDocument> readAllByUser(String userId, Pageable pageable) {
return recipeRepository.findByUserId(userId, pageable); return recipeRepository.findByUserId(userId, pageable);
} }
@Override
public Page<RecipeDocument> readFavoritesByUser(String userId, Pageable pageable) {
return recipeRepository.findByUserIdAndIsFavoriteTrue(userId, pageable);
}
} }
...@@ -6,4 +6,6 @@ import org.springframework.data.mongodb.repository.MongoRepository; ...@@ -6,4 +6,6 @@ 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); Page<RecipeDocument> findByUserId(String userId, Pageable pageable);
Page<RecipeDocument> findByUserIdAndIsFavoriteTrue(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