Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
5.5 kB
4
Indexable
Never
controller.java

package com.example.warehouse.controller;


import com.example.warehouse.model.Product;
import com.example.warehouse.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
public class ProductController {

    /*
  This controller would be responsible for the ProductController endpoints
  Add the required annotations and create the endpoints
   */

   @Autowired
    private ProductService productService;

    //to add the Product details using Product object
    @PostMapping("/product/add")
    private Object postProduct(@RequestBody Product product){
        //return null;
    	return productService.postProduct(product);
    	
    }

    //to get all the Product details
    @GetMapping("/product/get")
    private Object getProduct(){
        return productService.getProduct();
    }


    //to update the product with id as pathVariable and product as object in RequestBody
    @PutMapping("/product/{id}")
    public ResponseEntity<Object> updateProduct(@PathVariable int id,@RequestBody Product product){
        return productService.updateProduct(id, product);
    }

    // to delete the product by using id as PathVariable
    @DeleteMapping("/product/{id}")
    public ResponseEntity<Object> deleteProductById(@PathVariable int id){
        return productService.deleteProductById(id);
    }
    

    //to get the product items by vendor
    @GetMapping("/product/vendor")
    public ResponseEntity<Object> getSimilarVendor(@RequestParam String value){
        return productService.getSimilarVendor(value);
    }
}







-------------------

service.java

package com.example.warehouse.service;

import com.example.warehouse.model.Product;
import com.example.warehouse.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service
public class ProductService {

    /*
   Implement the business logic for the ProductService  operations in this class
   Make sure to add required annotations
    */

    @Autowired
    private ProductRepository productRepository;


    //to post all the Product details
    //created->201
    //badRequest->400
    public Object postProduct(Product product) {
       if(product.getName().isEmpty()||product.getDescription().isEmpty()||product.getVendor().isEmpty()||product.getCurrency().isEmpty()||product.getImage_url().isEmpty()||product.getSku().isEmpty()||product.getPrice()<0||product.getStock()<0) {
    	   return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
       }
       if(productRepository.existsBySku(product.getSku())||productRepository.existsById(product.getId())) {
    	   return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
       }
       
      Product p=productRepository.save(product);
      return new ResponseEntity<>(p,HttpStatus.CREATED);
    }
    
    

    //to get all the Product details
    //ok->200
    //badRequest->400
    public Object getProduct() {
        //return null;
    	 List<Product> a=productRepository.findAll();
    	 if(!a.isEmpty()) {
      	   return new ResponseEntity<>(a,HttpStatus.OK);
         }
    	 else {
    		 return new ResponseEntity<>(HttpStatus.BAD_REQUEST);  
    	 }
    }

    //to get the product with the value(pathVariable)
    //ok()->200
    //badRequest()->400
    public ResponseEntity<Object> getSimilarVendor(String value) {

       List<Product> a=productRepository.findByVendor(value);
       if(!a.isEmpty()) {
    	   return new ResponseEntity<>(a,HttpStatus.OK);
       }
       else {
    	   return new ResponseEntity<>(HttpStatus.BAD_REQUEST); 
       }
    }


    //to update the Product with id as pathVariable and Product as object in RequestBody
    //ok->200
    //badRequest->400
    public ResponseEntity<Object> updateProduct(int id, Product product) {
       // return null;
    	Product p=productRepository.findById(id).orElse(null);
    	if(p!=null) {
    		p.setPrice(product.getPrice());
    		p.setStock(product.getStock());
    		
    		return new ResponseEntity<>(productRepository.save(p),HttpStatus.OK);
    	}
    	else {
    		return new ResponseEntity<>(HttpStatus.BAD_REQUEST); 
    	}
    }

    // to delete the product by using id as PathVariable
    //ok->200
    //badRequest->400
    public ResponseEntity<Object> deleteProductById(int id) {
       //return null;
    	Product p=productRepository.findById(id).orElse(null);
    	if(p!=null) {
    		
    		productRepository.deleteById(id);
    		return new ResponseEntity<>(HttpStatus.OK);
        	
    	}
    	else {
    		return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    	}
    }



}




-------------------------------------

repo.java

package com.example.warehouse.repository;


import com.example.warehouse.model.Product;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductRepository extends JpaRepository<Product,Integer> {
List<Product> findByVendor(String value);

boolean existsBySku(String sku);
}
------------------------------------------------
Leave a Comment