perf(recipe/controller): añadida ordenación y devolver RecipeListDto

parent b29e7aa7
package com.example.apprecetas.recipe.infrastructure.controller;
import com.example.apprecetas.recipe.application.ReadRecipeUseCase;
import com.example.apprecetas.recipe.infrastructure.controller.dto.output.RecipeListDto;
import com.example.apprecetas.recipe.infrastructure.controller.dto.output.RecipeOutputDto;
import com.example.apprecetas.recipe.infrastructure.mapper.RecipeMapper;
import com.example.apprecetas.user.application.ReadUserUseCase;
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.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
......@@ -20,8 +21,6 @@ public class ReadRecipeController {
private final ReadRecipeUseCase service;
private final ReadUserUseCase readUserUseCase;
private final RecipeMapper mapper = Mappers.getMapper(RecipeMapper.class);
@GetMapping("/{id}")
......@@ -30,21 +29,25 @@ public class ReadRecipeController {
}
@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);
public ResponseEntity<Page<RecipeListDto>> readAll(@RequestParam(required = false, defaultValue = "0") int page,
@RequestParam(required = false, defaultValue = "6") int size,
@RequestParam(required = false, defaultValue = "DESC") String sortDirection) {
Sort sort = sortDirection.equalsIgnoreCase("asc") ? Sort.by("createdAt").ascending() : Sort.by("createdAt").descending();
Pageable pageable = PageRequest.of(page, size, sort);
String userId = SecurityContextHolder.getContext().getAuthentication().getName();
return ResponseEntity.ok().body(service.readAllByUser(userId, pageable).map(mapper::map));
return ResponseEntity.ok().body(service.readAllByUser(userId, pageable).map(mapper::mapList));
}
@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);
public ResponseEntity<Page<RecipeListDto>> readFavorites(@RequestParam(required = false, defaultValue = "0") int page,
@RequestParam(required = false, defaultValue = "6") int size,
@RequestParam(required = false, defaultValue = "DESC") String sortDirection) {
Sort sort = sortDirection.equalsIgnoreCase("asc") ? Sort.by("createdAt").ascending() : Sort.by("createdAt").descending();
Pageable pageable = PageRequest.of(page, size, sort);
String userId = SecurityContextHolder.getContext().getAuthentication().getName();
return ResponseEntity.ok().body(service.readFavoritesByUser(userId, pageable).map(mapper::map));
return ResponseEntity.ok().body(service.readFavoritesByUser(userId, pageable).map(mapper::mapList));
}
}
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