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 {
Recipe readById(String id);
Page<Recipe> readAllByUser(String userId, Pageable pageable);
Page<Recipe> readFavoritesByUser(String userId, Pageable pageable);
}
......@@ -32,4 +32,10 @@ public class ReadRecipeUseCaseImpl implements ReadRecipeUseCase {
return repository.readAllByUser(userId, pageable)
.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 {
Optional<RecipeDocument> readById(String id);
Page<RecipeDocument> readAllByUser(String userId, Pageable pageable);
Page<RecipeDocument> readFavoritesByUser(String userId, Pageable pageable);
}
......@@ -29,17 +29,22 @@ public class ReadRecipeController {
return ResponseEntity.ok().body(mapper.map(service.readById(id)));
}
@GetMapping("/mine")
@GetMapping
public ResponseEntity<Page<RecipeOutputDto>> readAll(@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();
if (service.readAllByUser(userId, pageable).isEmpty()) {
return ResponseEntity.noContent().build();
return ResponseEntity.ok().body(service.readAllByUser(userId, pageable).map(mapper::map));
}
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 {
public Page<RecipeDocument> readAllByUser(String userId, Pageable 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;
public interface RecipeRepository extends MongoRepository<RecipeDocument, String> {
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