package com.example.ecommerce.controller;
import com.example.ecommerce.dto.ProductDto;
import com.example.ecommerce.service.ProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;
@RestController
@RequestMapping("/api/products")
@RequiredArgsConstructor
public class ProductController {
private final ProductService productService;
@GetMapping
public ResponseEntity<Page<ProductDto>> getAllProducts(
@PageableDefault(size = 10, sort = "id") Pageable pageable) {
Page<ProductDto> products = productService.getAllProducts(pageable);
return ResponseEntity.ok(products);
}
@GetMapping("/{id}")
public ResponseEntity<ProductDto> getProductById(@PathVariable Long id) {
ProductDto product = productService.getProductById(id);
return ResponseEntity.ok(product);
}
@GetMapping("/category/{categoryId}")
public ResponseEntity<Page<ProductDto>> getProductsByCategory(
@PathVariable Long categoryId,
@PageableDefault(size = 10, sort = "id") Pageable pageable) {
Page<ProductDto> products = productService.getProductsByCategory(categoryId, pageable);
return ResponseEntity.ok(products);
}
@GetMapping("/price")
public ResponseEntity<List<ProductDto>> getProductsByPriceRange(
@RequestParam BigDecimal minPrice,
@RequestParam BigDecimal maxPrice) {
List<ProductDto> products = productService.getProductsByPriceRange(minPrice, maxPrice);
return ResponseEntity.ok(products);
}
@GetMapping("/search")
public ResponseEntity<Page<ProductDto>> searchProducts(
@RequestParam String keyword,
@PageableDefault(size = 10, sort = "id") Pageable pageable) {
Page<ProductDto> products = productService.searchProducts(keyword, pageable);
return ResponseEntity.ok(products);
}
@PostMapping
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<ProductDto> createProduct(@Valid @RequestBody ProductDto productDto) {
ProductDto createdProduct = productService.createProduct(productDto);
return ResponseEntity.status(HttpStatus.CREATED).body(createdProduct);
}
@PutMapping("/{id}")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<ProductDto> updateProduct(@PathVariable Long id, @Valid @RequestBody ProductDto productDto) {
ProductDto updatedProduct = productService.updateProduct(id, productDto);
return ResponseEntity.ok(updatedProduct);
}
@DeleteMapping("/{id}")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<Void> deleteProduct(@PathVariable Long id) {
productService.deleteProduct(id);
return ResponseEntity.noContent().build();
}
@PatchMapping("/{id}/stock")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<Void> updateProductStock(@PathVariable Long id, @RequestParam Integer quantity) {
productService.updateProductStock(id, quantity);
return ResponseEntity.noContent().build();
}
}