feat(controller): added idUser by token to read recipes

parent 11759042
...@@ -3,12 +3,15 @@ package com.example.apprecetas.recipe.infrastructure.controller; ...@@ -3,12 +3,15 @@ package com.example.apprecetas.recipe.infrastructure.controller;
import com.example.apprecetas.recipe.application.ReadRecipeUseCase; import com.example.apprecetas.recipe.application.ReadRecipeUseCase;
import com.example.apprecetas.recipe.infrastructure.controller.dto.output.RecipeOutputDto; import com.example.apprecetas.recipe.infrastructure.controller.dto.output.RecipeOutputDto;
import com.example.apprecetas.recipe.infrastructure.mapper.RecipeMapper; import com.example.apprecetas.recipe.infrastructure.mapper.RecipeMapper;
import com.example.apprecetas.user.application.ReadUserUseCase;
import com.example.apprecetas.user.domain.entity.User;
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.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@RestController @RestController
...@@ -18,6 +21,8 @@ public class ReadRecipeController { ...@@ -18,6 +21,8 @@ public class ReadRecipeController {
private final ReadRecipeUseCase service; private final ReadRecipeUseCase service;
private final ReadUserUseCase readUserUseCase;
private final RecipeMapper mapper = Mappers.getMapper(RecipeMapper.class); private final RecipeMapper mapper = Mappers.getMapper(RecipeMapper.class);
@GetMapping("/{id}") @GetMapping("/{id}")
...@@ -25,17 +30,18 @@ public class ReadRecipeController { ...@@ -25,17 +30,18 @@ public class ReadRecipeController {
return ResponseEntity.ok().body(mapper.map(service.readById(id))); return ResponseEntity.ok().body(mapper.map(service.readById(id)));
} }
@GetMapping("/{userId}") @GetMapping("/mine")
public ResponseEntity<Page<RecipeOutputDto>> readAll(@PathVariable String userId, public ResponseEntity<Page<RecipeOutputDto>> readAll(@RequestParam(required = false, defaultValue = "0") int page,
@RequestParam(required = false) 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 username = SecurityContextHolder.getContext().getAuthentication().getName();
User user = readUserUseCase.readByUsername(username);
if (service.readAllByUser(userId, pageable).isEmpty()) { if (service.readAllByUser(user.getId(), pageable).isEmpty()) {
return ResponseEntity.noContent().build(); return ResponseEntity.noContent().build();
} }
return ResponseEntity.ok().body(service.readAllByUser(userId, pageable).map(mapper::map)); return ResponseEntity.ok().body(service.readAllByUser(user.getId(), pageable).map(mapper::map));
} }
} }
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