1
resposta

Preciso de ajuda com java Springboot

Esta dando erro no pageable, findById, save, isPresent, setStatus, deleteById.

import br.com.compassuol.sp.challenge03.apigateway.repository.OrderRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import javax.persistence.criteria.Order; import java.util.List; @Service @RequiredArgsConstructor public class OrderService { private final OrderRepository orderRepository; public List getAllOrders() { return orderRepository.findAll(); } public Order getOrderById(Long id) { return orderRepository.findById(id).orElse(null); } public Order createOrder(Order order) { return orderRepository.save(order); } public Order updateOrderStatus(Long id, String newStatus) { Optional optionalOrder = orderRepository.findById(id); if (optionalOrder.isPresent()) { Order order = optionalOrder.get(); order.setStatus(newStatus); return orderRepository.save(order); } else { return null; } } public void deleteOrder(Long id) { orderRepository.deleteById(id); } }


import br.com.compassuol.sp.challenge03.apigateway.Product; import br.com.compassuol.sp.challenge03.apigateway.repository.ProductRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.util.List; import java.util.Optional; @Service @RequiredArgsConstructor public class ProductService { private final ProductRepository productRepository;

public List<Product> getAllProducts() {
    return productRepository.findAll();
}
public Product getProductById(Long id) {
    return productRepository.findById(id).orElse(null);
}
public Product createProduct(Product product) {
    return productRepository.save(product);
}
public Product updateProduct(Long id, Product updatedProduct) {
    Optional<Product> optionalProduct = productRepository.findById(id);
    if (optionalProduct.isPresent()) {
        Product product = optionalProduct.get();
        product.setName(updatedProduct.getName());
        product.setDescription(updatedProduct.getDescription());
        product.setPrice(updatedProduct.getPrice());
        // Atualize outros atributos, se houver

        return productRepository.save(product);
    } else {
        return null;
    }
}
public void deleteProduct(Long id) {
    productRepository.deleteById(id);
}

}


import br.com.compassuol.sp.challenge03.apigateway.service.OrderService;

import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.Order;

@RestController @RequestMapping("/orders") @RequiredArgsConstructor public class OrderController {

private final OrderService orderService;

@GetMapping
public ResponseEntity<Page<Order>> getAllOrders(Pageable pageable) {
    Page<Order> orders = orderService.getAllOrders(pageable);
    return new ResponseEntity<>(orders, HttpStatus.OK);
}

@PostMapping
public ResponseEntity<Order> createOrder(@RequestBody Order order) {
    Order createdOrder = orderService.createOrder(order);
    return new ResponseEntity<>(createdOrder, HttpStatus.CREATED);
}

@GetMapping("/{id}")
public ResponseEntity<Order> getOrderById(@PathVariable Long id) {
    Order order = orderService.getOrderById(id);
    if (order != null) {
        return new ResponseEntity<>(order, HttpStatus.OK);
    }
    return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

@PutMapping("/{id}")
public ResponseEntity<Order> updateOrderStatus(@PathVariable Long id, @RequestParam String status) {
    Order updatedOrder = orderService.updateOrderStatus(id, status);
    if (updatedOrder != null) {
        return new ResponseEntity<>(updatedOrder, HttpStatus.OK);
    }
    return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteOrder(@PathVariable Long id) {
    orderService.deleteOrder(id);
    return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

}

1 resposta

Cesar, boa tarde.

Tente adaptar o seu codigo colocando o PagedModel.

@GetMapping
public ResponseEntity<Page<Order>> getAllOrders(
            @RequestParam(value = "page", defaultValue = "0") Integer page,
            @RequestParam(value = "size", defaultValue = "12") Integer size,
            ) {
        
    Pageable pageable = PageRequest.of(page, size));
    
     return ResponseEntity.ok(service.findAll(pageable));
}

Pois você não está passando esses parametros na linha da URL (variavel) e simplesmente você colocou para receber um parametro que não recebe nunca.