Skip to content

Latest commit

 

History

History
174 lines (115 loc) · 4.15 KB

File metadata and controls

174 lines (115 loc) · 4.15 KB

Spring Boot MVC Rest API using REACT UI

    1. Common Mapping/ API end point

2. Making the DB Model and Connection

3. Fetching all Products from DB

4. Response Entity

  • Writing the Response code via Response Entity
- sending the Different Status Code
- 200 - OK (GET)
- 201 - OK Created (Post)
    @Autowired
    private ProductService productService;
    @GetMapping("/products")
    public ResponseEntity<List<Product>> getProducts()
    {
        // return productService.getAllProducts();

        // returning the custom http status 
        return  new ResponseEntity<>(productService.getAllProducts(), HttpStatus.ACCEPTED);

    }

5. Fetch product By Id

Controller

 // Controller to return the product by ID  
    @GetMapping("/product/{id}")
    public ResponseEntity<Product> getProductById(@PathVariable int id)
    {
        Product product = productService.getProductById(id);
        if(product.getId()>0)
        {
            return new ResponseEntity<>(product, HttpStatus.OK);

        }
        else
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }

6. Add Product with Image

Handling the image

Model

    private String imageName;
    private String imageType;

    @Lob  // large binary object 
    private byte[] imageData;


// Hndling the Images 
// By using the base 64 encoder 

//1.  convert images into text from and decode 
//2.  send the json and image seperateley (Using the 2nd way )

Controller

// Image Controller , 

    @PostMapping("/product")
    public ResponseEntity<?>  addProduct(@RequestPart Product product, @RequestPart MultipartFile imageFile)
   {
        Product saveProduct = null;
        try {
            saveProduct = productService.addProduct(product, imageFile);
            return new ResponseEntity<>(saveProduct, HttpStatus.CREATED);

        } catch (IOException e) {
            // throw new RuntimeException(e);
            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    

7. Fetch Product Image

  • Use the front-end 3
    
// Image Fetching Controller 

    @GetMapping("product/{productId}/image")
    public ResponseEntity<byte []> getImageByProductId(@PathVariable int productId)
    {
        Product product = productService.getProductById(productId);

        if(product.getId()>0)
            return new ResponseEntity<>(product.getImageData(), HttpStatus.OK);
        else
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);    

    }

8. Update and Delete Product

// Delete Controller 
    @DeleteMapping("/product/{id}")
    public ResponseEntity<String> deleteProduct(@PathVariable int id)
    {
        Product product = productService.getProductById(id);
        if(product != null){
            productService.deleteProduct(id);
            return new ResponseEntity<>("Deleted", HttpStatus.OK);
        }
        else
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }

// In service use the deleteByID(id)

9. Searching the Product by keyword

  
    @Query
    ("SELECT p FROM Product p WHERE " +
       "LOWER(p.name) LIKE LOWER(CONCAT('%', :keyword, '%')) OR " +
       "LOWER(p.brand) LIKE LOWER(CONCAT('%', :keyword, '%')) OR " +
       "LOWER(p.description) LIKE LOWER(CONCAT('%', :keyword, '%')) OR " +
       "LOWER(p.category) LIKE LOWER(CONCAT('%', :keyword, '%'))")
// List<Product> searchProducts(@Param("keyword") String keyword);
     List<Product> searchProducts(String keyword);